Managing python versions and virtual environments with pyenv

What is pyenv?

pyenv is a tool that lets you easily switch between multiple versions of Python, and it is really useful to work on python projects that require different python versions. If you are familiar with nvm the node version manager, pyenv follows a similar approach.
Another big advantage of pyenv is the use of pyenv-vitualenv which completely isolates environments. I know you can use python -m venv but, having completely isolated python installations and requisites makes the programming process more reproducible (say, to get things inside containers, or have proper specs to run the code)

Installing pyenv

Debian pre-requisites

On a freshly installed Debian 11 installation i had to install some packages prior being able to use pyenv successfully

apt install libreadline-dev libncursesw6 libssl-devlibbz2-dev zlib1g-dev libsqlite3-dev tk-dev liblzma-dev

Getting Pyenv it

The fastest way to install pyenv is to run,

curl https://pyenv.run | bash

and follow the prompted instructions. To actually understand better ways to configure and install pyenv go to https://github.com/pyenv/pyenv and read the docs there.

Configure the shell (bash)

In bash, you need to modify your .bashrc o .bash-profile (depending on your particular configuration) and add the lines (as indicated on the pyenv docs),

sed -Ei -e '/^([^#]|$)/ {a \
export PYENV_ROOT="$HOME/.pyenv"
a \
export PATH="$PYENV_ROOT/bin:$PATH"
a \
' -e ':a' -e '$!{n;ba};}' ~/.profile
echo 'eval "$(pyenv init --path)"' >>~/.profile

echo 'eval "$(pyenv init -)"' >> ~/.bashrc

In my case I only changed .bashrc by adding,

# PyENV python manager
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"

And it was enough.

Run some tests

You can use pyenv doctor already shipped with pyenv, in order to check whether it was properly installed or not

How to use it?

Once installed an properly configured, one can add python versions by running

$ pyenv update
$ pyenv install <<python version>>

If it fails, it may be that the build environment is not properly set, and you’d need to follow the docs here

Pyenv virtual environments

To take advante of virtual environments using pyenv, which translates into having different virtual environments with their own python version, we must install a plugin called pyenv-virtualenv

git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

And to enable auto-activation of virtualenvs, add pyenv virtualenv-init to your shell. In bash this is achieved by,

$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc

Creating virtual-envs

pyenv virtualenv <python-version> <virtual-env-name>

for instance

pyenv virtualenv 3.10.4 my-django-env-3.10.4

Will create virtualenv called my-django-env-3.10.4 using python version 3.10.4. Virtual envs created are shown by

pyenv virtualenvs

You activate/deactivate virtualenvs using

# Activate
pyenv activate <virtual-env-name>

# Deactivate
pyenv deactivate

Finally, to delete virtualenvs

pyenv virtualenv-delete <virtual-env-name> 
comments powered by Disqus