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.
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
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.
Let’s check all the available versions of Python
port search --name --line --regex '^python\d*$'
it will return something like this
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.12.1. Along with these Python versions we need to install pip packages for both versions.
sudo port install python311 python312 py311-pip py312-pip
It will give the list for dependencies and ask you if you want to continue.
---> Computing dependencies for python311
The following dependencies will be installed:
Continue? [Y/n]: y
After installation is complete, checkout the versions installed
python3.11 --version; python3.12 --version
You should see this output
You can switch between these installed versions by using the command
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.
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
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
pythonwith their version suffix, e.g.
python3.12respectively, 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,
For each Python application create a new virtual environment
python3.12 -m venv myvenv
Activate this environment using the command
Once activated, the default Python for this environment will be
python3.12, that means you can use just
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
To install project level dependencies you can use either
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.
pip install numpy pandas scipy matplotlib
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.