11
22.. _virtual-environments :
33
4- ===========================
5- Python Virtual Environments
6- ===========================
4+ ====================
5+ Virtual Environments
6+ ====================
77
8- For Python 3.3 and later versions, :pep: `405 ` introduced interpreter level support
9- for the concept of "Python Virtual Environments". Each virtual environment has
10- its own Python binary (allowing creation of environments with various Python
11- versions) and can have its own independent set of installed Python packages in
12- its site directories, but shares the standard library with the base installed
13- Python. While the concept of virtual environments existed prior to this update,
14- there was no previously standardised mechanism for declaring or discovering them.
8+ A virtual environment isolates a Python interpreter and its packages. The
9+ virtual environment has its own Python binary (allowing creation of
10+ environments with various Python versions) and can have its own independent set
11+ of installed Python packages in its site directories, but shares the standard
12+ library with the base installed Python.
13+
14+ A Python interpreter detects whether it is in a virtual environment by looking
15+ for a ``pyvenv.cfg `` file in the directory containing its executable or in that
16+ directory's parent, without resolving symlinks to the executable. The
17+ ``pyvenv.cfg `` needs to have at least a ``home `` key to declare a virtual
18+ environment.
1519
1620
1721Runtime detection of virtual environments
1822=========================================
1923
2024At runtime, virtual environments can be identified by virtue of
2125:py:data: `sys.prefix ` (the filesystem location of the running interpreter)
22- having a different value from :py:data: `sys.base_prefix ` (the default filesystem
23- location of the standard library directories).
26+ having a different value from :py:data: `sys.base_prefix ` (the default
27+ filesystem location of the standard library directories).
2428
2529:ref: `venv-explanation ` in the Python standard library documentation for the
2630:py:mod: `venv ` module covers this along with the concept of "activating" a
@@ -29,28 +33,35 @@ step is optional and hence the changes it makes can't be reliably used to
2933detect whether a Python program is running in a virtual environment or not).
3034
3135
32- Declaring installation environments as Python virtual environments
33- ==================================================================
36+ ``pyvenv.cfg ``
37+ ==============
38+
39+ The ``pyvenv.cfg `` has lines in the form ``key = value ``. The following keys
40+ are defined:
3441
35- As described in :pep: `405 `, a Python virtual environment in its simplest form
36- consists of nothing more than a copy or symlink of the Python binary accompanied
37- by a ``site-packages `` directory and a ``pyvenv.cfg `` file with a ``home `` key
38- that indicates where to find the Python standard library modules.
42+ * ``home ``: The directory containing the Python executable used to create this
43+ virtual environment.
44+ * ``include-system-site-packages ``: When ``true `` or absent, the ``site ``
45+ module will also add the system site directories to ``sys.path `` after the
46+ virtual environment site directories. If ``false ``, the system site
47+ directories are not included. It is RECOMMENDED to set it to ``false `` when
48+ creating a virtual environments. The value is not case sensitive.
3949
40- While designed to meet the needs of the standard :py:mod: `venv ` module, this
41- split installation and ``pyvenv.cfg `` file approach can be used by *any *
42- Python installation provider that desires Python-specific tools to be aware that
43- they are already operating in a virtual environment and no further environment
44- nesting is required or desired.
50+
51+ Declaring installation environments as Python virtual environments
52+ ==================================================================
4553
4654Even in the absence of a ``pyvenv.cfg `` file, any approach (e.g.
4755``sitecustomize.py ``, patching the installed Python runtime) that results in
4856:py:data: `sys.prefix ` and :py:data: `sys.base_prefix ` having different values,
4957while still providing a matching default package installation scheme in
50- :py:mod: `sysconfig `, will be detected and behave as a Python virtual environment.
58+ :py:mod: `sysconfig `, will be detected and behave as a Python virtual
59+ environment.
5160
5261
5362History
5463=======
5564
5665- May 2012: This specification was approved through :pep: `405 `.
66+ - Nov 2026: Removed PEP 405 references and clarified that
67+ ``include-system-site-packages `` defaults to ``true ``.
0 commit comments