Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de4f05b

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, partly documented in the 3.2 What’s New, is fully 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 e5cad23 commit de4f05b

4 files changed

Lines changed: 120 additions & 43 deletions

File tree

Doc/library/site.rst

Lines changed: 86 additions & 36 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

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

4346
For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
4447
:file:`/usr/local`. The Python X.Y library is then installed in
45-
:file:`/usr/local/lib/python{X.Y}` (where only the first three characters of
46-
``sys.version`` are used to form the installation path name). Suppose this has
48+
:file:`/usr/local/lib/python{X.Y}`. Suppose this has
4749
a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
4850
subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
4951
configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume
@@ -76,74 +78,122 @@ not mentioned in either path configuration file.
7678

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

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

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

8897

8998
.. data:: PREFIXES
9099

91-
A list of prefixes for site package directories
100+
A list of prefixes for site-packages directories.
92101

93102

94103
.. data:: ENABLE_USER_SITE
95104

96-
Flag showing the status of the user site directory. True means the
97-
user site directory is enabled and added to sys.path. When the flag
98-
is None the user site directory is disabled for security reasons.
105+
Flag showing the status of the user site-packages directory. ``True`` means
106+
that it is enabled and was added to ``sys.path``. ``False`` means that it
107+
was disabled by user request (with :option:`-s` or
108+
:envvar:`PYTHONNOUSERSITE`). ``None`` means it was disabled for security
109+
reasons (mismatch between user or group id and effective id) or by an
110+
administrator.
99111

100112

101113
.. data:: USER_SITE
102114

103-
Path to the user site directory for the current Python version or None
115+
Path to the user site-packages for the running Python. Can be ``None`` if
116+
:func:`getusersitepackages` hasn't been called yet. Default value is
117+
:file:`~/.local/lib/python{X.Y}/site-packages` for UNIX and non-framework Mac
118+
OS X builds, :file:`~/Library/Python/{X.Y}/lib/python/site-packages` for Mac
119+
framework builds, and :file:`{%APPDATA%}\\Python\\Python{XY}\\site-packages`
120+
on Windows. This directory is a site directory, which means that
121+
:file:`.pth` files in it will be processed.
104122

105123

106124
.. data:: USER_BASE
107125

108-
Path to the base directory for user site directories
126+
Path to the base directory for the user site-packages. Can be ``None`` if
127+
:func:`getuserbase` hasn't been called yet. Default value is
128+
:file:`~/.local` for UNIX and Mac OS X non-framework builds,
129+
:file:`~/Library/Python/{X.Y}` for Mac framework builds, and
130+
:file:`{%APPDATA%}\\Python` for Windows. This value is used by Distutils to
131+
compute the installation directories for scripts, data files, Python modules,
132+
etc. See also :envvar:`PYTHONUSERBASE`.
109133

110134

111-
.. envvar:: PYTHONNOUSERSITE
135+
.. function:: addsitedir(sitedir, known_paths=None)
112136

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

114-
.. envvar:: PYTHONUSERBASE
115139

140+
.. function:: getsitepackages()
116141

117-
.. function:: addsitedir(sitedir, known_paths=None)
142+
Return a list containing all global site-packages directories (and possibly
143+
site-python).
118144

119-
Adds a directory to sys.path and processes its pth files.
145+
.. versionadded:: 3.2
120146

121-
.. function:: getsitepackages()
122147

123-
Returns a list containing all global site-packages directories
124-
(and possibly site-python).
148+
.. function:: getuserbase()
149+
150+
Return the path of the user base directory, :data:`USER_BASE`. If it is not
151+
initialized yet, this function will also set it, respecting
152+
:envvar:`PYTHONUSERBASE`.
125153

126154
.. versionadded:: 3.2
127155

128-
.. function:: getuserbase()
129156

130-
Returns the "user base" directory path.
157+
.. function:: getusersitepackages()
131158

132-
The "user base" directory can be used to store data. If the global
133-
variable ``USER_BASE`` is not initialized yet, this function will also set
134-
it.
159+
Return the path of the user-specific site-packages directory,
160+
:data:`USER_SITE`. If it is not initialized yet, this function will also set
161+
it, respecting :envvar:`PYTHONNOUSERSITE` and :data:`USER_BASE`.
135162

136163
.. versionadded:: 3.2
137164

138-
.. function:: getusersitepackages()
139165

140-
Returns the user-specific site-packages directory path.
166+
The :mod:`site` module also provides a way to get the user directories from the
167+
command line:
141168

142-
If the global variable ``USER_SITE`` is not initialized yet, this
143-
function will also set it.
169+
.. code-block:: sh
144170
145-
.. versionadded:: 3.2
171+
$ python3 -m site --user-site
172+
/home/user/.local/lib/python3.3/site-packages
173+
174+
.. program:: site
175+
176+
If it is called without arguments, it will print the contents of
177+
:data:`sys.path` on the standard output, followed by the value of
178+
:data:`USER_BASE` and whether the directory exists, then the same thing for
179+
:data:`USER_SITE`, and finally the value of :data:`ENABLE_USER_SITE`.
180+
181+
.. cmdoption:: --user-base
182+
183+
Print the path to the user base directory.
184+
185+
.. cmdoption:: --user-site
186+
187+
Print the path to the user site-packages directory.
188+
189+
If both options are given, user base and user site will be printed (always in
190+
this order), separated by :data:`os.pathsep`.
191+
192+
If any option is given, the script will exit with one of these values: ``O`` if
193+
the user site-packages directory is enabled, ``1`` if it was disabled by the
194+
user, ``2`` if it is disabled for security reasons or by an administrator, and a
195+
value greater than 2 if there is an error.
146196

147-
.. XXX Update documentation
148-
.. XXX document python -m site --user-base --user-site
197+
.. seealso::
149198

199+
: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
@@ -236,11 +236,33 @@ in the script::
236236
exec(open(filename).read())
237237

238238

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

241264
.. [#] On Unix, the Python 3.x interpreter is by default not installed with the
242265
executable named ``python``, so that it does not conflict with a
243266
simultaneously installed Python 2.x executable.
244267
245268
.. [#] A problem with the GNU Readline package may prevent this.
246-

Doc/using/cmdline.rst

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

230230
.. cmdoption:: -s
231231

232-
Don't add user site directory to sys.path
232+
Don't add the :data:`user site-packages directory <site.USER_SITE>` to
233+
:data:`sys.path`.
233234

234235
.. seealso::
235236

@@ -468,7 +469,8 @@ These environment variables influence Python's behavior.
468469

469470
.. envvar:: PYTHONNOUSERSITE
470471

471-
If this is set, Python won't add the user site directory to sys.path
472+
If this is set, Python won't add the :data:`user site-packages directory
473+
<site.USER_SITE>` to :data:`sys.path`.
472474

473475
.. seealso::
474476

@@ -477,7 +479,9 @@ These environment variables influence Python's behavior.
477479

478480
.. envvar:: PYTHONUSERBASE
479481

480-
Sets the base directory for the user site directory
482+
Defines the :data:`user base directory <site.USER_BASE>`, which is used to
483+
compute the path of the :data:`user site-packages directory <site.USER_SITE>`
484+
and Distutils installation paths for ``python setup.py install --user``.
481485

482486
.. seealso::
483487

0 commit comments

Comments
 (0)