Multiple python on MacOS

When work on different project using python, there comes a situation where we might be using different python versions across project. In this article let’s see how we can maintain multiple python versions on MacOS and switch between those versions based on the requirement.
Let’s break down this process into three simple steps:
- Installation of python on MacOS
- Installation of multiple pythons
- Switch between python versions
Step 1 — Installation of python on MacOS
Installing python is very simple, but the hard part is the installing without making any changes to pre-installed python version, which comes with MacOS. Please follow this guide, which explains how we can install python on MacOS using pyenv.
Step 2 — Installation of mutliple pythons
As explain in the step 1 process, we will be using pyenv to install multiple versions of python on our MacOS. pyenv
will help us install and maintain mutliple versions of python
Let’s start with list all the available python versions using the following command.
pyenv install -l
Select the version we want to install. Using the following command to install the required version of python.
pyenv install <python_version>
Replace
<python_version>
with an actually version. For example, If I want to install python version 3.10.0, I runpyenv install 3.10.0
Step 3 — Switching between versions
Switching between versions is very simple process, we will be using pyenv shell
command to switch between the versions.
pyenv shell <python_version>
<python_version>
should we replaced with actual version, example3.10.0
pyenv shell
command will only set the python version to the current terminal session which we are working on. Opening a new terminal shell will reset the python version to default.
Additional Notes
Whenever we want to check all the python versions available on our macos, run the following command which will list all the python versions.
pyenv versions
When all the versions are lists, notice that a star(*) icon is shown beside one of the version. It represents the python version, which is being used by that terminal session.
That is all, we have now successfully install all the python versions required and able to switch between to use a specific version.