Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 64 additions & 13 deletions source/specifications/virtual-environments.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -36,32 +43,76 @@ 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.


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`.
Loading