Skip to content

Commit d969ae6

Browse files
committed
Update site.rst documentation
1 parent 88465c1 commit d969ae6

File tree

1 file changed

+90
-64
lines changed

1 file changed

+90
-64
lines changed

Doc/library/site.rst

Lines changed: 90 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import can be suppressed using the interpreter's :option:`-S` option.
1717

1818
Importing this module normally appends site-specific paths to the module search path
1919
and adds :ref:`callables <site-consts>`, including :func:`help` to the built-in
20-
namespace. However, Python startup option :option:`-S` blocks this and this module
20+
namespace. However, Python startup option :option:`-S` blocks this, and this module
2121
can be safely imported with no automatic modifications to the module search path
2222
or additions to the builtins. To explicitly trigger the usual site-specific
2323
additions, call the :func:`main` function.
@@ -71,52 +71,105 @@ the user site prefixes are also implicitly not searched for site-packages.
7171
single: # (hash); comment
7272
pair: statement; import
7373

74-
A path configuration file is a file whose name has the form :file:`{name}.pth`
75-
and exists in one of the four directories mentioned above; its contents are
76-
additional items (one per line) to be added to ``sys.path``. Non-existing items
77-
are never added to ``sys.path``, and no check is made that the item refers to a
78-
directory rather than a file. No item is added to ``sys.path`` more than
79-
once. Blank lines and lines beginning with ``#`` are skipped. Lines starting
80-
with ``import`` (followed by space or tab) are executed.
74+
.. _startup-configuration-files:
75+
76+
The ``site.py`` module recognizes two startup configuration files of the form
77+
:file:`{name}.pth` for path configurations, and :file:`{name}.start` for
78+
pre-first-line code execution. Both files can exist in one of the four
79+
directories mentioned above. Within each directory, these files are sorted
80+
alphabetically by filename, then parsed in sorted order.
81+
82+
.. _site-pth-files:
83+
84+
Path extensions (:file:`.pth` files)
85+
------------------------------------
86+
87+
:file:`{name}.pth` contains additional items (one per line) to be appended to
88+
``sys.path``. Items that name non-existing directories are never added to
89+
``sys.path``, and no check is made that the item refers to a directory rather
90+
than a file. No item is added to ``sys.path`` more than once. Blank lines
91+
and lines beginning with ``#`` are skipped.
92+
93+
For backward compatibility, lines starting with ``import`` (followed by space
94+
or tab) are executed with :func:`exec`.
95+
96+
.. _site-start-files:
97+
98+
Startup entry points (:file:`.start` files)
99+
--------------------------------------------
100+
101+
.. versionadded:: 3.15
102+
103+
A startup entry point file is a file whose name has the form
104+
:file:`{name}.start` and exists in one of the site-packages directories
105+
described above. Each file specifies entry points to be called during
106+
interpreter startup, using the ``pkg.mod:callable`` syntax understood by
107+
:func:`pkgutil.resolve_name`.
108+
109+
Each non-blank line that does not begin with ``#`` must contain an entry
110+
point reference in the form ``pkg.mod:callable``. The colon and callable
111+
portion are mandatory. Each callable is invoked with no arguments, and
112+
any return value is discarded.
113+
114+
:file:`.start` files are processed after all :file:`.pth` path extensions
115+
have been applied to :data:`sys.path`, ensuring that paths are available
116+
before any startup code runs.
117+
118+
Unlike :data:`sys.path` extensions from :file:`.pth` files, duplicate entry
119+
points are **not** deduplicated --- if an entry point appears more than once,
120+
it will be called more than once.
121+
122+
If an exception occurs during resolution or invocation of an entry point,
123+
a traceback is printed to :data:`sys.stderr` and processing continues with
124+
the remaining entry points.
125+
126+
:pep:`829` defined the specification for these features.
81127

82128
.. note::
83129

84-
An executable line in a :file:`.pth` file is run at every Python startup,
130+
If a :file:`{name}.start` file exists alongside a :file:`{name}.pth` file
131+
with the same base name, any ``import`` lines in the :file:`.pth` file are
132+
ignored in favor of the entry points in the :file:`.start` file.
133+
134+
.. note::
135+
136+
Executable lines (i.e. ``import`` lines in a :file:`.pth` file or
137+
:file:`{name}.start` entry points) are always run at Python startup (unless
138+
:option:`-S` is given to disable the ``site.py`` module entirely),
85139
regardless of whether a particular module is actually going to be used.
86-
Its impact should thus be kept to a minimum.
87-
The primary intended purpose of executable lines is to make the
88-
corresponding module(s) importable
89-
(load 3rd-party import hooks, adjust :envvar:`PATH` etc).
90-
Any other initialization is supposed to be done upon a module's
91-
actual import, if and when it happens.
92-
Limiting a code chunk to a single line is a deliberate measure
93-
to discourage putting anything more complex here.
94140

95141
.. versionchanged:: 3.13
142+
96143
The :file:`.pth` files are now decoded by UTF-8 at first and then by the
97144
:term:`locale encoding` if it fails.
98145

99146
.. versionchanged:: 3.15
100-
Lines starting with ``import`` are deprecated. During the deprecation
101-
period, such lines are still executed, but a diagnostic message is
102-
emitted when the :option:`-v` flag is given. If a :file:`{name}.start`
103-
file with the same base name exists, ``import`` lines are silently
104-
ignored. See :ref:`site-start-files` and :pep:`829`.
147+
148+
:file:`.pth` file lines starting with ``import`` are deprecated. During
149+
the deprecation period, such lines are still executed, but a diagnostic
150+
message is emitted when the :option:`-v` flag is given. If a
151+
:file:`{name}.start` file with the same base name exists, ``import`` lines
152+
:file:`{name}.pth` files are silently ignored. See :ref:`site-start-files`
153+
and :pep:`829`.
105154

106155
.. versionchanged:: 3.15
107-
Errors on individual lines no longer abort processing of the rest of
108-
the file. Each error is reported and the remaining lines continue to
109-
be processed.
156+
157+
Errors on individual lines no longer abort processing of the rest of the
158+
file. Each error is reported and the remaining lines continue to be
159+
processed.
110160

111161
.. index::
112162
single: package
113163
triple: path; configuration; file
114164

165+
Startup file examples
166+
---------------------
167+
115168
For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
116169
:file:`/usr/local`. The Python X.Y library is then installed in
117170
:file:`/usr/local/lib/python{X.Y}`. Suppose this has
118171
a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
119-
subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
172+
sub-subdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
120173
configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume
121174
:file:`foo.pth` contains the following::
122175

@@ -143,58 +196,31 @@ directory precedes the :file:`foo` directory because :file:`bar.pth` comes
143196
alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is
144197
not mentioned in either path configuration file.
145198

146-
.. _site-start-files:
147-
148-
Startup entry points (:file:`.start` files)
149-
--------------------------------------------
199+
Let's say that there is also a :file:`foo.start` file containing the
200+
following::
150201

151-
.. versionadded:: 3.15
202+
# foo package startup code
152203

153-
A startup entry point file is a file whose name has the form
154-
:file:`{name}.start` and exists in one of the site-packages directories
155-
described above. Each file specifies entry points to be called during
156-
interpreter startup, using the ``pkg.mod:callable`` syntax understood by
157-
:func:`pkgutil.resolve_name`.
204+
foo.submod:initialize()
158205

159-
Each non-blank line that does not begin with ``#`` must contain an entry
160-
point reference in the form ``pkg.mod:callable``. The colon and callable
161-
portion are mandatory. Each callable is invoked with no arguments, and
162-
any return value is discarded.
163-
164-
:file:`.start` files are processed after all :file:`.pth` path extensions
165-
have been applied to :data:`sys.path`, ensuring that paths are available
166-
before any startup code runs. Within each site-packages directory, files
167-
are sorted alphabetically by filename.
168-
169-
Unlike :data:`sys.path` extensions from :file:`.pth` files, duplicate entry
170-
points are **not** deduplicated --- if an entry point appears more than once,
171-
it will be called more than once.
172-
173-
If an exception occurs during resolution or invocation of an entry point,
174-
a traceback is printed to :data:`sys.stderr` and processing continues with
175-
the remaining entry points.
176-
177-
See :pep:`829` for the full specification.
178-
179-
.. note::
206+
Now, after ``sys.path`` has been extended as above, and before Python turns
207+
control over to user code, the ``foo.submod`` module is imported and the
208+
``initialize()`` function from that module is called.
180209

181-
If a :file:`{name}.start` file exists alongside a :file:`{name}.pth`
182-
file with the same base name, any ``import`` lines in the :file:`.pth`
183-
file are ignored in favour of the entry points in the :file:`.start`
184-
file.
185210

186211
.. _site-migration-guide:
187212

188213
Migrating from ``import`` lines in ``.pth`` files to ``.start`` files
189-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
214+
---------------------------------------------------------------------
190215

191216
If your package currently ships a :file:`{name}.pth` file, you can keep all
192217
``sys.path`` extension lines unchanged. Only ``import`` lines need to be
193218
migrated.
194219

195220
To migrate, create a callable (taking zero arguments) within an importable
196221
module in your package. Reference it as a ``pkg.mod:callable`` entry point
197-
in a matching :file:`{name}.start` file.
222+
in a matching :file:`{name}.start` file. Move everything on your ``import``
223+
line after the first semi-colon into the ``callable()`` function.
198224

199225
If your package must straddle older Pythons that do not support :pep:`829`
200226
and newer Pythons that do, change the ``import`` lines in your
@@ -204,7 +230,7 @@ and newer Pythons that do, change the ``import`` lines in your
204230

205231
Older Pythons will execute these ``import`` lines, while newer Pythons will
206232
ignore them in favor of the :file:`{name}.start` file. After the straddling
207-
period, remove all ``import`` lines from your :file:`.pth` file.
233+
period, remove all ``import`` lines from your :file:`.pth` files.
208234

209235

210236
:mod:`!sitecustomize`

0 commit comments

Comments
 (0)