DEV Community

Cover image for Pyenv to handle different versions of Python
jdxlabs
jdxlabs

Posted on • Updated on

Pyenv to handle different versions of Python

You may need to use different versions of Python on your computer, to make some programs work.

Today, I will explain how to use Pyenv, a software that lets you manage all available python versions on your computer.

First, install the binary :

brew install pyenv
pyenv --help
Enter fullscreen mode Exit fullscreen mode

Make sure you have all the recommended packages in your environment.
If necessary, the most frequent cases of installation problems are listed here.

Add Pyenv to your path :

# pyenv init
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

# Restart your shell for the changes to take effect.
Enter fullscreen mode Exit fullscreen mode

You can list the available versions of Python available for installation :

pyenv install -l
pyenv install -l | less
Enter fullscreen mode Exit fullscreen mode

Note that you will find versions packaged with Anaconda, Minicoda, Miniforge, Pypy, etc. The first versions of the list are not affected by any particular packages.

Install Python 2.7 and select it

pyenv install 2.7.18
pyenv global 2.7.18
which python
python -V
pip -V
Enter fullscreen mode Exit fullscreen mode

Install Python 3 and select it

pyenv install 3.11.4
pyenv global 3.11.4
which python
python -V
pip -V
Enter fullscreen mode Exit fullscreen mode

You can list the versions you have already installed and which version is currently selected :

pyenv versions
Enter fullscreen mode Exit fullscreen mode

With Pyenv, you can easily install and manage specific versions of Python directly on your computer.

There are similar tools that I commonly use for Terraform (tfenv) and NodeJS (nvm), this could be the subject of a future article of the same type.

You can also see how to use the virtual environments in addition, to manage different versions of packages for your different projects.

Top comments (0)