How to Install and Switch between Multiple Python Versions on Mac

In this simple tutorial I will demonstrate to Install and Switch between Multiple Python Versions on Mac. Sometimes you want to use latest version for your new application while you may need a minimum version of python such as 2.7.* for supporting your legacy or an old python application.

How to Install and Switch between Multiple Python Versions on Mac

So let’s start this with getting the current version. In your mac OS use terminal to display current version of Python. You can open the terminal by pressing Command ⌘ + Spacebar > Typeterminal‘ > Then click on the Terminal app from the results.

Type:

python --version

It should show something like:

Python 2.7.16

You can also show the executable file current in use as default version of python by typing:

which python

Outputs such as:

/usr/bin/python

Installing Multiple Python Versions

In order to Install and Switch between Python Versions on Mac we will use pyenv which is a great tool to install and switch between python versions. You could do it manually but using this lightweight tool pyenv is recommended since you do not need to edit ENV paths manually. All you need to is run the command in the terminal.

Lets install pyenv

First update brew

brew update

Install pyenv

brew install pyenv

Install the Pyenv Dependencies

brew install openssl readline sqlite3 xz zlib

View Available Python Versions

Using list flag –list with Pyenv displays all available Python versions one can install using Pyenv. It includes final versions that are released from Python, Anaconda, PyPy, Jython, and Stackless. This includes all the major, minor, and micro versions but it doesn’t include alpha, beta, or release candidate versions. The list something like:

Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4.0
  2.4.1
  2.4.2
  2.4.3
  2.4.4
  ...
  3.4.8
  3.4.9
  3.4.10
  3.5.0
  3.5-dev
  3.5.1
  3.5.2
  ...
  3.12.0a1
  3.12-dev
  activepython-2.7.14
  activepython-3.5.4
  activepython-3.6.0
  anaconda-1.4.0
  anaconda-1.5.0
  anaconda-1.5.1

etc., a long list of python versions as shown in the block above.

Install Python Versions

pyenv install 3.11.0

You can repeat this step to install more Python versions.

List Python Versions

Once installed you can list installed version using

pyenv versions

Set Global Version

pyenv global 3.11.0

Show current version

pyenv version

In order to make pyenv version available to our system we need to update our .zshrc or .bash_profile or .bashrc files

if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

Save it and then restart the close and re-open the bash window. Then check the python version:

python --version
Python 3.11.0

I hope this helps you to install multiple Python versions on your Mac pc.

Read Also:

Leave a Reply