multiple Python environments

Matias Guijarro,

PUG Grenoble, 22/10/2015


So many Pythons...

  • CPython 2.x, CPython 3.x, PyPy, Jython, Anaconda, ...

What if you need more than 1 Python ?

  • Usecase 1: Python 2.7 for legacy projects, Python 3 for new ones
  • Usecase 2: CPython for common projects, PyPy for specific ones
  • Usecase 3: OS-installed Python has to stay untouched in the system, but you need a more recent version

... here comes the need for a standalone Python versions manager


Fork of rbenv by Yuu Yamashita


pyenv lets you easily switch between multiple versions of Python

  • Let you change the global Python version on a per-user basis
  • Provide support for per-project Python versions
  • Allow you to override the Python version with an environment variable

Just Unix shell scripts !

  • Does not depend on Python itself
  • caveat: Doesn't work on Windows (Cygwin is ok)


How it works ?

  • inserts a shims directory in front of PATH

Note: In computer programming, a shim is a small library that transparently intercepts API calls and changes the arguments passed, handles the operation itself, or redirects the operation elsewhere.

  • intercepts Python commands and redirect them to the right executable according to the required Python version
$ echo $PATH

# use pyenv first
$ echo $PATH


Choosing the Python version

  • PYENV_VERSION environment variable
  • .python-version file in application directory
  • global ~/.pyenv/python-version file


pyenv does not automatically create shims for

new executables after installation

  • use "pyenv rehash" command

pyenv installation

matias@kashyyyk:~$ git clone ~/.pyenv
Cloning into '/home/matias/.pyenv'...
Checking connectivity... done.

  • Add pyenv init to the shell to enable shims and autocompletion
matias@kashyyyk:~$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
matias@kashyyyk:~$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
  • Define PYENV_ROOT environment variable and add PYENV_ROOT/bin to PATH to get access to pyenv utility
  • Clone repository
matias@kashyyyk:~$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
  • Reload .bash_profile
matias@kashyyyk:~$ source ~/.bash_profile

pyenv usage

matias@kashyyyk:~$ pyenv install 3.4.3
Downloading Python-3.4.3.tgz...
Installing Python-3.4.3...
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
Installed Python-3.4.3 to /home/matias/.pyenv/versions/3.4.3
matias@kashyyyk:~$ pyenv versions
* system (set by /home/matias/.pyenv/version)
  • List installed versions
  • Install a Python version

pyenv usage

  • Switching between Python versions
matias@kashyyyk:~$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
matias@kashyyyk:~$ pyenv shell 3.4.3
matias@kashyyyk:~$ python
Python 3.4.3 (default, Oct 21 2015, 23:47:55) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
matias@kashyyyk:~$ which pip

starting from here, installing a package with 'pip' would target Python 3.4.3, not messing anything with system Python

Useful links

pyenv plugins













Questions ?


By Matias G


Presentation of pyenv @ Python User Group Grenoble

  • 304
Loading comments...

More from Matias G