@@ -11,15 +11,22 @@ environments with various Python versions) and can have its own independent set
1111of installed Python packages in its site directories, but shares the standard
1212library with the base installed Python.
1313
14+
15+ Detection
16+ =========
17+
18+ File system
19+ -----------
20+
1421A 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
22+ for a :file: ` pyvenv.cfg ` file in the directory containing its executable or in
23+ that directory's parent, without resolving symlinks to the executable. The
24+ :file: ` pyvenv.cfg ` needs to have at least a ``home `` key to declare a virtual
1825environment.
1926
2027
21- Runtime detection of virtual environments
22- =========================================
28+ Runtime
29+ -------
2330
2431At runtime, virtual environments can be identified by virtue of
2532:py:data: `sys.prefix ` (the filesystem location of the running interpreter)
@@ -36,32 +43,76 @@ detect whether a Python program is running in a virtual environment or not).
3643``pyvenv.cfg ``
3744==============
3845
39- The `` pyvenv.cfg `` has lines in the form ``key = value ``. The following keys
40- are defined:
46+ The :file: ` pyvenv.cfg ` has lines in the form ``key = value ``. The following
47+ keys are defined:
4148
4249* ``home ``: The directory containing the Python executable used to create this
4350 virtual environment.
4451* ``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
52+ module will also add the system site directories to :py:data: ` sys.path ` after
53+ the virtual environment site directories. If ``false ``, the system site
4754 directories are not included. It is RECOMMENDED to set it to ``false `` when
4855 creating a virtual environments. The value is not case sensitive.
4956
5057
5158Declaring installation environments as Python virtual environments
5259==================================================================
5360
54- Even in the absence of a `` pyvenv.cfg ` ` file, any approach (e.g.
55- `` sitecustomize.py `` , patching the installed Python runtime) that results in
56- :py:data: `sys.prefix ` and :py:data: `sys.base_prefix ` having different values,
57- while still providing a matching default package installation scheme in
61+ Even in the absence of a :file: ` pyvenv.cfg ` file, any approach (e.g.
62+ :file: ` sitecustomize.py `, patching the installed Python runtime) that results
63+ in :py:data: `sys.prefix ` and :py:data: `sys.base_prefix ` having different
64+ values, while still providing a matching default package installation scheme in
5865:py:mod: `sysconfig `, will be detected and behave as a Python virtual
5966environment.
6067
6168
69+ Discovery
70+ =========
71+
72+ To find a virtual environment, there are a few options.
73+
74+ If a virtual environment has been activated in the terminal (which is an
75+ optional action), you can look for the ``VIRTUAL_ENV `` environment
76+ variable. It will point to the directory containing the virtual environment
77+ that the current terminal is using. Tools should use an activated or otherwise
78+ in-use environment instead of the project default when no previous environment
79+ selection has occurred, or when the tool has no independent justification for
80+ ignoring the user's choice. Tools may override a previous selection if an
81+ environment is detected as activated or in use.
82+
83+ There may be a :file: `.venv ` directory in either the current or parent
84+ directories that is a virtual environment.
85+
86+ There may be a :file: `.venv ` redirect file in the current or parent
87+ directories. The contents of the file represent where to find a virtual
88+ environment elsewhere. The file is encoded using UTF-8 with no BOM marker. The
89+ file contains a single line containing a path; an optional trailing newline of
90+ either ``\n `` or ``\r\n `` may be in the file and is to be ignored (as a
91+ corollary, an empty file or a file with only a newline is invalid). The path in
92+ the file may use ``/ `` regardless of the native path separator of the running
93+ OS. If the path is relative, it is resolved relative to the directory
94+ containing the :file: `.venv ` redirect file based on the file's physical path
95+ (e.g., for a redirect file with a logical path of ``/my/.venv `` and a physical
96+ path of ``/some/project/.venv ``, the file contents of ``../target `` resolves
97+ to ``/some/target ``). The path in a :file: `.venv ` redirect file must point to a
98+ directory containing a virtual environment when the file is written. Tools
99+ may decide how best to handle an invalid :file: `.venv `, but should not simply
100+ ignore it. The tool managing the virtual environment should update the
101+ redirect file when the tool or user wants a different environment to be the
102+ default for other tools.
103+
104+ There is no preference between :file: `.venv ` being a directory or a file; the
105+ closest should be preferred. Tools should look for and support both formats.
106+
107+ The :file: `.venv ` directory or redirect file identifies the project's preferred
108+ virtual environment; it may not be the only environment associated with the
109+ project.
110+
111+
62112History
63113=======
64114
65115- May 2012: This specification was approved through :pep: `405 `.
66116- Nov 2026: Removed PEP 405 references and clarified that
67117 ``include-system-site-packages `` defaults to ``true ``.
118+ - Dec 2026: Update for :pep: `832 `.
0 commit comments