diff --git a/source/specifications/virtual-environments.rst b/source/specifications/virtual-environments.rst index ba35d96c6..f366fc477 100644 --- a/source/specifications/virtual-environments.rst +++ b/source/specifications/virtual-environments.rst @@ -59,9 +59,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`.