Python 3.3 includes in its standard library the new package venv. What does it do, and how does it differ from all the other packages that match the regex (py)?(v|virtual|pip)?env?
Advertisement
Answer
This is my personal recommendation for beginners: start by learning virtualenv and pip, tools which work with both Python 2 and 3 and in a variety of situations, and pick up other tools once you start needing them.
Now on to answer the question: what is the difference between these similarly named things: venv, virtualenv, etc?
PyPI packages not in the standard library:
- virtualenvis a very popular tool that creates isolated Python environments for Python libraries. If you’re not familiar with this tool, I highly recommend learning it, as it is a very useful tool.- It works by installing a bunch of files in a directory (eg: - env/), and then modifying the- PATHenvironment variable to prefix it with a custom- bindirectory (eg:- env/bin/). An exact copy of the- pythonor- python3binary is placed in this directory, but Python is programmed to look for libraries relative to its path first, in the environment directory. It’s not part of Python’s standard library, but is officially blessed by the PyPA (Python Packaging Authority). Once activated, you can install packages in the virtual environment using- pip.
- pyenvis used to isolate Python versions. For example, you may want to test your code against Python 2.7, 3.6, 3.7 and 3.8, so you’ll need a way to switch between them. Once activated, it prefixes the- PATHenvironment variable with- ~/.pyenv/shims, where there are special files matching the Python commands (- python,- pip). These are not copies of the Python-shipped commands; they are special scripts that decide on the fly which version of Python to run based on the- PYENV_VERSIONenvironment variable, or the- .python-versionfile, or the- ~/.pyenv/versionfile.- pyenvalso makes the process of downloading and installing multiple Python versions easier, using the command- pyenv install.
- pyenv-virtualenvis a plugin for- pyenvby the same author as- pyenv, to allow you to use- pyenvand- virtualenvat the same time conveniently. However, if you’re using Python 3.3 or later,- pyenv-virtualenvwill try to run- python -m venvif it is available, instead of- virtualenv. You can use- virtualenvand- pyenvtogether without- pyenv-virtualenv, if you don’t want the convenience features.
- virtualenvwrapperis a set of extensions to- virtualenv(see docs). It gives you commands like- mkvirtualenv,- lssitepackages, and especially- workonfor switching between different- virtualenvdirectories. This tool is especially useful if you want multiple- virtualenvdirectories.
- pyenv-virtualenvwrapperis a plugin for- pyenvby the same author as- pyenv, to conveniently integrate- virtualenvwrapperinto- pyenv.
- pipenvaims to combine- Pipfile,- pipand- virtualenvinto one command on the command-line. The- virtualenvdirectory typically gets placed in- ~/.local/share/virtualenvs/XXX, with- XXXbeing a hash of the path of the project directory. This is different from- virtualenv, where the directory is typically in the current working directory.- pipenvis meant to be used when developing Python applications (as opposed to libraries). There are alternatives to- pipenv, such as- poetry, which I won’t list here since this question is only about the packages that are similarly named.
Standard library:
- pyvenv(not to be confused with- pyenvin the previous section) is a script shipped with Python 3.3 to 3.7. It was removed from Python 3.8 as it had problems (not to mention the confusing name). Running- python3 -m venvhas exactly the same effect as- pyvenv.
- venvis a package shipped with Python 3, which you can run using- python3 -m venv(although for some reason some distros separate it out into a separate distro package, such as- python3-venvon Ubuntu/Debian). It serves the same purpose as- virtualenv, but only has a subset of its features (see a comparison here).- virtualenvcontinues to be more popular than- venv, especially since the former supports both Python 2 and 3.
