Pyenv - for properly maintaining different versions of Python

Posted on Sun 03 May 2020 in Python

Your computer may have Python 2.7 installed or you may have some older version of Python 3 installed. How do you update the Python versions? Simply upgrading to a higher version of Python is not a good idea. Many programs may be depending on a specific version of Python and those programs will break if you simply upgrade the Python version.

We need a way to install, maintain and switch between Python versions easily. This is were the tool Pyenv becomes very useful.

Let's see this in action.

Pyenv Installation

On Linux and macOS

Use the pyenv-installer script

# curl https://pyenv.run | bash

At the end of the installation you will see some code you need to copy into your shell's 'rc' file (.bashrc for bash, .zshrc for zsh etc.). Make sure you follow the steps and then restart the terminal.

Pyenv installs the different Python versions by compiling them from sources. This requires some dependencies installed on your computer. Check this link for a list of dependencies on different operating systems.

On Windows

The installation instructions for Windows is given here

How to use Pyenv

Install a version of Python

To install a version of Python, we need to know what are the Python versions that are available. To list the different Python versions available, execute the following command:

# pyenv install --list
Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4.0
  2.4.1
  2.4.2
  2.4.3
...
  3.6.4
  3.6.5
  3.6.6
  3.6.7
  3.6.8
  3.6.9
  3.6.10
  3.7.0
  3.7-dev
  3.7.1
  3.7.2
...
  3.6.4
  3.6.5
  3.6.6
  3.6.7
  3.6.8
  3.6.9
  3.6.10
  3.7.0
  3.7-dev
  3.7.1
  3.7.2
...

To install a specific version of Python, simply run:

# pyenv install 3.8.2
Downloading Python-3.8.2.tar.xz...
-> https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz
Installing Python-3.8.2...
Installed Python-3.8.2 to /home/nitin/.pyenv/versions/3.8.2

When the installation finishes, you can see the different Python versions available on your system using:

# pyenv versions
* system (set by /home/nitin/.pyenv/version)
  3.8.2

The version in the result which is marked by * indicates the current global version of Python that is available on your system.

To change the Python version run:

# pyenv global 3.8.2

Confirm that the current Python version has changed:

# pyenv versions
  system
* 3.8.2 (set by /home/nitin/.pyenv/version)

If Pyenv does not detect the new version of Python, run:

# pyenv rehash

To set a Python version locally for a directory and its subdirectories, run:

# pyenv local 3.7.4

This Python version will be active only for that directory, while the global Python version is not modified.

To set a Python version for a shell session, run:

# pyenv shell 3.7.4

This version of Python will be active only for the current shell session.

Conclusion

In this article, I have talked about Pyenv. It is the tool you need to use, if you want to upgrade your Python versions or to maintain different versions of Python on the same system. Pyenv makes installation and switching of Python versions a breeze. I hope you have enjoyed this article.

That's it for now, until next time.