How to use pyenv to manage Python versions

Hello geeks, Recently, I was struggling with the Python version. We are connected with Python development because we have to manage different environments with different Python versions.

Quazi Md Aldin Fardin
3 min readAug 2, 2024

So, this is a mess when we install different Python versions manually . like python executable of path such as python, python2, python3, not only this python 3 has different version like python3.2 , python3.6, python3.7, and so on. Also, it becomes really hell to distinguish minor versions, such as Python 3.10.3 from Python 3.10.4.

Form this hell to rescue I found a superman tool named pyenv . This is command line tool that help us install, manage python version . we can switch one version to another version in python simply by running some commands .

if you want to learn how this tool works on more detail, you can checkout their github readme file . for your convenience, I am adding this below.

Let’s move on to the second part.

pyenv offers you multiple os support like Linux, MacOS, Windows . But today we are going to install this on Linux only (because I am a Linux fan) , so the show must go on .

1. Update and Install Dependencies

We need to ensure our package cache is updated, then install the dependencies to download, and build Python from Pyenv.

to install pyenv in Linux, just open your terminal and type

sudo apt update

then we need to install some necessary supporting dependencies for this

sudo apt-get install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

then we are going to install this by curl

2. Install Pyenv using Automatic installer

curl https://pyenv.run | bash

3. Configure user profile to use pyenv

Ensure the following is in your ~/.bash_profile (if exists), ~/.profile (for login shells), ~/.bashrc (for interactive shells), or ~/.zshrc

export PYENV_ROOT="$HOME/.pyenv" [[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init -)"

Optionally enable pyenv-virtualenv

eval "$(pyenv virtualenv-init -)"

4. Reload your profile

source ~/.bashrc

moving on third section

How does PYENV actually work?

For me, the best part about pyenv is that it’s so easy to use that I just use it to set up a virtual environment at the start of a project.

I no longer need to deal with pyenv after I’ve constructed a virtual environment using a particular Python version (which I obtain via pyenv).

I simply activate the virtual environment to obtain that version going forward.

Tell me, how can I accomplish this?

First, check if the Python version you want is available pyenv — version to view recent versions; you may need to update pyenv to do this):

pyenv install --list

This will give you a list of all available Python versions. this seems like this below.

❯ pyenv install --list
Available versions:
. . .
3.11-dev
3.11.1
3.11.2
3.11.3
3.11.4
3.11.5
3.11.6
3.11.7
3.12.0
3.12-dev
3.12.1
3.13.0a2
3.13-dev
. . .

so you can choose any of version from the list

To install a Python version:

pyenv install 3.13-dev

Selecting a Python version

You can see the currently-selected Python version with this command:

pyenv version

it’s going to be look like this

❯ pyenv version
3.12.1 (set by /home/aldinn/.pyenv/version)

for seeing all installed python vesion

pyenv versions

by doing so, you will get this type of output

❯ pyenv versions   
system
3.9.12
* 3.12.1 (set by /home/aldinn/.pyenv/version)

You can have a globally selected Python version, and then locally selected Python versions. The global version is used if no local version is selected.

You can select a local version with:

pyenv local 3.12.1

for global version

pyenv global 3.9.12

Using the selected Python version

Now, let’s create a virtual environment using pyenv:

pyenv exec python -m venv .venv

This uses pyenv exec to run the python command, which will use Python 3.12.1 in our case. The -m venv .venv argument passed to python tells it to run the venv module, and gives it the name of .venv.

This will create a virtual environment in a folder called .venv.

And to be honest, this is how I use pyenv. That’s it, nothing more!

--

--

Quazi Md Aldin Fardin
Quazi Md Aldin Fardin

Written by Quazi Md Aldin Fardin

learner, explorer, programmer and a human

No responses yet