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
1214import 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
2831if it refers to an existing directory, and if so, adds it to ``sys.path `` and
2932also 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 `
3235and exists in one of the four directories mentioned above; its contents are
3336additional 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
3639once. Blank lines and lines beginning with ``# `` are skipped. Lines starting
3740with ``import `` (followed by space or tab) are executed.
3841
@@ -45,8 +48,7 @@ with ``import`` (followed by space or tab) are executed.
4548
4649For 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
5052a subdirectory :file: `/usr/local/lib/python{ X.Y } /site-packages ` with three
5153subsubdirectories, :file: `foo `, :file: `bar ` and :file: `spam `, and two path
5254configuration files, :file: `foo.pth ` and :file: `bar.pth `. Assume
@@ -79,86 +81,130 @@ not mentioned in either path configuration file.
7981
8082After 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
8796Note that for some non-Unix systems, ``sys.prefix `` and ``sys.exec_prefix `` are
8897empty, 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
0 commit comments