UHG
Search
Close this search box.

How to Use Pyenv to Manage Multiple Versions of Python 

Install and manage multiple versions of Python using pyenv and manage Python dependencies without containers.

Share

use pyenv to manage multiple versions of Python
Table of Content

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
Install pyenv in macos using brew

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

install pyenv using cURL script

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 
install python 3.10.4 using pyenv

My project also required Python 3.12.4, so I installed it by executing the following:

pyenv install 3.12.4
install python 3.12.4 using pyenv

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
Change global version of Python using pyenv

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
change python version specifically for the project directory

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 
uninstall old python versions form pyenv

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.

📣 Want to advertise in AIM? Book here

Related Posts
19th - 23rd Aug 2024
Generative AI Crash Course for Non-Techies
Upcoming Large format Conference
Sep 25-27, 2024 | 📍 Bangalore, India
Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.
Flagship Events
Rising 2024 | DE&I in Tech Summit
April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore
Data Engineering Summit 2024
May 30 and 31, 2024 | 📍 Bangalore, India
MachineCon USA 2024
26 July 2024 | 583 Park Avenue, New York
MachineCon GCC Summit 2024
June 28 2024 | 📍Bangalore, India
Cypher USA 2024
Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA
Cypher India 2024
September 25-27, 2024 | 📍Bangalore, India
discord icon
AI Forum for India
Our Discord Community for AI Ecosystem, In collaboration with NVIDIA.