Text of peps.python.org › pep-0405 as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

PEP 405 – Python Virtual Environments | peps.python.org

PEP 405 – Python Virtual Environments PEP 405 – Python Virtual Environments Author : Carl Meyer <carl at oddbird.net> BDFL-Delegate : Alyssa Coghlan Status : Final Type : Standards Track Topic : Packaging Created : 13-Jun-2011 Python-Version : 3.3 Post-History : 24-Oct-2011, 28-Oct-2011, 06-Mar-2012, 24-May-2012 Resolution : Python-Dev message Table of Contents Abstract Motivation Specification Isolation from system site-packages Creating virtual environments Sysconfig install schemes and user-site Copies versus symlinks Include files API Backwards Compatibility Splitting the meanings of sys.prefix Impact on other Python implementations Reference Implementation Copyright Abstract This PEP proposes to add to Python a mechanism for lightweight “virtual environments” with their own site directories, optionally isolated from system site directories.

Each virtual environment has its own Python binary (allowing creation of environments with various Python versions) and can have its own independent set of installed Python packages in its site directories, but shares the standard library with the base installed Python. Motivation The utility of Python virtual environments has already been well established by the popularity of existing third-party virtual-environment tools, primarily Ian Bicking’s virtualenv .

Virtual environments are already widely used for dependency management and isolation, ease of installing and using Python packages without system-administrator access, and automated testing of Python software across multiple Python versions, among other uses. Existing virtual environment tools suffer from lack of support from the behavior of Python itself. Tools such as rvirtualenv , which do not copy the Python binary into the virtual environment, cannot provide reliable isolation from system site directories.

Virtualenv, which does copy the Python binary, is forced to duplicate much of Python’s site module and manually symlink/copy an ever-changing set of standard-library modules into the virtual environment in order to perform a delicate boot-strapping dance at every startup. (Virtualenv must copy the binary in order to provide isolation, as Python dereferences a symlinked executable before searching for sys.prefix .) The PYTHONHOME environment variable, Python’s only existing built-in solution for virtual environments, requires copying/symlinking the entire standard library into every environment.

Copying the whole standard library is not a lightweight solution, and cross-platform support for symlinks remains inconsistent (even on Windows platforms that do support them, creating them often requires administrator privileges). A virtual environment mechanism integrated with Python and drawing on years of experience with existing third-party tools can lower maintenance, raise reliability, and be more easily available to all Python users.

Specification When the Python binary is executed, it attempts to determine its prefix (which it stores in sys.prefix ), which is then used to find the standard library and other key files, and by the site module to determine the location of the site-package directories. Currently the prefix is found (assuming PYTHONHOME is not set) by first walking up the filesystem tree looking for a marker file ( os.py ) that signifies the presence of the standard library, and if none is found, falling back to the build-time prefix hardcoded in the binary.

This PEP proposes to add a new first step to this search. If a pyvenv.cfg file is found either adjacent to the Python executable or one directory above it (if the executable is a symlink, it is not dereferenced), this file is scanned for lines of the form key = value . If a home key is found, this signifies that the Python binary belongs to a virtual environment, and the value of the home key is the directory containing the Python executable used to create this virtual environment.

In this case, prefix-finding continues as normal using the value of the home key as the effective Python binary location, which finds the prefix of the base installation. sys.base_prefix is set to this value, while sys.prefix is set to the directory containing pyvenv.cfg . (If pyvenv.cfg is not found or does not contain the home key, prefix-finding continues normally, and sys.prefix will be equal to sys.base_prefix .) Also, sys.base_exec_prefix is added, and handled similarly with regard to sys.exec_prefix . ( sys.exec_prefix is the equivalent of sys.prefix , but for platform-specific files; by default it has the same value as sys.prefix .) The site and sysconfig standard-library modules are modified such that the standard library and header files are found relative to sys.base_prefix / sys.base_exec_prefix , while site-package directories (“purelib” and “platlib”, in sysconfig terms) are still found relative to sys.prefix / sys.exec_prefix .

Thus, a Python virtual environment in its simplest form would consist of nothing more than a copy or symlink of the Python binary accompanied by a pyvenv.cfg file and a site-packages directory. Isolation from system site-packages By default, a virtual environment is entirely isolated from the system-level site-packages directories. If the pyvenv.cfg file also contains a key include-system-site-packages with a value of true (not case sensitive), the site module will also add the system site directories to sys.path after the virtual environment site directories.

Thus system-installed packages will still be importable, but a package of the same name installed in the virtual environment will take precedence. PEP 370 user-level site-packages are considered part of the system site-packages for venv purposes: they are not available from an isolated venv, but are available from an include-system-site-packages = true venv. Creating virtual environments This PEP also proposes adding a new venv module to the standard library which implements the creation of virtual environments.

This module can be executed using the -m flag: python3 - m venv / path / to / new / virtual / environment A pyvenv installed script is also provided to make this more convenient: pyvenv / path / to / new / virtual / environment Running this command creates the target directory (creating any parent directories that don’t exist already) and places a pyvenv.cfg file in it with a home key pointing to the Python installation the command was run from.

It also creates a bin/ (or Scripts on Windows) subdirectory containing a copy (or symlink) of the python3 executable, and the pysetup3 script from the packaging standard library module (to facilitate easy installation of packages from PyPI into the new venv). And it creates an (initially empty) lib/pythonX.Y/site-packages (or Lib\site-packages on Windows) subdirectory. If the target directory already exists an error will be raised, unless the --clear option was provided, in which case the target directory will be deleted and virtual environment creation will proceed as usual.

The created pyvenv.cfg file also includes the include-system-site-packages key, set to true if pyvenv is run with the --system-site-packages option, false by default. Multiple paths can be given to pyvenv , in which case an identical venv will be created, according to the given options, at each provided path. The venv module also places “shell activation scripts” for POSIX and Windows systems in the bin or Scripts directory of the venv.

These scripts simply add the virtual environment’s bin (or Scripts ) directory to the front of the user’s shell PATH. This is not strictly necessary for use of a virtual environment (as an explicit path to the venv’s python binary or scripts can just as well be used), but it is convenient. In order to allow pysetup and other Python package managers to install packages into the virtual environment the same way they would install into a normal Python installation, and avoid special-casing virtual environments in sysconfig b

The rest of this page is on the live site.