• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1:mod:`site` --- Site-specific configuration hook
2================================================
3
4.. module:: site
5   :synopsis: Module responsible for site-specific configuration.
6
7**Source code:** :source:`Lib/site.py`
8
9--------------
10
11.. highlightlang:: none
12
13**This module is automatically imported during initialization.** The automatic
14import can be suppressed using the interpreter's :option:`-S` option.
15
16.. index:: triple: module; search; path
17
18Importing this module will append site-specific paths to the module search path
19and add a few builtins, unless :option:`-S` was used.  In that case, this module
20can be safely imported with no automatic modifications to the module search path
21or additions to the builtins.  To explicitly trigger the usual site-specific
22additions, call the :func:`site.main` function.
23
24.. versionchanged:: 3.3
25   Importing the module used to trigger paths manipulation even when using
26   :option:`-S`.
27
28.. index::
29   pair: site-packages; directory
30
31It starts by constructing up to four directories from a head and a tail part.
32For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads
33are skipped.  For the tail part, it uses the empty string and then
34:file:`lib/site-packages` (on Windows) or
35:file:`lib/python{X.Y}/site-packages` (on Unix and Macintosh).  For each
36of the distinct head-tail combinations, it sees if it refers to an existing
37directory, and if so, adds it to ``sys.path`` and also inspects the newly
38added path for configuration files.
39
40.. versionchanged:: 3.5
41   Support for the "site-python" directory has been removed.
42
43If a file named "pyvenv.cfg" exists one directory above sys.executable,
44sys.prefix and sys.exec_prefix are set to that directory and
45it is also checked for site-packages (sys.base_prefix and
46sys.base_exec_prefix will always be the "real" prefixes of the Python
47installation). If "pyvenv.cfg" (a bootstrap configuration file) contains
48the key "include-system-site-packages" set to anything other than "false"
49(case-insensitive), the system-level prefixes will still also be
50searched for site-packages; otherwise they won't.
51
52A path configuration file is a file whose name has the form :file:`{name}.pth`
53and exists in one of the four directories mentioned above; its contents are
54additional items (one per line) to be added to ``sys.path``.  Non-existing items
55are never added to ``sys.path``, and no check is made that the item refers to a
56directory rather than a file.  No item is added to ``sys.path`` more than
57once.  Blank lines and lines beginning with ``#`` are skipped.  Lines starting
58with ``import`` (followed by space or tab) are executed.
59
60.. index::
61   single: package
62   triple: path; configuration; file
63
64For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
65:file:`/usr/local`.  The Python X.Y library is then installed in
66:file:`/usr/local/lib/python{X.Y}`.  Suppose this has
67a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
68subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
69configuration files, :file:`foo.pth` and :file:`bar.pth`.  Assume
70:file:`foo.pth` contains the following::
71
72   # foo package configuration
73
74   foo
75   bar
76   bletch
77
78and :file:`bar.pth` contains::
79
80   # bar package configuration
81
82   bar
83
84Then the following version-specific directories are added to
85``sys.path``, in this order::
86
87   /usr/local/lib/pythonX.Y/site-packages/bar
88   /usr/local/lib/pythonX.Y/site-packages/foo
89
90Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar`
91directory precedes the :file:`foo` directory because :file:`bar.pth` comes
92alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is
93not mentioned in either path configuration file.
94
95.. index:: module: sitecustomize
96
97After these path manipulations, an attempt is made to import a module named
98:mod:`sitecustomize`, which can perform arbitrary site-specific customizations.
99It is typically created by a system administrator in the site-packages
100directory.  If this import fails with an :exc:`ImportError` exception, it is
101silently ignored.  If Python is started without output streams available, as
102with :file:`pythonw.exe` on Windows (which is used by default to start IDLE),
103attempted output from :mod:`sitecustomize` is ignored. Any exception other
104than :exc:`ImportError` causes a silent and perhaps mysterious failure of the
105process.
106
107.. index:: module: usercustomize
108
109After this, an attempt is made to import a module named :mod:`usercustomize`,
110which can perform arbitrary user-specific customizations, if
111:data:`ENABLE_USER_SITE` is true.  This file is intended to be created in the
112user site-packages directory (see below), which is part of ``sys.path`` unless
113disabled by :option:`-s`.  An :exc:`ImportError` will be silently ignored.
114
115Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are
116empty, and the path manipulations are skipped; however the import of
117:mod:`sitecustomize` and :mod:`usercustomize` is still attempted.
118
119
120.. _rlcompleter-config:
121
122Readline configuration
123----------------------
124
125On systems that support :mod:`readline`, this module will also import and
126configure the :mod:`rlcompleter` module, if Python is started in
127:ref:`interactive mode <tut-interactive>` and without the :option:`-S` option.
128The default behavior is enable tab-completion and to use
129:file:`~/.python_history` as the history save file.  To disable it, delete (or
130override) the :data:`sys.__interactivehook__` attribute in your
131:mod:`sitecustomize` or :mod:`usercustomize` module or your
132:envvar:`PYTHONSTARTUP` file.
133
134.. versionchanged:: 3.4
135   Activation of rlcompleter and history was made automatic.
136
137
138Module contents
139---------------
140
141.. data:: PREFIXES
142
143   A list of prefixes for site-packages directories.
144
145
146.. data:: ENABLE_USER_SITE
147
148   Flag showing the status of the user site-packages directory.  ``True`` means
149   that it is enabled and was added to ``sys.path``.  ``False`` means that it
150   was disabled by user request (with :option:`-s` or
151   :envvar:`PYTHONNOUSERSITE`).  ``None`` means it was disabled for security
152   reasons (mismatch between user or group id and effective id) or by an
153   administrator.
154
155
156.. data:: USER_SITE
157
158   Path to the user site-packages for the running Python.  Can be ``None`` if
159   :func:`getusersitepackages` hasn't been called yet.  Default value is
160   :file:`~/.local/lib/python{X.Y}/site-packages` for UNIX and non-framework Mac
161   OS X builds, :file:`~/Library/Python/{X.Y}/lib/python/site-packages` for Mac
162   framework builds, and :file:`{%APPDATA%}\\Python\\Python{XY}\\site-packages`
163   on Windows.  This directory is a site directory, which means that
164   :file:`.pth` files in it will be processed.
165
166
167.. data:: USER_BASE
168
169   Path to the base directory for the user site-packages.  Can be ``None`` if
170   :func:`getuserbase` hasn't been called yet.  Default value is
171   :file:`~/.local` for UNIX and Mac OS X non-framework builds,
172   :file:`~/Library/Python/{X.Y}` for Mac framework builds, and
173   :file:`{%APPDATA%}\\Python` for Windows.  This value is used by Distutils to
174   compute the installation directories for scripts, data files, Python modules,
175   etc. for the :ref:`user installation scheme <inst-alt-install-user>`.
176   See also :envvar:`PYTHONUSERBASE`.
177
178
179.. function:: main()
180
181   Adds all the standard site-specific directories to the module search
182   path.  This function is called automatically when this module is imported,
183   unless the Python interpreter was started with the :option:`-S` flag.
184
185   .. versionchanged:: 3.3
186      This function used to be called unconditionally.
187
188
189.. function:: addsitedir(sitedir, known_paths=None)
190
191   Add a directory to sys.path and process its :file:`.pth` files.  Typically
192   used in :mod:`sitecustomize` or :mod:`usercustomize` (see above).
193
194
195.. function:: getsitepackages()
196
197   Return a list containing all global site-packages directories.
198
199   .. versionadded:: 3.2
200
201
202.. function:: getuserbase()
203
204   Return the path of the user base directory, :data:`USER_BASE`.  If it is not
205   initialized yet, this function will also set it, respecting
206   :envvar:`PYTHONUSERBASE`.
207
208   .. versionadded:: 3.2
209
210
211.. function:: getusersitepackages()
212
213   Return the path of the user-specific site-packages directory,
214   :data:`USER_SITE`.  If it is not initialized yet, this function will also set
215   it, respecting :envvar:`PYTHONNOUSERSITE` and :data:`USER_BASE`.
216
217   .. versionadded:: 3.2
218
219
220The :mod:`site` module also provides a way to get the user directories from the
221command line:
222
223.. code-block:: sh
224
225   $ python3 -m site --user-site
226   /home/user/.local/lib/python3.3/site-packages
227
228.. program:: site
229
230If it is called without arguments, it will print the contents of
231:data:`sys.path` on the standard output, followed by the value of
232:data:`USER_BASE` and whether the directory exists, then the same thing for
233:data:`USER_SITE`, and finally the value of :data:`ENABLE_USER_SITE`.
234
235.. cmdoption:: --user-base
236
237   Print the path to the user base directory.
238
239.. cmdoption:: --user-site
240
241   Print the path to the user site-packages directory.
242
243If both options are given, user base and user site will be printed (always in
244this order), separated by :data:`os.pathsep`.
245
246If any option is given, the script will exit with one of these values: ``O`` if
247the user site-packages directory is enabled, ``1`` if it was disabled by the
248user, ``2`` if it is disabled for security reasons or by an administrator, and a
249value greater than 2 if there is an error.
250
251.. seealso::
252
253   :pep:`370` -- Per user site-packages directory
254