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
@@ -42,8 +45,7 @@ with ``import`` (followed by space or tab) are executed.
4245
4346For 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
4749a subdirectory :file: `/usr/local/lib/python{ X.Y } /site-packages ` with three
4850subsubdirectories, :file: `foo `, :file: `bar ` and :file: `spam `, and two path
4951configuration files, :file: `foo.pth ` and :file: `bar.pth `. Assume
@@ -76,74 +78,122 @@ not mentioned in either path configuration file.
7678
7779After 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
8493Note that for some non-Unix systems, ``sys.prefix `` and ``sys.exec_prefix `` are
8594empty, 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
0 commit comments