Python Virtual Environments
I want to read about Python virtual environments because I have been dealing with them recently.
References
Notes
The venev
module supports creating lightweight "virtual environments", each with their own independent set of Python packages installed in their site directories. A virtual environment is created on top of an existing Python installation, known as the virtual environment's "base" Python, and may optionally be isolated from the packages in the base environment, so only those explicitly installed in the virtual environment are available.
A python virtual environment is:
- Used to contain a specific Python interpreter and software libraries and binaries which are needed to support a project.
- Contain a directory, conventionally names
.venv
orvenv
in the project directory - Not checked in source control systems (Git)
- Considered as disposable
- Not considered movable or copyable
Creating Virtual Environments
Virtual environments are created by executing the venv
module:
$ python3 -m venv /path.to/new/virtual/environment
The command has a number of options.
How venvs Work
When a Python interpreter is running from a virtual environment, sys.prefix
and sys.exec_prefix
point to the directories of the virtual environment, whereas sys.base_prefix
and sys.base_exec_prefix
point to those of the base Python used to create the environment.
A virtual environment may be activated
using a script in its binary directory (bin
on POSIX; SCRIPTS
on Windows). This will prepend that directory to your PATH, so that running python
will invoke the environment's Python interpreter and you can run installed scripts without having to use their full path.
Platform | Shell | Command to activate virtual environment |
---|---|---|
POSIX | bash/zsh |
|
fish |
| |
csh/tcsh |
| |
pwsh |
| |
Windows | cmd.exe |
|
PowerShell |
|
If you don't want to activate the virtual environment, then all scripts that want to use the virtual environment have to have a "shebang" line that points to the virtual environment. This means that the script will run with that interpreter regardless of the value of PATH. When a virtual environment has been activated, the VIRTUAL_ENV environment variable is set to the path of the environment.
Virtual Environments and Packages
It may not be possible for one Python installation to meet the requirements of every application - due to different versions of a library needing to be installed. The solution to this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.
The module used to create and manage virtual environments is called venv
. venv
will install the Python version from which the command was run. Activating the virtual environment will change your shell's prompt to show which virtual environment you're using, and modify the environment so that running python
will get you that particular version and installation of Python.
To deactivate a virtual environment:
$ deactivate