The right way to setup Python on Macos
I use Python more and more regularly and each time I set up a new machine I go through the same steps, I forget a step and am left scratching my head, this time I decided to document them.
Problem Statement:
- I want to use multiple versions of Python locally
- I do not want to override the default version of Python on the Mac in-case it’s required some something else.
- I do not want to edit symlinks to the python binaries
Solution:
The solution is pyenv. Pyenv allows me to install and manage as many version as python as I need and flip between them where required, it uses a convention to allow my terminal to use a version on Python installed under my home directory rather then over writing the global install.
In this example I am setting up my 2020 M1 MacBook pro, by default the Python version is:
and
I really want Python 3.10 for Pygame, and 3.9 for AWS Lambda, this is how you can accomplish that.
Installation
To accomplish this we first start with Homebrew, If your reading this your likely familiar, if not Homebrew is a software manager that is priamrliy used for MacOs ( though I do use it on linux ) to install the software I require with ease.
You can install HomeBrew in a terminal like so:
Next we update and install pyenv
Now I am use zsh if you are using bash that should read:
Now we have to reload our terminal
We now have pyenv correctly installed and ready for application.
Let’s install Python 3.10 and set it as the local version of Python to use for this terminal session.
I have Xcode installed, if you don’t then you may need to follow these steps first
Then install Python 3.10
OK, let’s check it out did it work?
No, not yet, we still need to enable it, do so for this session by running:
Great! Now how do we set that as the “global” default?
Open a new terminal session and you’ll see that it’s still 3.10.0
Additional Python versions
For this session we actually want Python 3.9.6, to accomplish this install 3.9.6 like so:
Then:
Perfect! Nice and clean and ready for development!