@@ -33,6 +33,8 @@ step is optional and hence the changes it makes can't be reliably used to
3333detect whether a Python program is running in a virtual environment or not).
3434
3535
36+ .. _pyvenv-cfg-spec :
37+
3638``pyvenv.cfg ``
3739==============
3840
@@ -59,9 +61,83 @@ while still providing a matching default package installation scheme in
5961environment.
6062
6163
64+ Discovery
65+ =========
66+
67+ This specification does not define what the "root of a project" means, but the
68+ assumption is that it is the directory one would open in their code editor to
69+ work on a project's code. This could be the directory where the project's
70+ :ref: `pyproject.toml <pyproject-toml-spec >` lives, or potentially the top
71+ directory of a monorepo.
72+
73+ The virtual environment for a project MAY be a directory named :file: `.venv `
74+ (i.e., :file: `.venv/pyvenv.cfg ` will exist within the directory, which can be
75+ used to detect the existence of a virtual environment) in the root of the
76+ project. This specification makes no judgment whether :file: `.venv ` is a
77+ physical or logical path to a directory containing a virtual environment, nor
78+ whether logical paths should be resolved to their physical equivalent before
79+ use.
80+
81+ Instead of a directory containing a virtual environment, the root of a project
82+ MAY have a :file: `.venv ` redirect file (which, just like a :file: `.venv `
83+ directory, can be a logical or physical path). This file acts as a pointer to
84+ where the (current) virtual environment to be used for the project is located.
85+ This virtual environment may not be the only environment associated with the
86+ project, but it is considered the preferred virtual environment to use
87+ *at the moment * by other tools. The tool managing the virtual environment
88+ SHOULD update any :file: `.venv ` redirect file as appropriate when the tool or
89+ the user desires a different virtual environment to be used by default by other
90+ tools.
91+
92+ Tools looking for a virtual environment SHOULD look for either a :file: `.venv `
93+ directory or a :file: `.venv ` redirect file. IF a tool chooses to search parent
94+ directories, THEN the closest :file: `.venv ` in either form SHOULD be selected.
95+
96+ A :file: `.venv ` redirect file MUST be encoded using UTF-8 (the UTF-8 BOM MUST
97+ NOT be used). The file MUST only contain a single line. A trailing newline of
98+ either ``\n `` or ``\r\n `` is allowed and MUST be ignored. An empty file or one
99+ that only contains a newline is considered invalid.
100+
101+ The line in the :file: `.venv ` redirect file MUST point to a directory
102+ containing a virtual environment at the time of being written. The path MAY use
103+ POSIX path separators-- ``/ `` --regardless of what the operating system's
104+ native path separator is. The path MAY be relative; IF the path is relative
105+ THEN it MUST be resolved relative to the directory containing the :file: `.venv `
106+ redirect file based on the file's physical path (e.g., for a redirect file with
107+ a logical path of :file: `/my/.venv ` and a physical path of
108+ :file: `/some/project/.venv `, the file contents of :file: `../target ` resolves to
109+ :file: `/some/target `).
110+
111+ With regard to committing a :file: `.venv ` redirect file to version control, it
112+ MAY be done when the location of the virtual environment is considered static
113+ for a project once it is set up. For instance, some projects that use
114+ :doc: `tox <tox:index >` have a "dev" environment defined in their configuration
115+ that ends up at ``.tox/dev ``. Setting a :file: `.venv ` redirect file to point to
116+ that virtual environment and checking in the file is reasonable. The same goes
117+ for a project that is only worked on within a container where the location of
118+ the virtual environment is controlled and thus static on the file system. The
119+ guidance of NOT committing your actual virtual environment to version control
120+ is unchanged by this specification.
121+
122+ IF a tool can detect that an environment is already in use (e.g., the
123+ ``VIRTUAL_ENV `` environment variable is set), THEN tools SHOULD respect the
124+ user's choice and use the activated/in-use environment over the default
125+ environment when no previous environment selection has occurred or the tool
126+ doesn't have its own justification for ignoring the user's supposed choice.
127+ Tools MAY choose to override even a previous environment selection if an
128+ environment is detected as activated/in use.
129+
130+ This specification is choosing NOT to take a position on what to do if
131+ :file: `.venv ` is invalid (whether it's a directory or a file). It is up to the
132+ tool encountering the invalid directory/file to decide how best to handle the
133+ situation. The expectation, though, is the invalid directory/file will not
134+ simply be ignored.
135+
136+
62137History
63138=======
64139
65140- May 2012: This specification was approved through :pep: `405 `.
66- - Nov 2026: Removed PEP 405 references and clarified that
141+ - Nov 2026: Removed :pep: ` 405 ` references and clarified that
67142 ``include-system-site-packages `` defaults to ``true ``.
143+ - Dec 2026: Added :pep: `832 `.
0 commit comments