From cb8e898ad982f867b45d86eb7de81dcce21c064e Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Mon, 30 Nov 2026 16:49:04 -0700 Subject: [PATCH 1/4] Add PEP 832 to the virtual environment spec --- .../specifications/virtual-environments.rst | 77 +++++++++++++++---- 1 file changed, 64 insertions(+), 13 deletions(-) diff --git a/source/specifications/virtual-environments.rst b/source/specifications/virtual-environments.rst index ba35d96c6..c1617b934 100644 --- a/source/specifications/virtual-environments.rst +++ b/source/specifications/virtual-environments.rst @@ -11,15 +11,22 @@ environments with various Python versions) and can have its own independent set of installed Python packages in its site directories, but shares the standard library with the base installed Python. + +Detection +========= + +File system +----------- + A Python interpreter detects whether it is in a virtual environment by looking -for a ``pyvenv.cfg`` file in the directory containing its executable or in that -directory's parent, without resolving symlinks to the executable. The -``pyvenv.cfg`` needs to have at least a ``home`` key to declare a virtual +for a :file:`pyvenv.cfg` file in the directory containing its executable or in +that directory's parent, without resolving symlinks to the executable. The +:file:`pyvenv.cfg` needs to have at least a ``home`` key to declare a virtual environment. -Runtime detection of virtual environments -========================================= +Runtime +------- At runtime, virtual environments can be identified by virtue of :py:data:`sys.prefix` (the filesystem location of the running interpreter) @@ -36,14 +43,14 @@ detect whether a Python program is running in a virtual environment or not). ``pyvenv.cfg`` ============== -The ``pyvenv.cfg`` has lines in the form ``key = value``. The following keys -are defined: +The :file:`pyvenv.cfg` has lines in the form ``key = value``. The following +keys are defined: * ``home``: The directory containing the Python executable used to create this virtual environment. * ``include-system-site-packages``: When ``true`` or absent, the ``site`` - module will also add the system site directories to ``sys.path`` after the - virtual environment site directories. If ``false``, the system site + module will also add the system site directories to :py:data:`sys.path` after + the virtual environment site directories. If ``false``, the system site directories are not included. It is RECOMMENDED to set it to ``false`` when creating a virtual environments. The value is not case sensitive. @@ -51,17 +58,61 @@ are defined: Declaring installation environments as Python virtual environments ================================================================== -Even in the absence of a ``pyvenv.cfg`` file, any approach (e.g. -``sitecustomize.py``, patching the installed Python runtime) that results in -:py:data:`sys.prefix` and :py:data:`sys.base_prefix` having different values, -while still providing a matching default package installation scheme in +Even in the absence of a :file:`pyvenv.cfg` file, any approach (e.g. +:file:`sitecustomize.py`, patching the installed Python runtime) that results +in :py:data:`sys.prefix` and :py:data:`sys.base_prefix` having different +values, while still providing a matching default package installation scheme in :py:mod:`sysconfig`, will be detected and behave as a Python virtual environment. +Discovery +========= + +To find a virtual environment, there are a few options. + +If a virtual environment has been activated in the terminal (which is an +optional action), you can look for the ``VIRTUAL_ENV`` environment +variable. It will point to the directory containing the virtual environment +that the current terminal is using. Tools should use an activated or otherwise +in-use environment instead of the project default when no previous environment +selection has occurred, or when the tool has no independent justification for +ignoring the user's choice. Tools may override a previous selection if an +environment is detected as activated or in use. + +There may be a :file:`.venv` directory in either the current or parent +directories that is a virtual environment. + +There may be a :file:`.venv` redirect file in the current or parent +directories. The contents of the file represent where to find a virtual +environment elsewhere. The file is encoded using UTF-8 with no BOM marker. The +file contains a single line containing a path; an optional trailing newline of +either ``\n`` or ``\r\n`` may be in the file and is to be ignored (as a +corollary, an empty file or a file with only a newline is invalid). The path in +the file may use ``/`` regardless of the native path separator of the running +OS. If the path is relative, it is resolved relative to the directory +containing the :file:`.venv` redirect file based on the file's physical path +(e.g., for a redirect file with a logical path of ``/my/.venv`` and a physical +path of ``/some/project/.venv``, the file contents of ``../target`` resolves +to ``/some/target``). The path in a :file:`.venv` redirect file must point to a +directory containing a virtual environment when the file is written. Tools +may decide how best to handle an invalid :file:`.venv`, but should not simply +ignore it. The tool managing the virtual environment should update the +redirect file when the tool or user wants a different environment to be the +default for other tools. + +There is no preference between :file:`.venv` being a directory or a file; the +closest should be preferred. Tools should look for and support both formats. + +The :file:`.venv` directory or redirect file identifies the project's preferred +virtual environment; it may not be the only environment associated with the +project. + + History ======= - May 2012: This specification was approved through :pep:`405`. - Nov 2026: Removed PEP 405 references and clarified that ``include-system-site-packages`` defaults to ``true``. +- Dec 2026: Update for :pep:`832`. From 9dacd111768102d0c50fa623cd89709714b4d249 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Wed, 2 Dec 2026 16:36:05 -0700 Subject: [PATCH 2/4] Reset the file --- .../specifications/virtual-environments.rst | 77 ++++--------------- 1 file changed, 13 insertions(+), 64 deletions(-) diff --git a/source/specifications/virtual-environments.rst b/source/specifications/virtual-environments.rst index c1617b934..ba35d96c6 100644 --- a/source/specifications/virtual-environments.rst +++ b/source/specifications/virtual-environments.rst @@ -11,22 +11,15 @@ environments with various Python versions) and can have its own independent set of installed Python packages in its site directories, but shares the standard library with the base installed Python. - -Detection -========= - -File system ------------ - A Python interpreter detects whether it is in a virtual environment by looking -for a :file:`pyvenv.cfg` file in the directory containing its executable or in -that directory's parent, without resolving symlinks to the executable. The -:file:`pyvenv.cfg` needs to have at least a ``home`` key to declare a virtual +for a ``pyvenv.cfg`` file in the directory containing its executable or in that +directory's parent, without resolving symlinks to the executable. The +``pyvenv.cfg`` needs to have at least a ``home`` key to declare a virtual environment. -Runtime -------- +Runtime detection of virtual environments +========================================= At runtime, virtual environments can be identified by virtue of :py:data:`sys.prefix` (the filesystem location of the running interpreter) @@ -43,14 +36,14 @@ detect whether a Python program is running in a virtual environment or not). ``pyvenv.cfg`` ============== -The :file:`pyvenv.cfg` has lines in the form ``key = value``. The following -keys are defined: +The ``pyvenv.cfg`` has lines in the form ``key = value``. The following keys +are defined: * ``home``: The directory containing the Python executable used to create this virtual environment. * ``include-system-site-packages``: When ``true`` or absent, the ``site`` - module will also add the system site directories to :py:data:`sys.path` after - the virtual environment site directories. If ``false``, the system site + module will also add the system site directories to ``sys.path`` after the + virtual environment site directories. If ``false``, the system site directories are not included. It is RECOMMENDED to set it to ``false`` when creating a virtual environments. The value is not case sensitive. @@ -58,61 +51,17 @@ keys are defined: Declaring installation environments as Python virtual environments ================================================================== -Even in the absence of a :file:`pyvenv.cfg` file, any approach (e.g. -:file:`sitecustomize.py`, patching the installed Python runtime) that results -in :py:data:`sys.prefix` and :py:data:`sys.base_prefix` having different -values, while still providing a matching default package installation scheme in +Even in the absence of a ``pyvenv.cfg`` file, any approach (e.g. +``sitecustomize.py``, patching the installed Python runtime) that results in +:py:data:`sys.prefix` and :py:data:`sys.base_prefix` having different values, +while still providing a matching default package installation scheme in :py:mod:`sysconfig`, will be detected and behave as a Python virtual environment. -Discovery -========= - -To find a virtual environment, there are a few options. - -If a virtual environment has been activated in the terminal (which is an -optional action), you can look for the ``VIRTUAL_ENV`` environment -variable. It will point to the directory containing the virtual environment -that the current terminal is using. Tools should use an activated or otherwise -in-use environment instead of the project default when no previous environment -selection has occurred, or when the tool has no independent justification for -ignoring the user's choice. Tools may override a previous selection if an -environment is detected as activated or in use. - -There may be a :file:`.venv` directory in either the current or parent -directories that is a virtual environment. - -There may be a :file:`.venv` redirect file in the current or parent -directories. The contents of the file represent where to find a virtual -environment elsewhere. The file is encoded using UTF-8 with no BOM marker. The -file contains a single line containing a path; an optional trailing newline of -either ``\n`` or ``\r\n`` may be in the file and is to be ignored (as a -corollary, an empty file or a file with only a newline is invalid). The path in -the file may use ``/`` regardless of the native path separator of the running -OS. If the path is relative, it is resolved relative to the directory -containing the :file:`.venv` redirect file based on the file's physical path -(e.g., for a redirect file with a logical path of ``/my/.venv`` and a physical -path of ``/some/project/.venv``, the file contents of ``../target`` resolves -to ``/some/target``). The path in a :file:`.venv` redirect file must point to a -directory containing a virtual environment when the file is written. Tools -may decide how best to handle an invalid :file:`.venv`, but should not simply -ignore it. The tool managing the virtual environment should update the -redirect file when the tool or user wants a different environment to be the -default for other tools. - -There is no preference between :file:`.venv` being a directory or a file; the -closest should be preferred. Tools should look for and support both formats. - -The :file:`.venv` directory or redirect file identifies the project's preferred -virtual environment; it may not be the only environment associated with the -project. - - History ======= - May 2012: This specification was approved through :pep:`405`. - Nov 2026: Removed PEP 405 references and clarified that ``include-system-site-packages`` defaults to ``true``. -- Dec 2026: Update for :pep:`832`. From 537213b36b736ba0fd4909729785f9f8436372b0 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Wed, 2 Dec 2026 16:36:29 -0700 Subject: [PATCH 3/4] Copy over the Specification section from PEP 832 --- .../specifications/virtual-environments.rst | 78 ++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/source/specifications/virtual-environments.rst b/source/specifications/virtual-environments.rst index ba35d96c6..0b9007723 100644 --- a/source/specifications/virtual-environments.rst +++ b/source/specifications/virtual-environments.rst @@ -33,6 +33,8 @@ step is optional and hence the changes it makes can't be reliably used to detect whether a Python program is running in a virtual environment or not). +.. _pyvenv-cfg-spec: + ``pyvenv.cfg`` ============== @@ -59,9 +61,83 @@ while still providing a matching default package installation scheme in environment. +Discovery +========= + +This specification does not define what the "root of a project" means, but the +assumption is that it is the directory one would open in their code editor to +work on a project's code. This could be the directory where the project's +:ref:`pyproject.toml ` lives, or potentially the top +directory of a monorepo. + +The virtual environment for a project MAY be a directory named :file:`.venv` +(i.e., :file:`.venv/pyvenv.cfg` will exist within the directory, which can be +used to detect the existence of a virtual environment) in the root of the +project. This specification makes no judgment whether :file:`.venv` is a +physical or logical path to a directory containing a virtual environment, nor +whether logical paths should be resolved to their physical equivalent before +use. + +Instead of a directory containing a virtual environment, the root of a project +MAY have a :file:`.venv` redirect file (which, just like a :file:`.venv` +directory, can be a logical or physical path). This file acts as a pointer to +where the (current) virtual environment to be used for the project is located. +This virtual environment may not be the only environment associated with the +project, but it is considered the preferred virtual environment to use +*at the moment* by other tools. The tool managing the virtual environment +SHOULD update any :file:`.venv` redirect file as appropriate when the tool or +the user desires a different virtual environment to be used by default by other +tools. + +Tools looking for a virtual environment SHOULD look for either a :file:`.venv` +directory or a :file:`.venv` redirect file. IF a tool chooses to search parent +directories, THEN the closest :file:`.venv` in either form SHOULD be selected. + +A :file:`.venv` redirect file MUST be encoded using UTF-8 (the UTF-8 BOM MUST +NOT be used). The file MUST only contain a single line. A trailing newline of +either ``\n`` or ``\r\n`` is allowed and MUST be ignored. An empty file or one +that only contains a newline is considered invalid. + +The line in the :file:`.venv` redirect file MUST point to a directory +containing a virtual environment at the time of being written. The path MAY use +POSIX path separators-- ``/`` --regardless of what the operating system's +native path separator is. The path MAY be relative; IF the path is relative +THEN it MUST be resolved relative to the directory containing the :file:`.venv` +redirect file based on the file's physical path (e.g., for a redirect file with +a logical path of :file:`/my/.venv` and a physical path of +:file:`/some/project/.venv`, the file contents of :file:`../target` resolves to +:file:`/some/target`). + +With regard to committing a :file:`.venv` redirect file to version control, it +MAY be done when the location of the virtual environment is considered static +for a project once it is set up. For instance, some projects that use +:doc:`tox ` have a "dev" environment defined in their configuration +that ends up at ``.tox/dev``. Setting a :file:`.venv` redirect file to point to +that virtual environment and checking in the file is reasonable. The same goes +for a project that is only worked on within a container where the location of +the virtual environment is controlled and thus static on the file system. The +guidance of NOT committing your actual virtual environment to version control +is unchanged by this specification. + +IF a tool can detect that an environment is already in use (e.g., the +``VIRTUAL_ENV`` environment variable is set), THEN tools SHOULD respect the +user's choice and use the activated/in-use environment over the default +environment when no previous environment selection has occurred or the tool +doesn't have its own justification for ignoring the user's supposed choice. +Tools MAY choose to override even a previous environment selection if an +environment is detected as activated/in use. + +This specification is choosing NOT to take a position on what to do if +:file:`.venv` is invalid (whether it's a directory or a file). It is up to the +tool encountering the invalid directory/file to decide how best to handle the +situation. The expectation, though, is the invalid directory/file will not +simply be ignored. + + History ======= - May 2012: This specification was approved through :pep:`405`. -- Nov 2026: Removed PEP 405 references and clarified that +- Nov 2026: Removed :pep:`405` references and clarified that ``include-system-site-packages`` defaults to ``true``. +- Dec 2026: Added :pep:`832`. From a322dc39557c3c17cdd304af39b0407e3e625cec Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Wed, 2 Dec 2026 16:38:49 -0700 Subject: [PATCH 4/4] Drop a link target I didn't end up using --- source/specifications/virtual-environments.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/source/specifications/virtual-environments.rst b/source/specifications/virtual-environments.rst index 0b9007723..f366fc477 100644 --- a/source/specifications/virtual-environments.rst +++ b/source/specifications/virtual-environments.rst @@ -33,8 +33,6 @@ step is optional and hence the changes it makes can't be reliably used to detect whether a Python program is running in a virtual environment or not). -.. _pyvenv-cfg-spec: - ``pyvenv.cfg`` ==============