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 one or two levels above its executable (without
16+ resolving symlinks). The ``pyvenv.cfg `` needs to have at least a ``home `` key
17+ to declare a virtual environment.
1518
1619
1720Runtime detection of virtual environments
1821=========================================
1922
2023At runtime, virtual environments can be identified by virtue of
2124: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).
25+ having a different value from :py:data: `sys.base_prefix ` (the default
26+ filesystem location of the standard library directories).
2427
2528:ref: `venv-explanation ` in the Python standard library documentation for the
2629:py:mod: `venv ` module covers this along with the concept of "activating" a
@@ -29,28 +32,34 @@ step is optional and hence the changes it makes can't be reliably used to
2932detect whether a Python program is running in a virtual environment or not).
3033
3134
32- Declaring installation environments as Python virtual environments
33- ==================================================================
35+ ``pyvenv.cfg ``
36+ ==============
37+
38+ The ``pyvenv.cfg `` has lines in the form ``key = value ``. The following keys
39+ are defined:
3440
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.
41+ * ``home ``: The directory containing the Python executable used to create this
42+ virtual environment.
43+ * ``include-system-site-packages ``: When ``true ``, the ``site `` module will
44+ also add the system site directories to ``sys.path `` after the virtual
45+ environment site directories. If ``false ``, the system site directories are
46+ not included. Defaults to ``true ``.
3947
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.
48+
49+ Declaring installation environments as Python virtual environments
50+ ==================================================================
4551
4652Even in the absence of a ``pyvenv.cfg `` file, any approach (e.g.
4753``sitecustomize.py ``, patching the installed Python runtime) that results in
4854:py:data: `sys.prefix ` and :py:data: `sys.base_prefix ` having different values,
4955while still providing a matching default package installation scheme in
50- :py:mod: `sysconfig `, will be detected and behave as a Python virtual environment.
56+ :py:mod: `sysconfig `, will be detected and behave as a Python virtual
57+ environment.
5158
5259
5360History
5461=======
5562
5663- May 2012: This specification was approved through :pep: `405 `.
64+ - Nov 2026: Removed PEP 405 references and clarified that
65+ ``include-system-site-packages `` defaults to ``true ``.
0 commit comments