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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
python --version
python --version
python --version

It should show something like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Python 2.7.16
Python 2.7.16
Python 2.7.16

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
which python
which python
which python

Outputs such as:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/usr/bin/python
/usr/bin/python
/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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
brew update
brew update
brew update

Install pyenv

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
brew install pyenv
brew install pyenv
brew install pyenv

Install the Pyenv Dependencies

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
brew install openssl readline sqlite3 xz zlib
brew install openssl readline sqlite3 xz zlib
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pyenv install 3.11.0
pyenv install 3.11.0
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pyenv versions
pyenv versions
pyenv versions

Set Global Version

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pyenv global 3.11.0
pyenv global 3.11.0
pyenv global 3.11.0

Show current version

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pyenv version
pyenv 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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
if command -v pyenv 1>/dev/null 2>&1; then eval "$(pyenv init -)" fi
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
python --version
python --version
python --version
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Python 3.11.0
Python 3.11.0
Python 3.11.0

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

Read Also:

Leave a Reply