Section 12.24

Virtual environment

Definition

A virtual environment is a directory that gives a Python §12.23 interpreter §2.13 its own set of installed packages. The directory holds a configuration file named pyvenv.cfg, a link to or a copy of an existing interpreter, called the base interpreter, and a package directory of its own. An interpreter started from the directory loads third-party packages from that package directory instead of from the base installation; the standard library still comes from the base installation. The standard-library module venv creates virtual environments, and PEP 405 (Python Enhancement Proposal 405) defines them.

The idea

Project A needs version 1 of a package, project B needs version 2 of the same package, and the operating system’s own tools need whichever version the system installed. If all three share one package directory, installing a package for one of them can break the others. A virtual environment gives each project its own package directory, while all of them share one interpreter installation.

The mechanism is a small redirection, not a copy of Python. When the interpreter starts, it looks next to its own executable file for pyvenv.cfg. If the file is there, the interpreter takes that directory as the home of its packages. Everything else stays as it was: the same interpreter code, the same standard library, the same user account, the same files and the same network. Separate bookshelves in one shared house are a fair picture, with one limit: nothing stops a program from taking a book from another shelf.

How it works

Creating. The command python3 -m venv .venv creates a directory named .venv with these parts:

Starting.

  1. The interpreter starts from .venv/bin/python, either because the user typed that path or because the shell found that file first.

  2. The interpreter finds pyvenv.cfg next to its executable or one directory above it. It sets sys.prefix, the installation directory it reports, to the environment directory, and keeps the base installation’s directory in sys.base_prefix.

  3. The site module, which builds the module search path when the interpreter starts, adds the environment’s site-packages directory. Unless include-system-site-packages is true, it leaves out both the base installation’s site-packages directory and the user’s personal package directory.

  4. The standard library is still found in the base installation.

Activating. The command source .venv/bin/activate changes only the current shell. It puts .venv/bin at the front of PATH, the environment variable that lists the directories in which the shell looks for commands; it sets VIRTUAL_ENV; and it changes the prompt. The command deactivate undoes this. Activation is a convenience. Running .venv/bin/python by its path gives the same environment without activation, and a program that runs /usr/bin/python3 by its full path gets the base installation even inside an activated shell.

Installing and checking. pip installs into the package directory of the interpreter that runs it, so .venv/bin/python -m pip install installs into the environment. Inside a virtual environment, sys.prefix differs from sys.base_prefix; that difference is the standard test for whether a program runs in one.

Worked example

The base interpreter is /usr/bin/python3.12, version 3.12.3 of CPython, the reference implementation of Python, on Linux. A user creates the environment /home/alice/proj/.venv and prints three values from the environment’s interpreter: sys.prefix, sys.base_prefix and sys.path, the list of directories searched for modules. The results are:

Only the last of the five entries belongs to the environment. The standard library, including its compiled native modules in lib-dynload, comes from /usr and is shared with every other environment built on this interpreter. A security update of the base installation from 3.12.3 to 3.12.4 therefore changes the interpreter and the standard library of every such environment, although no file inside any environment directory changes.

A second environment, /home/alice/other/.venv, holds version 2.0 of a package P, while the first holds version 1.0. import P loads 1.0 under the first environment’s interpreter and 2.0 under the second’s. Both interpreters still run as the user alice. A script in the first environment can read every file that alice may read, including the second environment’s packages; it can open network connections; and it can start other programs. The separation lies only in which directories each interpreter searches.

An environment variable crosses the boundary just as easily. With PYTHONPATH=/opt/tools set in the shell, the same interpreter puts /opt/tools on its search path right after the current directory, ahead of the standard library and of the environment’s own packages.

Stated precisely

For an interpreter started from a virtual environment, the environment changes exactly these things:

Activation in addition changes PATH, VIRTUAL_ENV and the prompt of one shell.

The environment changes nothing else. In particular it leaves unchanged: the interpreter’s machine code, which is the base interpreter’s, linked or copied; the standard library; the system libraries that the interpreter and native packages load, such as the C library and the system’s cryptography library; environment variables such as PYTHONPATH, which still add directories to the search path; the user account and its rights over files, devices and processes; and access to the network.

Limits and common mistakes

A virtual environment is not a sandbox §2.30. Everything that runs in it, including the installation scripts that some packages run while pip installs them, runs with the user’s full rights.

It does not pin the interpreter. Updating or removing the base installation changes or breaks every environment built on it. To fix the interpreter version, install a separate interpreter for the project, with a tool such as pyenv or uv, or with conda, whose environments contain their own interpreter; or run the project in a container image.

It cannot be moved. Scripts in bin begin with a line that names the environment’s interpreter by its absolute path, so a moved or copied environment breaks. The Python documentation says to recreate an environment in its new place instead of moving or copying it.

It does not record itself. pip freeze lists the installed packages and their versions, including transitive dependencies §10.43, the packages that other packages need. A complete record also needs the interpreter version, the platform, and a cryptographic hash §5.1 of every package file, which pip checks in its --require-hashes mode, so that the same version names always mean the same bytes.

Activation belongs to one shell. A scheduled job, a system service or a child process started with a cleaned environment does not inherit it, so call the environment’s interpreter by its full path in such places.

Questions answered

Does the environment isolate the interpreter binary, operating system, or network?

No. A Python virtual environment isolates only the set of third-party packages that an interpreter loads. It has its own package directory, and its configuration file pyvenv.cfg makes an interpreter started from the environment search that directory instead of the base installation’s.

For those kinds of isolation, use operating-system mechanisms: a separate user account; a container, which gives a process its own view of files, processes and network; or a virtual machine, which runs a separate operating system. To pin the interpreter itself, install a separate interpreter for the project or use a container image.

Related entries: Python (§12.23), Deterministic replay (§10.37), Toolchain (§12.2), Sandbox (§2.30), Reproducible build (§13.8), Transitive dependency (§10.43). Further reading: [43].