Get a grip to your Python versions in macOS

There are multiple ways to manage and use Python with macOS, from using just system Python (comes with macOS) or managing it directly with Homebrew. I wanted to start from clean slate and here’s what I did. It will give you control which version of Python you are using and when. And no need for manual aliasing anymore.

First we need to install Homebrew. Homebrew is my go-to manager for applications. It keeps apps organised and updated, and it’s super easy to use. Install Homebrew by pasting the following command to your terminal. If you are not super confident with running random scripts from internet (like you should), you can download that .sh file and examine it. The command is copied from the site https://brew.sh:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Then we need Pyenv. Pyenv is a tool that lets you easily switch between the multiple versions of Python. Use this command to execute installation:

brew install pyenv

Now when we have Pyenv installed, we can install any Python version. I’m using at the moment the latest, which is 3.10.0, but you can install newer if it’s available. A list of available versions can be found on Python website. Install with the following command:

pyenv install 3.10.0

After you have successfully installed the needed version, you can set it to be the default version in any directory:

pyenv global 3.10.0

We also need to update your configuration file, macOS has moved using Bash to ZSH, but if you are not sure which shell you’re using, run: ps -p$$ -ocommand= (the output will tell you)

Then run the following commands to update your profile configuration file:

IF YOU ARE USING ZSH:
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.zshrc

echo 'PATH=$(pyenv root)/shims:$PATH' >> ~/.zshrc


IF YOU ARE USING BASH:
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

echo 'PATH=$(pyenv root)/shims:$PATH' >> ~/.bash_profile

To confirm you’re using the right version of Python, type:

python -V

You should see the installed version as output. And check that the PATH is working properly by typing:

which python

Output should be /Users/xxx/.pyenv/shims/python (except in xxx should be your account name).

And you’re ready to ro.. code! If you would like to use a specific Python version in your project just install it first (in this example I will use an older version 3.8)

pyenv install 3.9.7

Activate the version in ANY directory (globally) with:

pyenv global 3.9.7

Or just a specific directory with:

pyenv local 3.9.7

That’s it! I hope it makes your life easier, like it did mine 🙂 This was just a high-level guide, not going too deeply how things work under hood, but if you would like to dig into it, check the following link:

https://realpython.com/intro-to-pyenv/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: