Python Virtual Environments
Python dependencies can get complicated quickly. They can also tangle together and trample on each other. Developer best practice is to start each new project in its own virtual environment. To prevent things becoming too simple there are several, usually with quite similar names. Check out one 2019 comparison. This is a confusing topic that doesn’t seem close to consensus.
The one thing all experienced developers agree on don’t mess with your system Python. For Linux/Mac users that can mean you suddenly have no working OS and need to reinstall on a clean disk partition.
virtualenv and virtualenv-wrapper
Familiar to Python 2 programmers (current and former) but largely obsolete since Python 3.3. I used it back in the day, but found it confusing (especially file locations).
venv
Built into all recent versions of Python and supported by the Python core team.
Create a new environment with python3 -m venv /path/to/new/virtual/environment
Website: https://docs.python.org/3/library/venv.html
pyenv and pyenv-virtualenv
pyenv is a Python version manager (ported from Ruby’s rbenv). Ignore the confusingly-named pyvenv, which is now deprecated.
pyenv-virtualenv is a pyenv plugin to support virtualenvs: originally using virtualenv but now it defaults to venv if available. Not essential, but many developers find it makes life easier.
Websites:
Blog posts:
- Managing virtual environments with pyenv
- pyenv Tutorial
- Managing Multiple Python Versions With pyenv
- How to manage multiple Python versions and virtual environments
Pipenv
Sort of a higher-level replacement for pip. Opinion in the Python developer community seems divided (sometimes fiercely) on pipenv vs venv/pyenv. Bundled with and supported by ActiveState Python.
Blog posts:
conda
Most familiar to scientists. Windows users really need this to manage the NumPy/SciPy ecosystem, which can otherwise be a nightmare. Linux/Mac users may have more choices.
conda is two things in one: a package installer (sort of like pip but separate) and a virtual environment manager (sort of like venv but separate).
conda is great most of the time but can be a bit brittle: when it goes wrong without warning it can be a nightmare to fix. Said with feeling - I’ve been there. Some developers suggest Anaconda/Miniconda should only be installed within virtual environments and never at a system level, turning the conda env
idea on its head..
Conclusions
Well, it’s complicated…
One very detailed view: A guide to Python virtual environments. Long, opinionated, well worth reading
I largely followed a cheatsheet from the same author and made a Linux version