blob: e57b8ccc2d8bfbc403df7c8835a8aa1718cb0005 [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
Éric Araujode4f05b2011-08-06 01:51:07 +020011.. highlightlang:: none
12
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::
29 pair: site-python; directory
30 pair: site-packages; directory
31
32It starts by constructing up to four directories from a head and a tail part.
33For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads
34are skipped. For the tail part, it uses the empty string and then
35:file:`lib/site-packages` (on Windows) or
Ezio Melotti26e58782012-09-17 08:59:36 +020036:file:`lib/python{X.Y}/site-packages` and then :file:`lib/site-python` (on
Georg Brandl116aa622007-08-15 14:28:22 +000037Unix and Macintosh). For each of the distinct head-tail combinations, it sees
38if it refers to an existing directory, and if so, adds it to ``sys.path`` and
39also inspects the newly added path for configuration files.
40
Antoine Pitrou3b2f0f02013-10-25 21:39:26 +020041.. deprecated:: 3.4
42 Support for the "site-python" directory will be removed in 3.5.
43
Vinay Sajipabd344c2012-07-03 16:33:57 +010044If a file named "pyvenv.cfg" exists one directory above sys.executable,
45sys.prefix and sys.exec_prefix are set to that directory and
46it is also checked for site-packages and site-python (sys.base_prefix and
47sys.base_exec_prefix will always be the "real" prefixes of the Python
48installation). If "pyvenv.cfg" (a bootstrap configuration file) contains
49the key "include-system-site-packages" set to anything other than "false"
50(case-insensitive), the system-level prefixes will still also be
51searched for site-packages; otherwise they won't.
52
Éric Araujode4f05b2011-08-06 01:51:07 +020053A path configuration file is a file whose name has the form :file:`{name}.pth`
Georg Brandl116aa622007-08-15 14:28:22 +000054and exists in one of the four directories mentioned above; its contents are
55additional items (one per line) to be added to ``sys.path``. Non-existing items
Éric Araujode4f05b2011-08-06 01:51:07 +020056are never added to ``sys.path``, and no check is made that the item refers to a
57directory rather than a file. No item is added to ``sys.path`` more than
Georg Brandl116aa622007-08-15 14:28:22 +000058once. Blank lines and lines beginning with ``#`` are skipped. Lines starting
59with ``import`` (followed by space or tab) are executed.
60
Georg Brandl116aa622007-08-15 14:28:22 +000061.. index::
62 single: package
63 triple: path; configuration; file
64
65For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
66:file:`/usr/local`. The Python X.Y library is then installed in
Éric Araujode4f05b2011-08-06 01:51:07 +020067:file:`/usr/local/lib/python{X.Y}`. Suppose this has
Georg Brandl116aa622007-08-15 14:28:22 +000068a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
69subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
70configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume
71:file:`foo.pth` contains the following::
72
73 # foo package configuration
74
75 foo
76 bar
77 bletch
78
79and :file:`bar.pth` contains::
80
81 # bar package configuration
82
83 bar
84
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000085Then the following version-specific directories are added to
86``sys.path``, in this order::
Georg Brandl116aa622007-08-15 14:28:22 +000087
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000088 /usr/local/lib/pythonX.Y/site-packages/bar
89 /usr/local/lib/pythonX.Y/site-packages/foo
Georg Brandl116aa622007-08-15 14:28:22 +000090
91Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar`
92directory precedes the :file:`foo` directory because :file:`bar.pth` comes
93alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is
94not mentioned in either path configuration file.
95
96.. index:: module: sitecustomize
97
98After these path manipulations, an attempt is made to import a module named
99:mod:`sitecustomize`, which can perform arbitrary site-specific customizations.
Éric Araujode4f05b2011-08-06 01:51:07 +0200100It is typically created by a system administrator in the site-packages
101directory. If this import fails with an :exc:`ImportError` exception, it is
Terry Jan Reedy43e7cd32014-04-29 00:31:53 -0400102silently ignored. If Python is started without output streams available, as
103with :file:`pythonw.exe` on Windows (which is used by default to start IDLE),
104attempted output from :mod:`sitecustomize` is ignored. Any exception other
105than :exc:`ImportError` causes a silent and perhaps mysterious failure of the
106process.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
Éric Araujode4f05b2011-08-06 01:51:07 +0200108.. index:: module: usercustomize
109
110After this, an attempt is made to import a module named :mod:`usercustomize`,
111which can perform arbitrary user-specific customizations, if
112:data:`ENABLE_USER_SITE` is true. This file is intended to be created in the
113user site-packages directory (see below), which is part of ``sys.path`` unless
114disabled by :option:`-s`. An :exc:`ImportError` will be silently ignored.
Georg Brandl116aa622007-08-15 14:28:22 +0000115
116Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are
117empty, and the path manipulations are skipped; however the import of
Éric Araujode4f05b2011-08-06 01:51:07 +0200118:mod:`sitecustomize` and :mod:`usercustomize` is still attempted.
Georg Brandl116aa622007-08-15 14:28:22 +0000119
Christian Heimes8dc226f2008-05-06 23:45:46 +0000120
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200121.. _rlcompleter-config:
122
123Readline configuration
124----------------------
125
126On systems that support :mod:`readline`, this module will also import and
127configure the :mod:`rlcompleter` module, if Python is started in
128:ref:`interactive mode <tut-interactive>` and without the :option:`-S` option.
129The default behavior is enable tab-completion and to use
Larry Hastings3732ed22014-03-15 21:13:56 -0700130:file:`~/.python_history` as the history save file. To disable it, delete (or
131override) the :data:`sys.__interactivehook__` attribute in your
132:mod:`sitecustomize` or :mod:`usercustomize` module or your
133:envvar:`PYTHONSTARTUP` file.
134
135.. versionchanged:: 3.4
136 Activation of rlcompleter and history was made automatic.
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200137
138
139Module contents
140---------------
141
Christian Heimes8dc226f2008-05-06 23:45:46 +0000142.. data:: PREFIXES
143
Éric Araujode4f05b2011-08-06 01:51:07 +0200144 A list of prefixes for site-packages directories.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000145
Christian Heimes8dc226f2008-05-06 23:45:46 +0000146
147.. data:: ENABLE_USER_SITE
148
Éric Araujode4f05b2011-08-06 01:51:07 +0200149 Flag showing the status of the user site-packages directory. ``True`` means
150 that it is enabled and was added to ``sys.path``. ``False`` means that it
151 was disabled by user request (with :option:`-s` or
152 :envvar:`PYTHONNOUSERSITE`). ``None`` means it was disabled for security
153 reasons (mismatch between user or group id and effective id) or by an
154 administrator.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000155
Christian Heimes8dc226f2008-05-06 23:45:46 +0000156
157.. data:: USER_SITE
158
Éric Araujode4f05b2011-08-06 01:51:07 +0200159 Path to the user site-packages for the running Python. Can be ``None`` if
160 :func:`getusersitepackages` hasn't been called yet. Default value is
161 :file:`~/.local/lib/python{X.Y}/site-packages` for UNIX and non-framework Mac
162 OS X builds, :file:`~/Library/Python/{X.Y}/lib/python/site-packages` for Mac
163 framework builds, and :file:`{%APPDATA%}\\Python\\Python{XY}\\site-packages`
164 on Windows. This directory is a site directory, which means that
165 :file:`.pth` files in it will be processed.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000166
Christian Heimes8dc226f2008-05-06 23:45:46 +0000167
168.. data:: USER_BASE
169
Éric Araujode4f05b2011-08-06 01:51:07 +0200170 Path to the base directory for the user site-packages. Can be ``None`` if
171 :func:`getuserbase` hasn't been called yet. Default value is
172 :file:`~/.local` for UNIX and Mac OS X non-framework builds,
173 :file:`~/Library/Python/{X.Y}` for Mac framework builds, and
Éric Araujo859aad62012-06-24 00:07:41 -0400174 :file:`{%APPDATA%}\\Python` for Windows. This value is used by Distutils to
Éric Araujode4f05b2011-08-06 01:51:07 +0200175 compute the installation directories for scripts, data files, Python modules,
Éric Araujo859aad62012-06-24 00:07:41 -0400176 etc. for the :ref:`user installation scheme <inst-alt-install-user>`.
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200177 See also :envvar:`PYTHONUSERBASE`.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000178
Christian Heimes8dc226f2008-05-06 23:45:46 +0000179
Éric Araujoc09fca62011-03-23 02:06:24 +0100180.. function:: main()
181
182 Adds all the standard site-specific directories to the module search
183 path. This function is called automatically when this module is imported,
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200184 unless the Python interpreter was started with the :option:`-S` flag.
Éric Araujoc09fca62011-03-23 02:06:24 +0100185
Éric Araujo17b60f02011-05-06 18:50:08 +0200186 .. versionchanged:: 3.3
187 This function used to be called unconditionnally.
188
189
Christian Heimes8dc226f2008-05-06 23:45:46 +0000190.. function:: addsitedir(sitedir, known_paths=None)
191
Éric Araujo6ef038e2011-08-06 16:30:42 +0200192 Add a directory to sys.path and process its :file:`.pth` files. Typically
193 used in :mod:`sitecustomize` or :mod:`usercustomize` (see above).
Éric Araujode4f05b2011-08-06 01:51:07 +0200194
Christian Heimes8dc226f2008-05-06 23:45:46 +0000195
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000196.. function:: getsitepackages()
197
Éric Araujode4f05b2011-08-06 01:51:07 +0200198 Return a list containing all global site-packages directories (and possibly
199 site-python).
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000200
Mark Dickinson574b1d62009-10-01 20:20:09 +0000201 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000202
Éric Araujode4f05b2011-08-06 01:51:07 +0200203
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000204.. function:: getuserbase()
205
Éric Araujode4f05b2011-08-06 01:51:07 +0200206 Return the path of the user base directory, :data:`USER_BASE`. If it is not
207 initialized yet, this function will also set it, respecting
208 :envvar:`PYTHONUSERBASE`.
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000209
Mark Dickinson574b1d62009-10-01 20:20:09 +0000210 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000211
Éric Araujode4f05b2011-08-06 01:51:07 +0200212
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000213.. function:: getusersitepackages()
214
Éric Araujode4f05b2011-08-06 01:51:07 +0200215 Return the path of the user-specific site-packages directory,
216 :data:`USER_SITE`. If it is not initialized yet, this function will also set
217 it, respecting :envvar:`PYTHONNOUSERSITE` and :data:`USER_BASE`.
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000218
Mark Dickinson574b1d62009-10-01 20:20:09 +0000219 .. versionadded:: 3.2
Christian Heimes8dc226f2008-05-06 23:45:46 +0000220
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000221
Éric Araujode4f05b2011-08-06 01:51:07 +0200222The :mod:`site` module also provides a way to get the user directories from the
223command line:
224
225.. code-block:: sh
226
227 $ python3 -m site --user-site
228 /home/user/.local/lib/python3.3/site-packages
229
230.. program:: site
231
232If it is called without arguments, it will print the contents of
233:data:`sys.path` on the standard output, followed by the value of
234:data:`USER_BASE` and whether the directory exists, then the same thing for
235:data:`USER_SITE`, and finally the value of :data:`ENABLE_USER_SITE`.
236
237.. cmdoption:: --user-base
238
239 Print the path to the user base directory.
240
241.. cmdoption:: --user-site
242
243 Print the path to the user site-packages directory.
244
245If both options are given, user base and user site will be printed (always in
246this order), separated by :data:`os.pathsep`.
247
248If any option is given, the script will exit with one of these values: ``O`` if
249the user site-packages directory is enabled, ``1`` if it was disabled by the
250user, ``2`` if it is disabled for security reasons or by an administrator, and a
251value greater than 2 if there is an error.
252
253.. seealso::
254
255 :pep:`370` -- Per user site-packages directory