-
Notifications
You must be signed in to change notification settings - Fork 2
Using pyenv
The pyenv utility allows you to install any and all versions of Python in parallel in your Home directory. It has some overlap with conda, another way to install multiple versions of Python into your home directory. In fact, though, conda (Miniconda/Anaconda) is contained in pyenv, so you can get the best of both systems by installing them withing pyenv.
-
pyenvallows to have multiple versions of Python active at the same time. A lot of projects using [tox][] for testing rely on this. In contrast,condahas a specific Python version for each environment, and while you can create as many environments as you want (each with a different version of Python), only one environment can be active at a time. - Virtual project environments created with
pyenvare much smaller than conda environments -
pyenvinstalls packages withpipexclusively, which means installing either the official pre-compiled wheels, or installing packages from source. Assuming you have all the necessary compilers and dependencies installed, this can avoid the crashes sometimes associated with binary incompatibilities in the (less standardized) conda packages. Current (2018) versions of QuTiP are a particular problem.
-
condais a general package manager, not restricted to Python. You can installgit,pandoc,npmand many other loosely related programs throughconda. This is particularly useful for Jupyter Notebooks / JupyterLab, which have optional dependencies on these programs. - Packages that do not provide wheels and are hard to install from source may provide
condapackages, which would be easier to install. As wheels are becoming more and more common, this is likely to become less relevant in the future. - The Anaconda distribution specifically guarantees compatibility of the packages it includes.
Follow pyenv's installation instructions
-
git clone https://github.com/pyenv/pyenv.git ~/.pyenv -
git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv -
Modify your
~/.bashrcto set up the correct paths:-
near the top of your
.bashrc, putexport PYENV_ROOT=$HOME/.pyenv export PATH=$PYENV_ROOT/bin:$PATH -
near the bottom of your
.bashrc, putif [ -d $PYENV_ROOT ]; then eval "$(pyenv init -)" fi export PATH=$HOME/bin:$PATH
-
-
Log out and in again, check that
pyenv versionsshowssystemas the active environment -
Install all the Python versions that you might want to use
pyenv install 2.7.16 pyenv install 3.4.10 pyenv install 3.5.7 pyenv install 3.6.8 pyenv install 3.7.3 pyenv install 3.8-dev pyenv install miniconda3-latestYou may use newer versions if they are available
-
Activate the python versions in order of precedence
pyenv global 3.7.3 3.6.8 3.5.7 3.4.10 2.7.16 miniconda-latestThis means that going forward, the commands
pythonandpython3will refer to Python 3.7.3,python2will refer to Python 2.7.16,python2.7,python3.4,python3.5,python3.6,python3.7, andpython3.8will refer to the respective Python version, andcondawill allow to create and manage Conda-environments based on Miniconda 3. All of these will be available in your shell at the same time, giving you the greatest flexibility.
At any time, you can now create project-specific virtual environments.
This can be either "named" environments that will be stored e.g. ~/.pyenv/versions or ~/anaconda3/envs, or an environment rooted at a specific path (the recommendation is the .venv subfolder of a particular project-folder).
-
Creating a named environment ("myproject") with
pyenv:pyenv virtualenv 3.6.8 myproject pyenv activate myproject -
Creating a named environment ("myproject") with
conda:conda create -n myproject python=3.6.8 conda activate myproject -
Creating an environment in the
.venvfolder of the current working directory, withpyenv:python3.6 -m venv .venv source .venv/bin/activate -
Creating an environment in the
.venvfolder of the current working directory, withconda:conda create -p .venv python=3.6.8 source .venv/bin/activate
Virtual environments in the .venv subfolder of a project are recommended above named environments: They are nicely isolated and can easily created and managed with a Makefile in the project folder. Using the pyenv method will produce a much smaller .venv folder than the conda method.
This page is part of the cookiecutter-pypackage Wiki