Skip to content

Commit c62b944

Browse files
authored
bpo-31582: Created a new documentation section describing sys.path initialization (GH-31082)
1 parent fe01060 commit c62b944

File tree

10 files changed

+128
-29
lines changed

10 files changed

+128
-29
lines changed

Doc/library/importlib.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ managing aspects of Python packages:
5151
The :func:`.__import__` function
5252
The :keyword:`import` statement is syntactic sugar for this function.
5353

54+
:ref:`sys-path-init`
55+
The initialization of :data:`sys.path`.
56+
5457
:pep:`235`
5558
Import on Case-Insensitive Platforms
5659

Doc/library/modules.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ The full list of modules described in this chapter is:
1919
importlib.rst
2020
importlib.resources.rst
2121
importlib.metadata.rst
22+
sys_path_init.rst

Doc/library/site.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,6 @@ value greater than 2 if there is an error.
276276

277277
.. seealso::
278278

279-
:pep:`370` -- Per user site-packages directory
279+
* :pep:`370` -- Per user site-packages directory
280+
* :ref:`sys-path-init` -- The initialization of :data:`sys.path`.
281+

Doc/library/sys.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,15 +1124,16 @@ always available.
11241124
current directory first. Notice that the script directory is inserted *before*
11251125
the entries inserted as a result of :envvar:`PYTHONPATH`.
11261126

1127+
The initialization of :data:`sys.path` is documented at :ref:`sys-path-init`.
1128+
11271129
A program is free to modify this list for its own purposes. Only strings
11281130
and bytes should be added to :data:`sys.path`; all other data types are
11291131
ignored during import.
11301132

11311133

11321134
.. seealso::
1133-
Module :mod:`site` This describes how to use .pth files to extend
1134-
:data:`sys.path`.
1135-
1135+
* Module :mod:`site` This describes how to use .pth files to
1136+
extend :data:`sys.path`.
11361137

11371138
.. data:: path_hooks
11381139

Doc/library/sys_path_init.rst

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
.. _sys-path-init:
2+
3+
The initialization of the :data:`sys.path` module search path
4+
=============================================================
5+
6+
A module search path is initialized when Python starts. This module search path
7+
may be accessed at :data:`sys.path`.
8+
9+
The first entry in the module search path is the directory that contains the
10+
input script, if there is one. Otherwise, the first entry is the current
11+
directory, which is the case when executing the interactive shell, a :option:`-c`
12+
command, or :option:`-m` module.
13+
14+
The :envvar:`PYTHONPATH` environment variable is often used to add directories
15+
to the search path. If this environment variable is found then the contents are
16+
added to the module search path.
17+
18+
.. note::
19+
20+
:envvar:`PYTHONPATH` will affect all installed Python versions/environments.
21+
Be wary of setting this in your shell profile or global environment variables.
22+
The :mod:`site` module offers more nuanced techniques as mentioned below.
23+
24+
The next items added are the directories containing standard Python modules as
25+
well as any :term:`extension module`\s that these modules depend on. Extension
26+
modules are ``.pyd`` files on Windows and ``.so`` files on other platforms. The
27+
directory with the platform-independent Python modules is called ``prefix``.
28+
The directory with the extension modules is called ``exec_prefix``.
29+
30+
The :envvar:`PYTHONHOME` environment variable may be used to set the ``prefix``
31+
and ``exec_prefix`` locations. Otherwise these directories are found by using
32+
the Python executable as a starting point and then looking for various 'landmark'
33+
files and directories. Note that any symbolic links are followed so the real
34+
Python executable location is used as the search starting point. The Python
35+
executable location is called ``home``.
36+
37+
Once ``home`` is determined, the ``prefix`` directory is found by first looking
38+
for :file:`python{majorversion}{minorversion}.zip` (``python311.zip``). On Windows
39+
the zip archive is searched for in ``home`` and on Unix the archive is expected
40+
to be in :file:`lib`. Note that the expected zip archive location is added to the
41+
module search path even if the archive does not exist. If no archive was found,
42+
Python on Windows will continue the search for ``prefix`` by looking for :file:`Lib\\os.py`.
43+
Python on Unix will look for :file:`lib/python{majorversion}.{minorversion}/os.py`
44+
(``lib/python3.11/os.py``). On Windows ``prefix`` and ``exec_prefix`` are the same,
45+
however on other platforms :file:`lib/python{majorversion}.{minorversion}/lib-dynload`
46+
(``lib/python3.11/lib-dynload``) is searched for and used as an anchor for
47+
``exec_prefix``. On some platforms :file:`lib` may be :file:`lib64` or another value,
48+
see :data:`sys.platlibdir` and :envvar:`PYTHONPLATLIBDIR`.
49+
50+
Once found, ``prefix`` and ``exec_prefix`` are available at :data:`sys.prefix` and
51+
:data:`sys.exec_prefix` respectively.
52+
53+
Finally, the :mod:`site` module is processed and :file:`site-packages` directories
54+
are added to the module search path. A common way to customize the search path is
55+
to create :mod:`sitecustomize` or :mod:`usercustomize` modules as described in
56+
the :mod:`site` module documentation.
57+
58+
.. note::
59+
60+
Certain command line options may further affect path calculations.
61+
See :option:`-E`, :option:`-I`, :option:`-s` and :option:`-S` for further details.
62+
63+
Virtual environments
64+
--------------------
65+
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.
68+
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:
71+
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``.
75+
76+
_pth files
77+
----------
78+
79+
To completely override :data:`sys.path` create a ``._pth`` file with the same
80+
name as the shared library or executable (``python._pth`` or ``python311._pth``).
81+
The shared library path is always known on Windows, however it may not be
82+
available on other platforms. In the ``._pth`` file specify one line for each path
83+
to add to :data:`sys.path`. The file based on the shared library name overrides
84+
the one based on the executable, which allows paths to be restricted for any
85+
program loading the runtime if desired.
86+
87+
When the file exists, all registry and environment variables are ignored,
88+
isolated mode is enabled, and :mod:`site` is not imported unless one line in the
89+
file specifies ``import site``. Blank paths and lines starting with ``#`` are
90+
ignored. Each path may be absolute or relative to the location of the file.
91+
Import statements other than to ``site`` are not permitted, and arbitrary code
92+
cannot be specified.
93+
94+
Note that ``.pth`` files (without leading underscore) will be processed normally
95+
by the :mod:`site` module when ``import site`` has been specified.
96+
97+
Embedded Python
98+
---------------
99+
100+
If Python is embedded within another application :c:func:`Py_InitializeFromConfig` and
101+
the :c:type:`PyConfig` structure can be used to initialize Python. The path specific
102+
details are described at :ref:`init-path-config`. Alternatively the older :c:func:`Py_SetPath`
103+
can be used to bypass the initialization of the module search path.
104+
105+
.. seealso::
106+
107+
* :ref:`windows_finding_modules` for detailed Windows notes.
108+
* :ref:`using-on-unix` for Unix details.

Doc/tutorial/modules.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ named :file:`spam.py` in a list of directories given by the variable
194194
* The installation-dependent default (by convention including a
195195
``site-packages`` directory, handled by the :mod:`site` module).
196196

197+
More details are at :ref:`sys-path-init`.
198+
197199
.. note::
198200
On file systems which support symlinks, the directory containing the input
199201
script is calculated after the symlink is followed. In other words the

Doc/using/windows.rst

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -946,32 +946,13 @@ target Python.
946946

947947

948948

949-
.. _finding_modules:
949+
.. _windows_finding_modules:
950950

951951
Finding modules
952952
===============
953953

954-
Python usually stores its library (and thereby your site-packages folder) in the
955-
installation directory. So, if you had installed Python to
956-
:file:`C:\\Python\\`, the default library would reside in
957-
:file:`C:\\Python\\Lib\\` and third-party modules should be stored in
958-
:file:`C:\\Python\\Lib\\site-packages\\`.
959-
960-
To completely override :data:`sys.path`, create a ``._pth`` file with the same
961-
name as the DLL (``python37._pth``) or the executable (``python._pth``) and
962-
specify one line for each path to add to :data:`sys.path`. The file based on the
963-
DLL name overrides the one based on the executable, which allows paths to be
964-
restricted for any program loading the runtime if desired.
965-
966-
When the file exists, all registry and environment variables are ignored,
967-
isolated mode is enabled, and :mod:`site` is not imported unless one line in the
968-
file specifies ``import site``. Blank paths and lines starting with ``#`` are
969-
ignored. Each path may be absolute or relative to the location of the file.
970-
Import statements other than to ``site`` are not permitted, and arbitrary code
971-
cannot be specified.
972-
973-
Note that ``.pth`` files (without leading underscore) will be processed normally
974-
by the :mod:`site` module when ``import site`` has been specified.
954+
These notes supplement the description at :ref:`sys-path-init` with
955+
detailed Windows notes.
975956

976957
When no ``._pth`` file is found, this is how :data:`sys.path` is populated on
977958
Windows:

Doc/whatsnew/3.6.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ Windows improvements:
162162

163163
* A ``._pth`` file can be added to force isolated mode and fully specify
164164
all search paths to avoid registry and environment lookup. See
165-
:ref:`the documentation <finding_modules>` for more information.
165+
:ref:`the documentation <windows_finding_modules>` for more information.
166166

167167
* A ``python36.zip`` file now works as a landmark to infer
168-
:envvar:`PYTHONHOME`. See :ref:`the documentation <finding_modules>` for
168+
:envvar:`PYTHONHOME`. See :ref:`the documentation <windows_finding_modules>` for
169169
more information.
170170

171171

Doc/whatsnew/3.7.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2474,7 +2474,7 @@ Windows-only Changes
24742474

24752475
The file used to override :data:`sys.path` is now called
24762476
``<python-executable>._pth`` instead of ``'sys.path'``.
2477-
See :ref:`finding_modules` for more information.
2477+
See :ref:`windows_finding_modules` for more information.
24782478
(Contributed by Steve Dower in :issue:`28137`.)
24792479

24802480

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,7 @@ Colin Watson
18971897
David Watson
18981898
Aaron Watters
18991899
Alex Waygood
1900+
Russel Webber
19001901
Henrik Weber
19011902
Leon Weber
19021903
Steve Weber

0 commit comments

Comments
 (0)