virtual environment

term

programming

Kept in this browser · My desk

A directory with its own set of Python packages, built on top of a base Python installation and isolated by default from the base's packages and from other environments.

python -m venv .venv creates one: a pyvenv.cfg file naming the base interpreter, a bin directory (Scripts on Windows) and its own site-packages, with pip bootstrapped inside. A script can tell it runs in one because sys.prefix differs from sys.base_prefix. The documentation calls an environment disposable and not movable or copyable, since installed commands point at its interpreter by absolute path. Activation is optional: calling the environment's python by full path is enough, which is the form for cron jobs and services.

A virtual environment answers a packaging conflict, not a security one. The Python tutorial states the case: if application A needs version 1.0 of a module and application B needs version 2.0, one installation cannot satisfy both, so each application gets a directory tree with its own packages. PEP (Python Enhancement Proposal) 405, which put the mechanism into the language, defines it as a lightweight environment with its own site directories and its own Python binary that shares the standard library with the base installation. That last clause says what it is not. virtualenv's documentation puts it plainly: virtual environments are not self-contained, they are lightweight shells that borrow most of their content from the system Python and hold only what is needed to redirect its behaviour. Only the set of importable packages is separated. Nothing in the mechanism, a configuration file and a search path, confines what the code can do, so an environment is neither a container nor a virtual machine.

The mechanism is a change to how the interpreter finds its prefix at startup. Before looking for the standard library, Python checks for a file named pyvenv.cfg next to its executable or one directory above it, without dereferencing a symbolic link. If the file has a home key, the binary belongs to a virtual environment, and home is the directory of the Python that created it. The interpreter then sets sys.base_prefix to the base installation and sys.prefix to the directory that holds pyvenv.cfg; the standard library and the header files are found relative to the first, the site-packages directories relative to the second. PEP 405 concludes that an environment in its simplest form is nothing more than a copy or symlink of the Python binary, a pyvenv.cfg file and a site-packages directory. Since Python 3.14 the prefixes are set during path initialization and no longer by the site module, so they hold even when the interpreter is started with -S.

Isolation is the default, and one key relaxes it. Created with --system-site-packages, an environment has include-system-site-packages set to true in pyvenv.cfg, and the site module adds the system site directories to sys.path after the environment's own: system packages can be imported, but a package of the same name installed in the environment takes precedence. Without it, the per-user site-packages directory is not searched either, because PEP 405 counts user-level packages as part of the system's. This is the first thing to check when an environment imports something that was never installed into it.

The venv module, in the standard library since Python 3.3, builds the directory when run as python -m venv followed by a path: the configuration file, a bin directory holding a copy or symlink of the interpreter, and lib/pythonX.Y/site-packages (on Windows, Scripts, and site-packages under Lib). Unless --without-pip is given, ensurepip bootstraps pip into it. The other options are few: --symlinks and --copies choose how the interpreter is placed, --clear empties an existing directory first, --upgrade adapts the environment after Python has been upgraded in place, --upgrade-deps updates pip to the latest release on the Python Package Index (PyPI), and --prompt sets the name shown in the shell. Since 3.13 venv also writes a .gitignore file, because an environment is not meant to be checked into source control. On Windows the documentation advises against symlinks, since double-clicking python.exe resolves the link and ignores the environment. The tutorial explains the usual name, .venv: it stays hidden in a shell listing, says what the directory is for, and does not clash with the .env files in which some tools keep environment variables.

Activation changes the shell, not the environment. The activate script for bash and zsh (activate.fish, activate.csh and Activate.ps1 are its siblings, with activate.bat for cmd.exe) prepends the environment's bin directory to PATH and sets the VIRTUAL_ENV variable; deactivate undoes it. On Windows, PowerShell may refuse to run Activate.ps1 until the user's execution policy allows it, and the documentation gives the command: Set-ExecutionPolicy with RemoteSigned and the CurrentUser scope. Because nothing requires activation, VIRTUAL_ENV cannot be relied on to tell whether an environment is in use; comparing the two prefixes is the test. Commands installed into an environment run without activation because their first line, the shebang, names the environment's interpreter, and that line is why the documentation calls environments inherently non-portable: after moving or renaming the directory, or a parent of it, the environment has to be recreated, typically from a requirements file.

The idea is older than the module. PEP 405, written by Carl Meyer and created on 13 June 2011, credits the popularity of third-party tools, primarily Ian Bicking's virtualenv, and explains why they needed help from the interpreter: virtualenv had to copy the Python binary, duplicate much of the site module and link an ever-changing set of standard-library modules into each environment, while the only built-in alternative, the PYTHONHOME variable, meant copying or linking the entire standard library. The PEP is Final and shipped in Python 3.3 together with a pyvenv script, the recommended tool for 3.3 and 3.4, deprecated in 3.6 and removed in 3.8 in favour of running the module directly. Python 3.4 began installing pip by default, 3.9 added --upgrade-deps, and since 3.12 setuptools is no longer installed alongside pip.

virtualenv did not go away. Its documentation, with a copyright line that starts in 2007, says a subset of it was integrated into the standard library as venv, and compares the two with uv, a newer tool written in Rust. By its account venv is the slowest of the three because it spawns pip as a subprocess to seed the environment, has no plugin system and creates environments only for the Python version it runs under; virtualenv caches its seed packages, can target any installed interpreter and offers the programmatic interface that tox, poetry and pipx build on; uv venv is the fastest and does not install pip into the environment by default. uv's own documentation adds that it creates .venv when no path is given, can download a requested Python version and, unlike pip, requires a virtual environment by default. pipx applies the same idea to command-line applications: a separate environment for each application, with its commands exposed on PATH.

Linux distributions turned the convention into a rule. PEP 668, created on 18 May 2021 and approved in June 2022 as the Externally Managed Environments specification, lets a distributor place a marker file named EXTERNALLY-MANAGED in the standard-library directory. Before installing, a tool such as pip checks two things: whether it is running outside a virtual environment (sys.prefix equal to sys.base_prefix) and whether the marker exists. If both are true it exits with an error, using the message the file supplies, unless the user passes an override that the PEP wants to sound risky, --break-system-packages. pip has read the marker since release 23.0 of 30 January 2023 and gained the override in 23.0.1 on 17 February 2023. Debian 12 (bookworm) marks its python3.11 and pypy3 interpreters this way, and its release notes name the alternatives: pipx for an application, and for a library a virtual environment made with the venv module, which Debian ships in the python3-venv package, or with virtualenv.

Most trouble follows from three properties. An environment is tied to its base: pyvenv.cfg points at one Python installation, which is why venv has an --upgrade option for the case where that Python is upgraded in place. It carries its own copy of pip, bootstrapped when the environment was created, so pip is updated per environment, with --upgrade-deps or with python -m pip install --upgrade pip run by the environment's interpreter. And it is addressed by path: pip's user guide recommends python -m pip because that form runs pip with exactly the interpreter named, and a scheduled job or a service should name the environment's interpreter in full. pip can enforce the habit itself: with --require-virtualenv, or the PIP_REQUIRE_VIRTUALENV environment variable, it exits with an error when run outside a virtual environment. The documentation's closing advice is the rule for any automation host: always keep a simple means of recreating an environment, such as a requirements file.

Also known as: venv, .venv, Python virtual environment

Sources

← Back to V Back to the glossary