blob: e1ca160c107b8b88983546d2b006f66d99b1e9b8 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`site` --- Site-specific configuration hook
2================================================
3
4.. module:: site
Éric Araujode4f05b2011-08-06 01:51:07 +02005 :synopsis: Module responsible for site-specific configuration.
Georg Brandl116aa622007-08-15 14:28:22 +00006
Raymond Hettinger469271d2011-01-27 20:38:46 +00007**Source code:** :source:`Lib/site.py`
8
9--------------
Georg Brandl116aa622007-08-15 14:28:22 +000010
Stéphane Wirtelcbb64842019-05-17 11:55:34 +020011.. highlight:: none
Éric Araujode4f05b2011-08-06 01:51:07 +020012
Georg Brandl116aa622007-08-15 14:28:22 +000013**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
Éric Araujode4f05b2011-08-06 01:51:07 +020018Importing this module will append site-specific paths to the module search path
Éric Araujo7dc76fd2011-08-06 16:58:15 +020019and 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.
Georg Brandl116aa622007-08-15 14:28:22 +000023
Éric Araujob1734512011-04-24 03:15:32 +020024.. versionchanged:: 3.3
25 Importing the module used to trigger paths manipulation even when using
26 :option:`-S`.
27
Georg Brandl116aa622007-08-15 14:28:22 +000028.. index::
Georg Brandl116aa622007-08-15 14:28:22 +000029 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
Antoine Pitrou9e82b172014-06-12 19:41:30 -040035: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.
Georg Brandl116aa622007-08-15 14:28:22 +000039
Antoine Pitrou9e82b172014-06-12 19:41:30 -040040.. versionchanged:: 3.5
41 Support for the "site-python" directory has been removed.
Antoine Pitrou3b2f0f02013-10-25 21:39:26 +020042
Vinay Sajipabd344c2012-07-03 16:33:57 +010043If a file named "pyvenv.cfg" exists one directory above sys.executable,
44sys.prefix and sys.exec_prefix are set to that directory and
Antoine Pitrou9e82b172014-06-12 19:41:30 -040045it is also checked for site-packages (sys.base_prefix and
Vinay Sajipabd344c2012-07-03 16:33:57 +010046sys.base_exec_prefix will always be the "real" prefixes of the Python
47installation). If "pyvenv.cfg" (a bootstrap configuration file) contains
Lukas Waymannc324c742019-04-09 15:27:36 +080048the key "include-system-site-packages" set to anything other than "true"
49(case-insensitive), the system-level prefixes will not be
50searched for site-packages; otherwise they will.
Vinay Sajipabd344c2012-07-03 16:33:57 +010051
Serhiy Storchakaddb961d2018-10-26 09:00:49 +030052.. index::
Serhiy Storchaka913876d2018-10-28 13:41:26 +020053 single: # (hash); comment
Serhiy Storchakaddb961d2018-10-26 09:00:49 +030054 statement: import
55
Éric Araujode4f05b2011-08-06 01:51:07 +020056A path configuration file is a file whose name has the form :file:`{name}.pth`
Georg Brandl116aa622007-08-15 14:28:22 +000057and exists in one of the four directories mentioned above; its contents are
58additional items (one per line) to be added to ``sys.path``. Non-existing items
Éric Araujode4f05b2011-08-06 01:51:07 +020059are never added to ``sys.path``, and no check is made that the item refers to a
60directory rather than a file. No item is added to ``sys.path`` more than
Georg Brandl116aa622007-08-15 14:28:22 +000061once. Blank lines and lines beginning with ``#`` are skipped. Lines starting
62with ``import`` (followed by space or tab) are executed.
63
native-apif9b58402019-09-11 16:21:04 +030064.. note::
65
66 An executable line in a :file:`.pth` file is run at every Python startup,
67 regardless of whether a particular module is actually going to be used.
68 Its impact should thus be kept to a minimum.
69 The primary intended purpose of executable lines is to make the
70 corresponding module(s) importable
71 (load 3rd-party import hooks, adjust :envvar:`PATH` etc).
72 Any other initialization is supposed to be done upon a module's
73 actual import, if and when it happens.
74 Limiting a code chunk to a single line is a deliberate measure
75 to discourage putting anything more complex here.
76
Georg Brandl116aa622007-08-15 14:28:22 +000077.. index::
78 single: package
79 triple: path; configuration; file
80
81For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
82:file:`/usr/local`. The Python X.Y library is then installed in
Éric Araujode4f05b2011-08-06 01:51:07 +020083:file:`/usr/local/lib/python{X.Y}`. Suppose this has
Georg Brandl116aa622007-08-15 14:28:22 +000084a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
85subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
86configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume
87:file:`foo.pth` contains the following::
88
89 # foo package configuration
90
91 foo
92 bar
93 bletch
94
95and :file:`bar.pth` contains::
96
97 # bar package configuration
98
99 bar
100
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000101Then the following version-specific directories are added to
102``sys.path``, in this order::
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000104 /usr/local/lib/pythonX.Y/site-packages/bar
105 /usr/local/lib/pythonX.Y/site-packages/foo
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar`
108directory precedes the :file:`foo` directory because :file:`bar.pth` comes
109alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is
110not mentioned in either path configuration file.
111
112.. index:: module: sitecustomize
113
114After these path manipulations, an attempt is made to import a module named
115:mod:`sitecustomize`, which can perform arbitrary site-specific customizations.
Éric Araujode4f05b2011-08-06 01:51:07 +0200116It is typically created by a system administrator in the site-packages
Xiang Zhanga29ddf42018-01-31 23:50:50 +0800117directory. If this import fails with an :exc:`ImportError` or its subclass
118exception, and the exception's :attr:`name` attribute equals to ``'sitecustomize'``,
119it is silently ignored. If Python is started without output streams available, as
Terry Jan Reedy43e7cd32014-04-29 00:31:53 -0400120with :file:`pythonw.exe` on Windows (which is used by default to start IDLE),
Xiang Zhanga29ddf42018-01-31 23:50:50 +0800121attempted output from :mod:`sitecustomize` is ignored. Any other exception
122causes a silent and perhaps mysterious failure of the process.
Georg Brandl116aa622007-08-15 14:28:22 +0000123
Éric Araujode4f05b2011-08-06 01:51:07 +0200124.. index:: module: usercustomize
125
126After this, an attempt is made to import a module named :mod:`usercustomize`,
127which can perform arbitrary user-specific customizations, if
128:data:`ENABLE_USER_SITE` is true. This file is intended to be created in the
129user site-packages directory (see below), which is part of ``sys.path`` unless
Xiang Zhanga29ddf42018-01-31 23:50:50 +0800130disabled by :option:`-s`. If this import fails with an :exc:`ImportError` or
131its subclass exception, and the exception's :attr:`name` attribute equals to
132``'usercustomize'``, it is silently ignored.
Georg Brandl116aa622007-08-15 14:28:22 +0000133
134Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are
135empty, and the path manipulations are skipped; however the import of
Éric Araujode4f05b2011-08-06 01:51:07 +0200136:mod:`sitecustomize` and :mod:`usercustomize` is still attempted.
Georg Brandl116aa622007-08-15 14:28:22 +0000137
Christian Heimes8dc226f2008-05-06 23:45:46 +0000138
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200139.. _rlcompleter-config:
140
141Readline configuration
142----------------------
143
144On systems that support :mod:`readline`, this module will also import and
145configure the :mod:`rlcompleter` module, if Python is started in
146:ref:`interactive mode <tut-interactive>` and without the :option:`-S` option.
147The default behavior is enable tab-completion and to use
R David Murray4e40cec2014-03-09 12:03:30 -0400148:file:`~/.python_history` as the history save file. To disable it, delete (or
149override) the :data:`sys.__interactivehook__` attribute in your
150:mod:`sitecustomize` or :mod:`usercustomize` module or your
151:envvar:`PYTHONSTARTUP` file.
152
153.. versionchanged:: 3.4
154 Activation of rlcompleter and history was made automatic.
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200155
156
157Module contents
158---------------
159
Christian Heimes8dc226f2008-05-06 23:45:46 +0000160.. data:: PREFIXES
161
Éric Araujode4f05b2011-08-06 01:51:07 +0200162 A list of prefixes for site-packages directories.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000163
Christian Heimes8dc226f2008-05-06 23:45:46 +0000164
165.. data:: ENABLE_USER_SITE
166
Éric Araujode4f05b2011-08-06 01:51:07 +0200167 Flag showing the status of the user site-packages directory. ``True`` means
168 that it is enabled and was added to ``sys.path``. ``False`` means that it
169 was disabled by user request (with :option:`-s` or
170 :envvar:`PYTHONNOUSERSITE`). ``None`` means it was disabled for security
171 reasons (mismatch between user or group id and effective id) or by an
172 administrator.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000173
Christian Heimes8dc226f2008-05-06 23:45:46 +0000174
175.. data:: USER_SITE
176
Éric Araujode4f05b2011-08-06 01:51:07 +0200177 Path to the user site-packages for the running Python. Can be ``None`` if
178 :func:`getusersitepackages` hasn't been called yet. Default value is
179 :file:`~/.local/lib/python{X.Y}/site-packages` for UNIX and non-framework Mac
180 OS X builds, :file:`~/Library/Python/{X.Y}/lib/python/site-packages` for Mac
181 framework builds, and :file:`{%APPDATA%}\\Python\\Python{XY}\\site-packages`
182 on Windows. This directory is a site directory, which means that
183 :file:`.pth` files in it will be processed.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000184
Christian Heimes8dc226f2008-05-06 23:45:46 +0000185
186.. data:: USER_BASE
187
Éric Araujode4f05b2011-08-06 01:51:07 +0200188 Path to the base directory for the user site-packages. Can be ``None`` if
189 :func:`getuserbase` hasn't been called yet. Default value is
190 :file:`~/.local` for UNIX and Mac OS X non-framework builds,
191 :file:`~/Library/Python/{X.Y}` for Mac framework builds, and
Éric Araujo859aad62012-06-24 00:07:41 -0400192 :file:`{%APPDATA%}\\Python` for Windows. This value is used by Distutils to
Éric Araujode4f05b2011-08-06 01:51:07 +0200193 compute the installation directories for scripts, data files, Python modules,
Éric Araujo859aad62012-06-24 00:07:41 -0400194 etc. for the :ref:`user installation scheme <inst-alt-install-user>`.
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200195 See also :envvar:`PYTHONUSERBASE`.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000196
Christian Heimes8dc226f2008-05-06 23:45:46 +0000197
Éric Araujoc09fca62011-03-23 02:06:24 +0100198.. function:: main()
199
200 Adds all the standard site-specific directories to the module search
201 path. This function is called automatically when this module is imported,
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200202 unless the Python interpreter was started with the :option:`-S` flag.
Éric Araujoc09fca62011-03-23 02:06:24 +0100203
Éric Araujo17b60f02011-05-06 18:50:08 +0200204 .. versionchanged:: 3.3
Donald Stufft8b852f12014-05-20 12:58:38 -0400205 This function used to be called unconditionally.
Éric Araujo17b60f02011-05-06 18:50:08 +0200206
207
Christian Heimes8dc226f2008-05-06 23:45:46 +0000208.. function:: addsitedir(sitedir, known_paths=None)
209
Éric Araujo6ef038e2011-08-06 16:30:42 +0200210 Add a directory to sys.path and process its :file:`.pth` files. Typically
211 used in :mod:`sitecustomize` or :mod:`usercustomize` (see above).
Éric Araujode4f05b2011-08-06 01:51:07 +0200212
Christian Heimes8dc226f2008-05-06 23:45:46 +0000213
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000214.. function:: getsitepackages()
215
Antoine Pitrou9e82b172014-06-12 19:41:30 -0400216 Return a list containing all global site-packages directories.
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000217
Mark Dickinson574b1d62009-10-01 20:20:09 +0000218 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000219
Éric Araujode4f05b2011-08-06 01:51:07 +0200220
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000221.. function:: getuserbase()
222
Éric Araujode4f05b2011-08-06 01:51:07 +0200223 Return the path of the user base directory, :data:`USER_BASE`. If it is not
224 initialized yet, this function will also set it, respecting
225 :envvar:`PYTHONUSERBASE`.
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000226
Mark Dickinson574b1d62009-10-01 20:20:09 +0000227 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000228
Éric Araujode4f05b2011-08-06 01:51:07 +0200229
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000230.. function:: getusersitepackages()
231
Éric Araujode4f05b2011-08-06 01:51:07 +0200232 Return the path of the user-specific site-packages directory,
233 :data:`USER_SITE`. If it is not initialized yet, this function will also set
234 it, respecting :envvar:`PYTHONNOUSERSITE` and :data:`USER_BASE`.
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000235
Mark Dickinson574b1d62009-10-01 20:20:09 +0000236 .. versionadded:: 3.2
Christian Heimes8dc226f2008-05-06 23:45:46 +0000237
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000238
Éric Araujode4f05b2011-08-06 01:51:07 +0200239The :mod:`site` module also provides a way to get the user directories from the
240command line:
241
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300242.. code-block:: shell-session
Éric Araujode4f05b2011-08-06 01:51:07 +0200243
244 $ python3 -m site --user-site
245 /home/user/.local/lib/python3.3/site-packages
246
247.. program:: site
248
249If it is called without arguments, it will print the contents of
250:data:`sys.path` on the standard output, followed by the value of
251:data:`USER_BASE` and whether the directory exists, then the same thing for
252:data:`USER_SITE`, and finally the value of :data:`ENABLE_USER_SITE`.
253
254.. cmdoption:: --user-base
255
256 Print the path to the user base directory.
257
258.. cmdoption:: --user-site
259
260 Print the path to the user site-packages directory.
261
262If both options are given, user base and user site will be printed (always in
263this order), separated by :data:`os.pathsep`.
264
Mariatta95d34c22018-07-24 09:14:20 -0700265If any option is given, the script will exit with one of these values: ``0`` if
Éric Araujode4f05b2011-08-06 01:51:07 +0200266the user site-packages directory is enabled, ``1`` if it was disabled by the
267user, ``2`` if it is disabled for security reasons or by an administrator, and a
268value greater than 2 if there is an error.
269
270.. seealso::
271
272 :pep:`370` -- Per user site-packages directory