What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc?
There are many different tools that you can use to create isolated Python environments, each with their own benefits and drawbacks.
There are many different tools that you can use to create isolated Python environments, each with their own benefits and drawbacks. Here is a brief overview of some of the most popular ones:
-
venvis a built-in Python module that provides support for creating lightweight, isolated Python environments. It is included in Python 3.3 and later versions. -
pyvenvwas a script included in Python 3.3–3.9 that provided the same functionality as thevenvmodule. It was deprecated in Python 3.6 and removed in Python 3.10. Modern Python versions usepython -m venvinstead. -
pyenvis a third-party tool that allows you to easily install and manage multiple versions of Python on your system. It does not provide support for creating isolated environments, but it can be used in conjunction withvirtualenvorpyenv-virtualenvto achieve this. -
virtualenvis a third-party Python package that provides support for creating isolated Python environments. It is a popular choice for working on multiple projects that might require different package versions. -
virtualenvwrapperis a set of shell scripts that provide additional functionality on top ofvirtualenv. It makes it easier to create and manage multiple virtual environments, and provides some additional features such as the ability to set environment variables for an environment, or to specify which Python interpreter should be used when creating a new environment. -
pipenvis a packaging tool for Python that aims to bring the best of all packaging worlds (bundled, required, and development) to the Python world. It automatically creates and manages a virtual environment for your projects, as well as adds/removes packages from yourPipfileas you install/uninstall packages. It also generates thePipfile.lock, which is used to produce deterministic builds.
Here is an example of how you might use the venv module to create a new isolated Python environment:
Create a new isolated Python environment using the venv module
python3 -m venv myenvThis will create a new directory called myenv that contains a self-contained Python installation, as well as a copy of the pip package manager.
To activate the environment, you can use the following command:
Activate an environment command
source myenv/bin/activateThis will modify your shell's environment variables so that the python and pip commands refer to the isolated Python installation and package manager. You should see the name of your environment in the command prompt, like this:
(myenv) $ commandTo deactivate the environment and return to the global Python installation, you can use the following command:
Deactivate a Python environment
deactivateHere is an example of how you might use virtualenv to create a new isolated Python environment:
Use virtualenv to create a new isolated Python environment
pip install virtualenv
virtualenv myenvThe first command installs the virtualenv package, and the second command creates a new directory called myenv that contains a self-contained Python installation, along with pip and other standard library tools. To activate it, run source myenv/bin/activate (or myenv\Scripts\activate on Windows).
Quick Comparison
| Tool | Type | Primary Use |
|---|---|---|
venv | Built-in | Standard isolated environments (Python 3.3+) |
virtualenv | Third-party | Isolated environments (pre-3.3 or advanced features) |
pyenv | Third-party | Managing multiple Python versions |
virtualenvwrapper | Shell scripts | Managing multiple virtualenv instances |
pipenv | Third-party | Dependency & environment management (Pipfile) |