Use multiple Python versions and environments with pyenv

When using different Python-based tools, frameworks or applications, you need to install all the corresponding compatible versions of Python and easily switch between them. Assuming, you are running on Linux, you will enjoy using pyenv. It is one popular tool to manage several versions of Python, and even environments dedicated to projects.

Let's get started!

First, install pyenv using the provided installer script

A quick way to install pyenv is using the installation script "pyenv-installer" available on Github:

$ curl -L | bash

Then define the following environment variables needed for things to work smoothly.

The PYENV_ROOT variable, for the path where pyenv has been installed. You may use the default installation path, if that is your case, $HOME/.pyenv.

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc

The PATH variable, to make it easy to access the pyenv program directly from anywhere on the computer:

$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc

After these changes, you need to restart your shell, for these settings to become active. You may use the following trick to do that, so you don't have to close the current shell and launch another one:

$ exec $SHELL

Install and use any version of Python

To install version 3.6.1 of Python on the machine:

$ pyenv install 3.6.1
Downloading Python-3.6.1.tar.xz...
Installing Python-3.6.1...
Installed Python-3.6.1 to /home/kayeva/.pyenv/versions/3.6.1

To install version 2.7.13 of Python:

$ pyenv install 2.7.13
Downloading Python-2.7.13.tar.xz...
Installing Python-2.7.13...
Installed Python-2.7.13 to /home/kayeva/.pyenv/versions/2.7.13

By the way, if you want to get the list of all Python versions that pyenv can install for you, just use the install command with the -l option (short for --list). You will get a long list of available versions.

One very useful command is versions, which allows you to get the summary view of all installed versions on the system. For example:

$ pyenv versions
* system (set by /home/kayeva/.pyenv/version)

There is a feature which allows you to change the version that is considered by pyenv as the current global version of Python on the machine, using the global command. So at any point in time, you are pointing to one of the Python installations managed by pyenv, and using it as the default Python. And you can switch to another installed version when you want.

Let's use that feature here, and check the result with the versions command:

$ pyenv global 2.7.13  # Command to use version 2.7.13 as global
$ pyenv versions
* 2.7.13 (set by /home/kayeva/.pyenv/version)

You can then use the pip program that is part of the "global Python", for example here let's use it to add the requests module to that Python installation:

$ pip -V
pip 9.0.1 from /home/kayeva/.pyenv/versions/2.7.13/lib/python2.7/site-packages (python 2.7)
$ pip install requests
Collecting requests
  Using cached requests-2.13.0-py2.py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.13.0
$ ls -la /home/kayeva/.pyenv/versions/2.7.13/lib/python2.7/site-packages
total 64
drwxr-xr-x  9 kayeva kayeva  4096 Apr 29 19:15 .
drwxr-xr-x 28 kayeva kayeva 20480 Apr 29 13:41 ..
-rw-rw-r--  1 kayeva kayeva   126 Apr 29 13:41
-rw-rw-r--  1 kayeva kayeva   315 Apr 29 13:41 easy_install.pyc
drwxr-xr-x 10 kayeva kayeva  4096 Apr 29 13:41 pip
drwxr-xr-x  2 kayeva kayeva  4096 Apr 29 13:41 pip-9.0.1.dist-info
drwxr-xr-x  4 kayeva kayeva  4096 Apr 29 13:41 pkg_resources
-rw-r--r--  1 kayeva kayeva   119 Apr 29 13:41 README
drwxrwxr-x  3 kayeva kayeva  4096 Apr 29 19:15 requests
drwxrwxr-x  2 kayeva kayeva  4096 Apr 29 19:15 requests-2.13.0.dist-info
drwxr-xr-x  4 kayeva kayeva  4096 Apr 29 13:41 setuptools
drwxr-xr-x  2 kayeva kayeva  4096 Apr 29 13:41 setuptools-28.8.0.dist-info

To change back the "system Python", the one that comes with my Linux Ubuntu software packaging:

$ pyenv global system   # Command to use the system's Python as global
$ pyenv versions
* system (set by /home/kayeva/.pyenv/version)

And again when I need to set as global the Python version 3.6.1 that was installed with pyenv:

$ pyenv global 3.6.1

There is also a localcommand which is meant to be used for a Python "local to an application". I don't actually use this since I don't really have the need to ; I just use a dedicated isolated Python interpreter for each of my projects and Python application deployment, using the technique of virtual environments.

I will discuss, in a separate post, how to use the "virtualenv" tool to manage such environments with and without pyenv.

comments powered by Disqus

Need help for your project?

I can contribute to your project, working on a specific task, or doing all the coding based on your specifications using a web framework / CMS.