Understanding Python 3 virtual environments different approaches
A bit of light over the usage of virtualenv, pip, venv, pyvenv and pipenv.
Overview
Virtual environments has evolved and many packages and different approaches has appeared in last years to solve the same situation for isolating each project set of dependencies.
We will take a look at the different alternatives and how it evolved to try to make it clear the confusion caused by all the similar project names.
Definitions
First of all, let’s see some useful concepts.
Virtual environment
What is a Virtual environment?
A cooperatively isolated runtime environment that allows Python users and applications to install and upgrade Python distribution packages without interfering with the behaviour of other Python applications running on the same system.
Pip
What is Pip?
A tool for installing Python packages. 1
It is recommended by The Python Packaging Authority (PyPA), a working group that maintains many of the relevant projects in Python packaging. 2
Understanding differences
Following is a list of the recommended ways of handling virtual environments for each Python version to see how they relate to each other:
- Before Python 3.3:
- Ian Bicking’s virtualenv package
- virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool.
- Python 3.3:
- recommends pyvenv script
- a subset of virtualenv was integrated into the standard library under the venv module
- Python 3.6: venv module
- deprecates
pyvenv
to avoid confusion as to what Python interpreter pyvenv is connected to and thus what Python interpreter will be used by the virtual environment 3 - recommends: python3 -m venv /path/to/new/virtual/environment
- In Python
-m <module-name>
searchssys.path
for the named module and execute its contents as the__main__
module.4 - If you work with many virtualenvironments it is helpful to use the same system site-packages and symlinks to save space as: python3 -m venv –system-site-packages –symlinks /path/to/new/virtual/environment
- In Python
- deprecates
--system-site-packages
Give the virtual environment access to the system
site-packages dir.
--symlinks Try to use symlinks rather than copies, when symlinks
are not the default for the platform.
Note that this is a raw summary mostly based in recommendations of the official docs, you may still use any of them.
Nowadays there is also the pipenv project which is a more integral
approach to handle not only virtual environments but also
Pipfiles, and pip. It will create virtualenvs at
~/.local/share/virtualenvs
.
Pipfile and Pipfile.lock are a replacement for the existing standard pip’s requirements.txt file 5 which provides more control over package’s versions.
References
- PEP 405 – Python Virtual Environments: https://www.python.org/dev/peps/pep-0405/
- virtualenv package: https://virtualenv.pypa.io/
- pipenv project: https://github.com/pypa/pipenv
- virtualenvwrapper: https://virtualenvwrapper.readthedocs.io/
- pipenv: https://github.com/pypa/pipenv
- Managing application dependencies: https://packaging.python.org/tutorials/managing-dependencies/
The PyPA recommended tool for installing Python packages. https://pip.pypa.io/en/stable/ ↩︎
https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features ↩︎
- Understanding Python 3 virtual environments different approaches
- Python Projects Isolation Using Virtual EnvironmentsJune 10, 2016
Articles
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.
Powered by SimpleIT Hugo Theme
·