pyenv
multiple Python environments
Matias Guijarro, matias.guijarro@free.fr
PUG Grenoble, 22/10/2015
Introduction
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
pyenv
Fork of rbenv by Yuu Yamashita
pyenv
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)
pyenv
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
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
# use pyenv first
$ echo $PATH
~/.pyenv/shims:~/.pyenv/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
pyenv
Choosing the Python version
- PYENV_VERSION environment variable
- .python-version file in application directory
- global ~/.pyenv/python-version file
pyenv
pyenv does not automatically create shims for
new executables after installation
- use "pyenv rehash" command
pyenv installation
matias@kashyyyk:~$ git clone https://github.com/yyuu/pyenv.git ~/.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
matias@kashyyyk:~$
- 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...
-> https://yyuu.github.io/pythons/4281ff86778db65892c05151d5de738d
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)
3.4.3
matias@kashyyyk:~$
- 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
/home/matias/.pyenv/shims/pip
matias@kashyyyk:~$
starting from here, installing a package with 'pip' would target Python 3.4.3, not messing anything with system Python
Useful links
- pyenv-pip-rehash: https://github.com/yyuu/pyenv-pip-rehash
- pyenv-virtualenvwrapper: https://github.com/yyuu/pyenv-virtualenvwrapper
An alternative: https://github.com/saghul/pythonz
pyenv plugins
The END
Questions ?
pyenv
By Matias Guijarro
pyenv
Presentation of pyenv @ Python User Group Grenoble
- 1,953