Skip to content

Commit f98ec67

Browse files
committed
Update documentation
Signed-off-by: Filipe Laíns <[email protected]>
1 parent 3c24f44 commit f98ec67

File tree

5 files changed

+112
-41
lines changed

5 files changed

+112
-41
lines changed

Doc/c-api/init_config.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,9 +1588,22 @@ If a ``._pth`` file is present:
15881588
* Set :c:member:`~PyConfig.site_import` to ``0``.
15891589
* Set :c:member:`~PyConfig.safe_path` to ``1``.
15901590
1591+
If :c:member:`~PyConfig.home` is not set and a ``pyvenv.cfg`` file is present in
1592+
the same directory as :c:member:`~PyConfig.executable`, or its parent,
1593+
:c:member:`~PyConfig.prefix` and :c:member:`~PyConfig.exec_prefix` are set that
1594+
location. When this happens, :c:member:`~PyConfig.base_prefix` and
1595+
:c:member:`~PyConfig.base_exec_prefix` still keep their value, pointing to the
1596+
base installation. See :ref:`sys-path-init-virtual-environments` for more
1597+
information.
1598+
15911599
The ``__PYVENV_LAUNCHER__`` environment variable is used to set
15921600
:c:member:`PyConfig.base_executable`.
15931601
1602+
.. versionchanged:: 3.14
1603+
1604+
:c:member:`~PyConfig.prefix`, and :c:member:`~PyConfig.exec_prefix` are now
1605+
set to the ``pyvenv.cfg`` directory. This was previously done by :mod:`site`,
1606+
therefore affected by :option:`-S`.
15941607
15951608
PyInitConfig C API
15961609
==================

Doc/library/site.rst

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,22 @@ added path for configuration files.
4949
identified by the "t" suffix in the version-specific directory name, such as
5050
:file:`lib/python3.13t/`.
5151

52-
If a file named "pyvenv.cfg" exists one directory above sys.executable,
53-
sys.prefix and sys.exec_prefix are set to that directory and
54-
it is also checked for site-packages (sys.base_prefix and
55-
sys.base_exec_prefix will always be the "real" prefixes of the Python
56-
installation). If "pyvenv.cfg" (a bootstrap configuration file) contains
57-
the key "include-system-site-packages" set to anything other than "true"
58-
(case-insensitive), the system-level prefixes will not be
59-
searched for site-packages; otherwise they will.
52+
.. versionchanged:: 3.14
53+
54+
:mod:`site` is no longer responsible for updating :data:`sys.prefix` and
55+
:data:`sys.exec_prefix` on :ref:`sys-path-init-virtual-environments`. This is
56+
now done during the :ref:`path initialization <sys-path-init>`. As a result,
57+
under :ref:`sys-path-init-virtual-environments`, :data:`sys.prefix` and
58+
:data:`sys.exec_prefix` no longer depend on the :ref:`site` initialization,
59+
and are therefore unaffected by :option:`-S`.
60+
61+
.. _site-virtual-environments-configuration:
62+
63+
When running under a :ref:`virtual environment <sys-path-init-virtual-environments>`,
64+
the ``pyvenv.cfg`` file in :data:`sys.prefix` is checked for site-specific
65+
configurations. If the ``include-system-site-packages`` key exists and is set to
66+
``true`` (case-insensitive), the system-level prefixes will be searched for
67+
site-packages, otherwise they won't.
6068

6169
.. index::
6270
single: # (hash); comment

Doc/library/sys.rst

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -130,27 +130,26 @@ always available.
130130

131131
.. data:: base_exec_prefix
132132

133-
Set during Python startup, before ``site.py`` is run, to the same value as
134-
:data:`exec_prefix`. If not running in a
135-
:ref:`virtual environment <venv-def>`, the values will stay the same; if
136-
``site.py`` finds that a virtual environment is in use, the values of
137-
:data:`prefix` and :data:`exec_prefix` will be changed to point to the
138-
virtual environment, whereas :data:`base_prefix` and
139-
:data:`base_exec_prefix` will remain pointing to the base Python
140-
installation (the one which the virtual environment was created from).
133+
Equivalent to :data:`exec_prefix`, but refering to the base Python installation.
134+
135+
When running under :ref:`sys-path-init-virtual-environments`,
136+
:data:`exec_prefix` gets overwritten to the virtual environment prefix.
137+
:data:`base_exec_prefix`, conversely, does not change, and always points to
138+
the base Python installation.
139+
Refer to :ref:`sys-path-init-virtual-environments` for more information.
141140

142141
.. versionadded:: 3.3
143142

144143

145144
.. data:: base_prefix
146145

147-
Set during Python startup, before ``site.py`` is run, to the same value as
148-
:data:`prefix`. If not running in a :ref:`virtual environment <venv-def>`, the values
149-
will stay the same; if ``site.py`` finds that a virtual environment is in
150-
use, the values of :data:`prefix` and :data:`exec_prefix` will be changed to
151-
point to the virtual environment, whereas :data:`base_prefix` and
152-
:data:`base_exec_prefix` will remain pointing to the base Python
153-
installation (the one which the virtual environment was created from).
146+
Equivalent to :data:`prefix`, but refering to the base Python installation.
147+
148+
When running under :ref:`virtual environment <venv-def>`,
149+
:data:`prefix` gets overwritten to the virtual environment prefix.
150+
:data:`base_prefix`, conversely, does not change, and always points to
151+
the base Python installation.
152+
Refer to :ref:`sys-path-init-virtual-environments` for more information.
154153

155154
.. versionadded:: 3.3
156155

@@ -483,11 +482,19 @@ always available.
483482

484483
.. note::
485484

486-
If a :ref:`virtual environment <venv-def>` is in effect, this
487-
value will be changed in ``site.py`` to point to the virtual environment.
488-
The value for the Python installation will still be available, via
489-
:data:`base_exec_prefix`.
485+
If a :ref:`virtual environment <venv-def>` is in effect, this :data:`exec_prefix`
486+
will point to the virtual environment. The value for the Python installation
487+
will still be available, via :data:`base_exec_prefix`.
488+
Refer to :ref:`sys-path-init-virtual-environments` for more information.
490489

490+
.. versionchanged:: 3.14
491+
492+
When running under a :ref:`virtual environment <venv-def>`,
493+
:data:`prefix` and :data:`exec_prefix` are now set to the virtual
494+
environment prefix by the :ref:`path initialization <sys-path-init>`,
495+
instead of :mod:`site`. This means that :data:`prefix` and
496+
:data:`exec_prefix` always point to the virtual environment, even when
497+
:mod:`site` is disabled (:option:`-S`).
491498

492499
.. data:: executable
493500

@@ -1483,10 +1490,21 @@ always available.
14831490
argument to the :program:`configure` script. See
14841491
:ref:`installation_paths` for derived paths.
14851492

1486-
.. note:: If a :ref:`virtual environment <venv-def>` is in effect, this
1487-
value will be changed in ``site.py`` to point to the virtual
1488-
environment. The value for the Python installation will still be
1489-
available, via :data:`base_prefix`.
1493+
.. note::
1494+
1495+
If a :ref:`virtual environment <venv-def>` is in effect, this :data:`prefix`
1496+
will point to the virtual environment. The value for the Python installation
1497+
will still be available, via :data:`base_prefix`.
1498+
Refer to :ref:`sys-path-init-virtual-environments` for more information.
1499+
1500+
.. versionchanged:: 3.14
1501+
1502+
When running under a :ref:`virtual environment <venv-def>`,
1503+
:data:`prefix` and :data:`exec_prefix` are now set to the virtual
1504+
environment prefix by the :ref:`path initialization <sys-path-init>`,
1505+
instead of :mod:`site`. This means that :data:`prefix` and
1506+
:data:`exec_prefix` always point to the virtual environment, even when
1507+
:mod:`site` is disabled (:option:`-S`).
14901508

14911509

14921510
.. data:: ps1

Doc/library/sys_path_init.rst

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,15 @@ however on other platforms :file:`lib/python{majorversion}.{minorversion}/lib-dy
4747
``exec_prefix``. On some platforms :file:`lib` may be :file:`lib64` or another value,
4848
see :data:`sys.platlibdir` and :envvar:`PYTHONPLATLIBDIR`.
4949

50-
Once found, ``prefix`` and ``exec_prefix`` are available at :data:`sys.prefix` and
51-
:data:`sys.exec_prefix` respectively.
50+
Once found, ``prefix`` and ``exec_prefix`` are available at
51+
:data:`sys.base_prefix` and :data:`sys.base_exec_prefix` respectively.
52+
53+
If :envvar:`PYTHONHOME` is not set, and a ``pyvenv.cfg`` file is found alongside
54+
the main executable, or in its parent directory, :data:`sys.prefix` and
55+
:data:`sys.exec_prefix` get set to the directory containing ``pyvenv.cfg``,
56+
otherwise they are set to the same value as :data:`sys.base_prefix` and
57+
:data:`sys.base_exec_prefix`, respectively.
58+
This is used by :ref:`sys-path-init-virtual-environments`.
5259

5360
Finally, the :mod:`site` module is processed and :file:`site-packages` directories
5461
are added to the module search path. A common way to customize the search path is
@@ -60,18 +67,40 @@ the :mod:`site` module documentation.
6067
Certain command line options may further affect path calculations.
6168
See :option:`-E`, :option:`-I`, :option:`-s` and :option:`-S` for further details.
6269

63-
Virtual environments
70+
.. versionchanged:: 3.14
71+
72+
:data:`sys.prefix` and :data:`sys.exec_prefix` are now set to the
73+
``pyvenv.cfg`` directory during the path initialization. This was previously
74+
done by :mod:`site`, therefore affected by :option:`-S`.
75+
76+
.. _sys-path-init-virtual-environments:
77+
78+
Virtual Environments
6479
--------------------
6580

66-
If Python is run in a virtual environment (as described at :ref:`tut-venv`)
67-
then ``prefix`` and ``exec_prefix`` are specific to the virtual environment.
81+
Virtual environments place a ``pyvenv.cfg`` file in their prefix, which causes
82+
:data:`sys.prefix` and :data:`sys.exec_prefix` to point to them, instead of the
83+
base installation.
84+
85+
The ``prefix`` and ``exec_prefix`` values of the base installation are available
86+
at :data:`sys.base_prefix` and :data:`sys.base_exec_prefix`.
6887

69-
If a ``pyvenv.cfg`` file is found alongside the main executable, or in the
70-
directory one level above the executable, the following variations apply:
88+
As well as being used as a marker to identify virtual environments,
89+
``pyvenv.cfg`` may also be used to configure the :mod:`site` initialization.
90+
Please refer to :mod:`site`'s
91+
:ref:`virtual environments documentation <site-virtual-environments-configuration>`.
92+
93+
.. note::
94+
95+
:envvar:`PYTHONHOME` overrides the ``pyvenv.cfg`` detection.
96+
97+
.. note::
7198

72-
* If ``home`` is an absolute path and :envvar:`PYTHONHOME` is not set, this
73-
path is used instead of the path to the main executable when deducing ``prefix``
74-
and ``exec_prefix``.
99+
There are other ways how "virtual environments" could be implemented, this
100+
documentation referes implementations based on the ``pyvenv.cfg`` mechanism,
101+
such as :mod:`venv`. Most virtual environment implementations follow the
102+
model set by :mod:`venv`, but there may be exotic implementations that
103+
diverge from it.
75104

76105
_pth files
77106
----------

Doc/library/venv.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ A virtual environment is created on top of an existing
2525
Python installation, known as the virtual environment's "base" Python, and may
2626
optionally be isolated from the packages in the base environment,
2727
so only those explicitly installed in the virtual environment are available.
28+
See :ref:`sys-path-init-virtual-environments` and :mod:`site`'s
29+
:ref:`virtual environments documentation <site-virtual-environments-configuration>`
30+
for more information.
2831

2932
When used from within a virtual environment, common installation tools such as
3033
:pypi:`pip` will install Python packages into a virtual environment

0 commit comments

Comments
 (0)