Managing multiple python versions without going insane (i.e pyenv)
Introduction
It’s good python practice to segregate your python project environments with virtual environments.
This keeps your dependencies separate between projects which can be vital if they depend on different versions packages/modules and/or python.
The most popular virtual environment tool is virtualenv (or often just venv).
Pyenv has the ability to wrap virtualenv and makes installing and working with multiple versions of python seamless and effortless.
Basically, pyenv intercepts any shell commands you would normally make to python in your path and reroutes them to the appropriate binary or virtual environment you’ve selected (usually declared on a per directory basis).
Installation
OSX
pyenv is painless to install in OSX, just use homebrew
$ brew install pyenv
Linux
The most uptodate instructions are probably best to get from the pyenv github repo itself, but below is the process I went through when installing pyenv on ubuntu.
- Check out the pyenv repo to ~/.pyenv (other places are ok too)
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
- Make the pyenv directory visible to your PATH
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc $ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
- Add pyenv init to your shell to enable shims and autocompletion
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
- Restart shell so path changes take effect
$ exec "$SHELL"
- Install your new clean pyenv-managed version of python!.
For example, for Python 2.7.10:
$ pyenv install 2.7.10
Managing and using installed python versions
To see which version of python you have selected
$ pyenv which python
To change which python version the current directory should use
$ pyenv local <python version number>
#(e.g....2.7.10)
$ pyenv local 2.7.10
Managing Virtual Environments
Being able to install and use an arbitrary version of python is great but sometimes even for the same version you need a special set of dependencies (like when I use GPU-enabled source-compiled tensorflow).
Situations like this make pyenv-virtualenv and pyenv-virtualenvwrapper essential.
-edc