Post

How to install multiple Python versions on macOS?

A comprehensive tutorial on how to manage and use multiple versions of Python on Mac

How to install multiple Python versions on macOS?

A step by step tutorial for beginners to install and use multiple versions of Python on macOS

If you are an absolute beginner and just started Python programming and want to setup Python coding environment on you Mac then you probably don’t need multiple versions. In that case I would suggest you to follow this tutorial instead: How to setup Python coding environment on macOS.

1. Introduction

There are plenty of tutorials on how to use Python right way or manage multiple versions of python on Mac. And then comes the swarm of other python packages and dependencies to work with Python, namely pyenv, pyvenv, virtualenvwrapper, pip-tool, poetry, anaconda etc. Well, this seem overwhelming for beginners. In no way I am against using any of these packages but things can be simplified and you can manage multiple versions of Python and use virtual environment without using any of the packages mentioned earlier, specially without using pyenv. The only package you will need is a package manager for macOS and then pip (to install Python packages) and venv (to isolate project-specific dependencies from a shared Python installation) will be suffice for any Python project for beginners.

2. Install a Package Manager

Install MacPorts if you don’t have it already on your Mac. Follow this detailed tutorial on How to install MacPorts on Mac?.

3. Install Python

Let’s check all the available versions of Python

1
port search --name --line --regex '^python\d*$'

it will return something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
python26	2.6.9	lang	An interpreted, object-oriented programming language
python27	2.7.18	lang	An interpreted, object-oriented programming language
python32	3.2.6	lang	An interpreted, object-oriented programming language
python33	3.3.7	lang	An interpreted, object-oriented programming language
python34	3.4.10	lang	An interpreted, object-oriented programming language
python35	3.5.10	lang	An interpreted, object-oriented programming language
python36	3.6.15	lang	An interpreted, object-oriented programming language
python37	3.7.17	lang	An interpreted, object-oriented programming language
python38	3.8.18	lang	An interpreted, object-oriented programming language
python39	3.9.18	lang	An interpreted, object-oriented programming language
python310	3.10.13	lang	An interpreted, object-oriented programming language
python311	3.11.7	lang	An interpreted, object-oriented programming language
python312	3.12.1	lang	An interpreted, object-oriented programming language

For this tutorial we will install Python versions 3.11.7 and 3.12.1. Along with these Python versions we need to install pip packages for both versions.

1
sudo port install python311 python312 py311-pip py312-pip

It will give the list for dependencies and ask you if you want to continue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
--->  Computing dependencies for python311
The following dependencies will be installed: 
 bzip2
 expat
 gettext
 gettext-runtime
 gettext-tools-libs
 gperf
 libedit
 libffi
 libiconv
 libtextstyle
 openssl
 openssl3
 pkgconfig
 python3_select
 python3_select-311
 python_select
 python_select-311
 sqlite3
 xz
 zlib
Continue? [Y/n]: y

After installation is complete, checkout the versions installed

1
python3.11 --version; python3.12 --version 

You should see this output

1
2
Python 3.11.7
Python 3.12.1

4. Switch between different versions of installed Python

You can switch between these installed versions by using the command python3.11 or python3.12. Type these command one by one in the terminal and seeit in action.

which python3 will give the system’s Python /usr/bin/python3 and this is the default Python on your Mac.

You can set any of these installed versions as the default version. Below command will set python3.12 as default version.

1
 sudo port select --set python3 python312

If you will type which python3 then it will show /opt/local/bin/python3 (the one installed and set default by port).

If you have installed multiple versions of Python using port, e.g. python310, python311 and python312, then each of these will be active and you can switch between them by using the command python with their version suffix, e.g. python3.10, python3.11 and python3.12 respectively, in the terminal. You can make any these versions as the default python by running the sudo port select --set python3 python<version> command.

You can use pip-<version> command to switch between pip for each version. For example, pip-3.11 or pip-3.12.

For each Python application create a new virtual environment

1
 python3.12 -m venv myvenv

Activate this environment using the command

1
source myvenv/bin/activate

Once activated, the default Python for this environment will be python3.12, that means you can use just python or python3 instead of python3.12 in this environment even if Python 3.12 is set as the default version. You can check it anytime by running which python3 command

1
which python
1
/Users/dankhan/demo-venv/bin/python3

To install project level dependencies you can use either pip or pip3 in this environment (both are same in the virtual environment). Outside the environment you have to use pip-3.12 for Python 3.12 version.

1
pip install numpy pandas scipy matplotlib

Run deactivate to deactivate the virtual environment.

Note that most of the IDEs or text editors offers feature to create or select virtual environment for your project. Once you create a virtual environment through CLI, you can select this environment and use it in your favorite IDE or text editor.

This post is licensed under CC BY 4.0 by the author.