Every developer will face this issue one day when his projects require multiple versions of Python. For example, project A demands Python 3.2 where as the project B requires Python 3.6 as the main dependency.
Sure, you can use containers to meet demands, but installing multiple containers just to have multiple versions of Python won’t make sense, especially when dealing with resource-constrained environment.
So what do you do in that case? You can use pyenv which lets you switch between multiple versions of Python, and in this tutorial, I will walk you through how you can install and use pyenv to manage multiple versions of Python on a single machine.
How to Install Pyenv
There are two ways you can install pyenv on your computer:
- Using the brew package manager (preferred for macOS)
- Using a cURL script to automate the installation (preferred for Linux and WSL)
Let’s start with the first one.
How to Install Pyenv Using Brew Package Manager
If you’re a mac user or have configured the brew package manager on Linux, then it can be used directly to install Pyenv.
First, update the repository index of the Brew using the following command:
brew update
Now, you can install Pyenv through the Brew package manager by executing the following:
brew install pyenv
Optionally, if you want to have the latest development head of Pyenv rather than the latest release, execute the following command:
brew install pyenv --head
How to Install Brew Using cURL script
If you don’t prefer using an external package manager and want to install the latest version of Brew through a single command, then you can use the cURL script, which will fetch you the most recent stable release of Brew on Linux and UNIX-like operating systems:
curl https://pyenv.run | bash
Once done, you have to configure it according to your shell.
For bash user:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
Now, restart the shell using the following command:
exec "$SHELL"
For zsh users:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
Use the following to restart the shell:
exec "$SHELL"
Use Pyenv to Manage Multiple versions of Python
To install multiple versions of Python, you’d have to use the pyenv command in the given syntax:
Pyenv install <python-version>
For example, if I want to install Python 3.10.4, then I will use the following commands:
pyenv install 3.10.4
My project also required Python 3.12.4, so I installed it by executing the following:
pyenv install 3.12.4
Switch between multiple Python Versions
Pyenv allows you to switch between global Python versions and specify a specific version for the project directory.
To specify a global version of Python, you can use the global option as shown below:
pyenv global <python-version>
If I want to set Python version 3.10.4 as a global version of Python, then I’ll use the following command:
pyenv global 3.10.4
But if you want to configure a Python version for a specific project directory, then first switch to that directory and use the local flag as shown below:
Pyenv local <python-version>
Let’s say I want to set Python version 3.12.4 for a local directory named my-project; then, I will use the following command:
pyenv local 3.12.4
As you can see, when I changed my directory, it displayed the global Python version (configured earlier), whereas when I checked for the Python version for the project directory, it showed Pythen version 3.12.4.
How to Uninstall Multiple Versions of Python from Pyenv
As time goes on, your project might not require the Python version that it required earlier, and you may install a new version. This way, you end up with an unnecessary version of Python installed on your system.
To uninstall the target Python version from your system, you can use the uninstall flag with the pyenv command, as shown here:
pyenv uninstall <python-version>
Let’s say I want to uninstall Python version 3.12.4; then, I will execute the following command:
pyenv uninstall 3.12.4
There you go!
Conclusion
In this tutorial, I went through how you can install and use the pyenv CLI utility to install and manage multiple versions of Python. I also went through how you can configure a specific version of Python for a specific project directory.
I hope you will find this tutorial helpful. If you have any questions, leave us a comment.