Skip to content

Commit afd2fe2

Browse files
committed
Improve documentation for PEP 370 support in site module (#8617).
site.USER_BASE and site.USER_SITE are now fully documented. PEP 370 is outdated with respects to the Mac framework situation, but the code in sysconfig and the example in the 3.2 What’s New document helped me find the right values to document for Mac OS X. The command-line interface of the site module is also described in the module docs. The purpose of the usercustomize module is explained in the site docs, with a gentle introduction in the tutorial (right after the section that talks about PYTHONSTARTUP; a comment mentions it should be moved from the tutorial to another file, but that will be another bug). Various markup and wording improvements were made along the way in the site module docs. Duplicate and incomplete declarations of environment variables have also been removed (the original bug report was actually about these entries :). The site module docs are still a bit messy; I’ll see about improving them for #11553. All these sections are copiously interlinked and findable from the doc indexes.
1 parent e7329f4 commit afd2fe2

4 files changed

Lines changed: 119 additions & 46 deletions

File tree

‎Doc/library/site.rst‎

Lines changed: 85 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22
================================================
33

44
.. module:: site
5-
:synopsis: A standard way to reference site-specific modules.
5+
:synopsis: Module responsible for site-specific configuration.
66

77
**Source code:** :source:`Lib/site.py`
88

99
--------------
1010

11+
.. highlightlang:: none
12+
1113
**This module is automatically imported during initialization.** The automatic
1214
import can be suppressed using the interpreter's :option:`-S` option.
1315

1416
.. index:: triple: module; search; path
1517

16-
Importing this module will append site-specific paths to the module search path.
18+
Importing this module will append site-specific paths to the module search path
19+
and add a few builtins.
1720

1821
.. index::
1922
pair: site-python; directory
@@ -28,11 +31,11 @@ Unix and Macintosh). For each of the distinct head-tail combinations, it sees
2831
if it refers to an existing directory, and if so, adds it to ``sys.path`` and
2932
also inspects the newly added path for configuration files.
3033

31-
A path configuration file is a file whose name has the form :file:`package.pth`
34+
A path configuration file is a file whose name has the form :file:`{name}.pth`
3235
and exists in one of the four directories mentioned above; its contents are
3336
additional items (one per line) to be added to ``sys.path``. Non-existing items
34-
are never added to ``sys.path``, but no check is made that the item refers to a
35-
directory (rather than a file). No item is added to ``sys.path`` more than
37+
are never added to ``sys.path``, and no check is made that the item refers to a
38+
directory rather than a file. No item is added to ``sys.path`` more than
3639
once. Blank lines and lines beginning with ``#`` are skipped. Lines starting
3740
with ``import`` (followed by space or tab) are executed.
3841

@@ -45,8 +48,7 @@ with ``import`` (followed by space or tab) are executed.
4548

4649
For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
4750
:file:`/usr/local`. The Python X.Y library is then installed in
48-
:file:`/usr/local/lib/python{X.Y}` (where only the first three characters of
49-
``sys.version`` are used to form the installation path name). Suppose this has
51+
:file:`/usr/local/lib/python{X.Y}`. Suppose this has
5052
a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
5153
subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
5254
configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume
@@ -79,86 +81,130 @@ not mentioned in either path configuration file.
7981

8082
After these path manipulations, an attempt is made to import a module named
8183
:mod:`sitecustomize`, which can perform arbitrary site-specific customizations.
82-
If this import fails with an :exc:`ImportError` exception, it is silently
83-
ignored.
84+
It is typically created by a system administrator in the site-packages
85+
directory. If this import fails with an :exc:`ImportError` exception, it is
86+
silently ignored.
8487

85-
.. index:: module: sitecustomize
88+
.. index:: module: usercustomize
89+
90+
After this, an attempt is made to import a module named :mod:`usercustomize`,
91+
which can perform arbitrary user-specific customizations, if
92+
:data:`ENABLE_USER_SITE` is true. This file is intended to be created in the
93+
user site-packages directory (see below), which is part of ``sys.path`` unless
94+
disabled by :option:`-s`. An :exc:`ImportError` will be silently ignored.
8695

8796
Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are
8897
empty, and the path manipulations are skipped; however the import of
89-
:mod:`sitecustomize` is still attempted.
98+
:mod:`sitecustomize` and :mod:`usercustomize` is still attempted.
9099

91100

92101
.. data:: PREFIXES
93102

94-
A list of prefixes for site package directories
103+
A list of prefixes for site-packages directories.
95104

96105
.. versionadded:: 2.6
97106

98107

99108
.. data:: ENABLE_USER_SITE
100109

101-
Flag showing the status of the user site directory. True means the
102-
user site directory is enabled and added to sys.path. When the flag
103-
is None the user site directory is disabled for security reasons.
110+
Flag showing the status of the user site-packages directory. ``True`` means
111+
that it is enabled and was added to ``sys.path``. ``False`` means that it
112+
was disabled by user request (with :option:`-s` or
113+
:envvar:`PYTHONNOUSERSITE`). ``None`` means it was disabled for security
114+
reasons (mismatch between user or group id and effective id) or by an
115+
administrator.
104116

105117
.. versionadded:: 2.6
106118

107119

108120
.. data:: USER_SITE
109121

110-
Path to the user site directory for the current Python version or None
122+
Path to the user site-packages for the running Python. Can be ``None`` if
123+
:func:`getusersitepackages` hasn't been called yet. Default value is
124+
:file:`~/.local/lib/python{X.Y}/site-packages` for UNIX and non-framework Mac
125+
OS X builds, :file:`~/Library/Python/{X.Y}/lib/python/site-packages` for Mac
126+
framework builds, and :file:`{%APPDATA%}\\Python\\Python{XY}\\site-packages`
127+
on Windows. This directory is a site directory, which means that
128+
:file:`.pth` files in it will be processed.
111129

112130
.. versionadded:: 2.6
113131

114132

115133
.. data:: USER_BASE
116134

117-
Path to the base directory for user site directories
135+
Path to the base directory for the user site-packages. Can be ``None`` if
136+
:func:`getuserbase` hasn't been called yet. Default value is
137+
:file:`~/.local` for UNIX and Mac OS X non-framework builds,
138+
:file:`~/Library/Python/{X.Y}` for Mac framework builds, and
139+
:file:`{%APPDATA%}\\Python` for Windows. This value is used by Distutils to
140+
compute the installation directories for scripts, data files, Python modules,
141+
etc. See also :envvar:`PYTHONUSERBASE`.
118142

119143
.. versionadded:: 2.6
120144

121145

122-
.. envvar:: PYTHONNOUSERSITE
123-
124-
.. versionadded:: 2.6
146+
.. function:: addsitedir(sitedir, known_paths=None)
125147

148+
Add a directory to sys.path and process its :file:`.pth` files.
126149

127-
.. envvar:: PYTHONUSERBASE
128150

129-
.. versionadded:: 2.6
151+
.. function:: getsitepackages()
130152

153+
Return a list containing all global site-packages directories (and possibly
154+
site-python).
131155

132-
.. function:: addsitedir(sitedir, known_paths=None)
156+
.. versionadded:: 2.7
133157

134-
Adds a directory to sys.path and processes its pth files.
135158

136-
.. function:: getsitepackages()
159+
.. function:: getuserbase()
137160

138-
Returns a list containing all global site-packages directories
139-
(and possibly site-python).
161+
Return the path of the user base directory, :data:`USER_BASE`. If it is not
162+
initialized yet, this function will also set it, respecting
163+
:envvar:`PYTHONUSERBASE`.
140164

141165
.. versionadded:: 2.7
142166

143-
.. function:: getuserbase()
144167

145-
Returns the "user base" directory path.
168+
.. function:: getusersitepackages()
146169

147-
The "user base" directory can be used to store data. If the global
148-
variable ``USER_BASE`` is not initialized yet, this function will also set
149-
it.
170+
Return the path of the user-specific site-packages directory,
171+
:data:`USER_SITE`. If it is not initialized yet, this function will also set
172+
it, respecting :envvar:`PYTHONNOUSERSITE` and :data:`USER_BASE`.
150173

151174
.. versionadded:: 2.7
152175

153-
.. function:: getusersitepackages()
154176

155-
Returns the user-specific site-packages directory path.
177+
The :mod:`site` module also provides a way to get the user directories from the
178+
command line:
156179

157-
If the global variable ``USER_SITE`` is not initialized yet, this
158-
function will also set it.
180+
.. code-block:: sh
159181
160-
.. versionadded:: 2.7
182+
$ python3 -m site --user-site
183+
/home/user/.local/lib/python3.3/site-packages
184+
185+
.. program:: site
186+
187+
If it is called without arguments, it will print the contents of
188+
:data:`sys.path` on the standard output, followed by the value of
189+
:data:`USER_BASE` and whether the directory exists, then the same thing for
190+
:data:`USER_SITE`, and finally the value of :data:`ENABLE_USER_SITE`.
191+
192+
.. cmdoption:: --user-base
193+
194+
Print the path to the user base directory.
195+
196+
.. cmdoption:: --user-site
197+
198+
Print the path to the user site-packages directory.
199+
200+
If both options are given, user base and user site will be printed (always in
201+
this order), separated by :data:`os.pathsep`.
202+
203+
If any option is given, the script will exit with one of these values: ``O`` if
204+
the user site-packages directory is enabled, ``1`` if it was disabled by the
205+
user, ``2`` if it is disabled for security reasons or by an administrator, and a
206+
value greater than 2 if there is an error.
161207

162-
.. XXX Update documentation
163-
.. XXX document python -m site --user-base --user-site
208+
.. seealso::
164209

210+
:pep:`370` -- Per user site-packages directory

‎Doc/tutorial/interactive.rst‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,18 @@ symbol table. A command to check (or even suggest) matching parentheses,
156156
quotes, etc., would also be useful.
157157

158158
One alternative enhanced interactive interpreter that has been around for quite
159-
some time is `IPython`_, which features tab completion, object exploration and
159+
some time is IPython_, which features tab completion, object exploration and
160160
advanced history management. It can also be thoroughly customized and embedded
161161
into other applications. Another similar enhanced interactive environment is
162-
`bpython`_.
162+
bpython_.
163163

164164

165165
.. rubric:: Footnotes
166166

167167
.. [#] Python will execute the contents of a file identified by the
168168
:envvar:`PYTHONSTARTUP` environment variable when you start an interactive
169-
interpreter.
169+
interpreter. To customize Python even for non-interactive mode, see
170+
:ref:`tut-customize`.
170171
171172
172173
.. _GNU Readline: http://tiswww.case.edu/php/chet/readline/rltop.html

‎Doc/tutorial/interpreter.rst‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,29 @@ the startup file in a script, you must do this explicitly in the script::
242242
execfile(filename)
243243

244244

245+
.. _tut-customize:
246+
247+
The Customization Modules
248+
-------------------------
249+
250+
Python provides two hooks to let you customize it: :mod:`sitecustomize` and
251+
:mod:`usercustomize`. To see how it works, you need first to find the location
252+
of your user site-packages directory. Start Python and run this code:
253+
254+
>>> import site
255+
>>> site.getusersitepackages()
256+
'/home/user/.local/lib/python3.2/site-packages'
257+
258+
Now you can create a file named :file:`usercustomize.py` in that directory and
259+
put anything you want in it. It will affect every invocation of Python, unless
260+
it is started with the :option:`-s` option to disable the automatic import.
261+
262+
:mod:`sitecustomize` works in the same way, but is typically created by an
263+
administrator of the computer in the global site-packages directory, and is
264+
imported before :mod:`usercustomize`. See the documentation of the :mod:`site`
265+
module for more details.
266+
267+
245268
.. rubric:: Footnotes
246269

247270
.. [#] A problem with the GNU Readline package may prevent this.
248-

‎Doc/using/cmdline.rst‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ Miscellaneous options
255255

256256
.. cmdoption:: -s
257257

258-
Don't add user site directory to sys.path
258+
Don't add the :data:`user site-packages directory <site.USER_SITE>` to
259+
:data:`sys.path`.
259260

260261
.. versionadded:: 2.6
261262

@@ -532,7 +533,8 @@ These environment variables influence Python's behavior.
532533

533534
.. envvar:: PYTHONNOUSERSITE
534535

535-
If this is set, Python won't add the user site directory to sys.path
536+
If this is set, Python won't add the :data:`user site-packages directory
537+
<site.USER_SITE>` to :data:`sys.path`.
536538

537539
.. versionadded:: 2.6
538540

@@ -543,7 +545,9 @@ These environment variables influence Python's behavior.
543545

544546
.. envvar:: PYTHONUSERBASE
545547

546-
Sets the base directory for the user site directory
548+
Defines the :data:`user base directory <site.USER_BASE>`, which is used to
549+
compute the path of the :data:`user site-packages directory <site.USER_SITE>`
550+
and Distutils installation paths for ``python setup.py install --user``.
547551

548552
.. versionadded:: 2.6
549553

0 commit comments

Comments
 (0)