How To Manage Different Python3 Versions On Your Mac (Safely)

Default Python Installation

As you probably already know, your Mac comes with a pre-installed version of Python.

  • Run the python3 --version command to find your system's default version.
  • Run the which python3 to see where the Python has been installed.

🚨 Do not tamper with your system’s default Python installation. If you do, there is a good chance that you will break something.

Path

The Path environment variable is why you can run python3 ... from anywhere. Your system checks every directory listed in the Path (separated by a : ), from left to right, for the python3 interpreter. The first that contains python3 is the one that gets used.

  • Run the env | grep PATH command to see the contents of your Path.

In my system, the only directory that contains python3 is /usr/bin .

Multiple Python Versions

I suggest installing different Python versions with Homebrew. You can easily do this with a command like brew install python@3.8 , brew install python@3.10 , etc.

Homebrew typically installs all Python interpreters in the /opt/homebrew/bin .

  • Run ls /opt/homebrew/bin | grep python3 to view which interpreters you have installed.

In my case, I have versions 3.8 and 3.10 .

💡 Learn how to configure your environment to work with Python in VS Code using WSL from Microsoft:

Work with Python in VS Code using WSL from Microsoft

👉 To read more such acrticles, sign up for free on Differ.

Taking a look at the path again, notice that the opt/homebrew/bin directory comes before /usr/bin .

Run the export PATH=/opt/homebrew/bin:$PATH command if this isn’t the case on your machine. You should also append this command to the end of your ~/.zshrc file.

Say we want to make it so that python3 points to version 3.8 .

  • Run cd /opt/homebrew/bin to change into the directory containing the desired interpreter.
  • Create a symlink to the python3.8 interpreter and call it python3 using the ln -sf python3.8 python3 command.

  • Confirm the symlink by listing the contents of the directory using ls -l /opt/homebrew/bin | grep python3 .

Note the second entry python3 -> python3.8 .

Confirming The Python3 Version

Let’s execute the same commands we used at the beginning of this article.

Perfect! python3 now points to the version 3.8.16 . Remember, this only happens because /opt/homebrew/bin comes before the /usr/bin in the Path environment variable.

To switch to another version, you remove the link by navigating to the /opt/homebrew/bin directory and running the rm python3 command. Then create a new link with any other version you have available (e.g. ln -sf python@#.# python3 ).

Enjoyed this article?

Share it with your network to help others discover it

Continue Learning

Discover more articles on similar topics