blob: b9878973c72522c0c6a38e6b45c7a1fd09250448 [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
36:file:`lib/python|version|/site-packages` and then :file:`lib/site-python` (on
37Unix 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
Éric Araujode4f05b2011-08-06 01:51:07 +020041A path configuration file is a file whose name has the form :file:`{name}.pth`
Georg Brandl116aa622007-08-15 14:28:22 +000042and exists in one of the four directories mentioned above; its contents are
43additional items (one per line) to be added to ``sys.path``. Non-existing items
Éric Araujode4f05b2011-08-06 01:51:07 +020044are never added to ``sys.path``, and no check is made that the item refers to a
45directory rather than a file. No item is added to ``sys.path`` more than
Georg Brandl116aa622007-08-15 14:28:22 +000046once. Blank lines and lines beginning with ``#`` are skipped. Lines starting
47with ``import`` (followed by space or tab) are executed.
48
Georg Brandl116aa622007-08-15 14:28:22 +000049.. index::
50 single: package
51 triple: path; configuration; file
52
53For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
54:file:`/usr/local`. The Python X.Y library is then installed in
Éric Araujode4f05b2011-08-06 01:51:07 +020055:file:`/usr/local/lib/python{X.Y}`. Suppose this has
Georg Brandl116aa622007-08-15 14:28:22 +000056a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
57subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
58configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume
59:file:`foo.pth` contains the following::
60
61 # foo package configuration
62
63 foo
64 bar
65 bletch
66
67and :file:`bar.pth` contains::
68
69 # bar package configuration
70
71 bar
72
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000073Then the following version-specific directories are added to
74``sys.path``, in this order::
Georg Brandl116aa622007-08-15 14:28:22 +000075
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000076 /usr/local/lib/pythonX.Y/site-packages/bar
77 /usr/local/lib/pythonX.Y/site-packages/foo
Georg Brandl116aa622007-08-15 14:28:22 +000078
79Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar`
80directory precedes the :file:`foo` directory because :file:`bar.pth` comes
81alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is
82not mentioned in either path configuration file.
83
84.. index:: module: sitecustomize
85
86After these path manipulations, an attempt is made to import a module named
87:mod:`sitecustomize`, which can perform arbitrary site-specific customizations.
Éric Araujode4f05b2011-08-06 01:51:07 +020088It is typically created by a system administrator in the site-packages
89directory. If this import fails with an :exc:`ImportError` exception, it is
90silently ignored.
Georg Brandl116aa622007-08-15 14:28:22 +000091
Éric Araujode4f05b2011-08-06 01:51:07 +020092.. index:: module: usercustomize
93
94After this, an attempt is made to import a module named :mod:`usercustomize`,
95which can perform arbitrary user-specific customizations, if
96:data:`ENABLE_USER_SITE` is true. This file is intended to be created in the
97user site-packages directory (see below), which is part of ``sys.path`` unless
98disabled by :option:`-s`. An :exc:`ImportError` will be silently ignored.
Georg Brandl116aa622007-08-15 14:28:22 +000099
100Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are
101empty, and the path manipulations are skipped; however the import of
Éric Araujode4f05b2011-08-06 01:51:07 +0200102:mod:`sitecustomize` and :mod:`usercustomize` is still attempted.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Christian Heimes8dc226f2008-05-06 23:45:46 +0000104
105.. data:: PREFIXES
106
Éric Araujode4f05b2011-08-06 01:51:07 +0200107 A list of prefixes for site-packages directories.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000108
Christian Heimes8dc226f2008-05-06 23:45:46 +0000109
110.. data:: ENABLE_USER_SITE
111
Éric Araujode4f05b2011-08-06 01:51:07 +0200112 Flag showing the status of the user site-packages directory. ``True`` means
113 that it is enabled and was added to ``sys.path``. ``False`` means that it
114 was disabled by user request (with :option:`-s` or
115 :envvar:`PYTHONNOUSERSITE`). ``None`` means it was disabled for security
116 reasons (mismatch between user or group id and effective id) or by an
117 administrator.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000118
Christian Heimes8dc226f2008-05-06 23:45:46 +0000119
120.. data:: USER_SITE
121
Éric Araujode4f05b2011-08-06 01:51:07 +0200122 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.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000129
Christian Heimes8dc226f2008-05-06 23:45:46 +0000130
131.. data:: USER_BASE
132
Éric Araujode4f05b2011-08-06 01:51:07 +0200133 Path to the base directory for the user site-packages. Can be ``None`` if
134 :func:`getuserbase` hasn't been called yet. Default value is
135 :file:`~/.local` for UNIX and Mac OS X non-framework builds,
136 :file:`~/Library/Python/{X.Y}` for Mac framework builds, and
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200137 :file:`{%APPDATA%}\\Python` for Windows. This value is used by Packaging to
Éric Araujode4f05b2011-08-06 01:51:07 +0200138 compute the installation directories for scripts, data files, Python modules,
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200139 etc. for the :ref:`user installation scheme <packaging-alt-install-user>`.
140 See also :envvar:`PYTHONUSERBASE`.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000141
Christian Heimes8dc226f2008-05-06 23:45:46 +0000142
Éric Araujoc09fca62011-03-23 02:06:24 +0100143.. function:: main()
144
145 Adds all the standard site-specific directories to the module search
146 path. This function is called automatically when this module is imported,
147 unless the :program:`python` interpreter was started with the :option:`-S`
148 flag.
149
Éric Araujo17b60f02011-05-06 18:50:08 +0200150 .. versionchanged:: 3.3
151 This function used to be called unconditionnally.
152
153
Christian Heimes8dc226f2008-05-06 23:45:46 +0000154.. function:: addsitedir(sitedir, known_paths=None)
155
Éric Araujo6ef038e2011-08-06 16:30:42 +0200156 Add a directory to sys.path and process its :file:`.pth` files. Typically
157 used in :mod:`sitecustomize` or :mod:`usercustomize` (see above).
Éric Araujode4f05b2011-08-06 01:51:07 +0200158
Christian Heimes8dc226f2008-05-06 23:45:46 +0000159
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000160.. function:: getsitepackages()
161
Éric Araujode4f05b2011-08-06 01:51:07 +0200162 Return a list containing all global site-packages directories (and possibly
163 site-python).
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000164
Mark Dickinson574b1d62009-10-01 20:20:09 +0000165 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000166
Éric Araujode4f05b2011-08-06 01:51:07 +0200167
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000168.. function:: getuserbase()
169
Éric Araujode4f05b2011-08-06 01:51:07 +0200170 Return the path of the user base directory, :data:`USER_BASE`. If it is not
171 initialized yet, this function will also set it, respecting
172 :envvar:`PYTHONUSERBASE`.
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000173
Mark Dickinson574b1d62009-10-01 20:20:09 +0000174 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000175
Éric Araujode4f05b2011-08-06 01:51:07 +0200176
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000177.. function:: getusersitepackages()
178
Éric Araujode4f05b2011-08-06 01:51:07 +0200179 Return the path of the user-specific site-packages directory,
180 :data:`USER_SITE`. If it is not initialized yet, this function will also set
181 it, respecting :envvar:`PYTHONNOUSERSITE` and :data:`USER_BASE`.
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000182
Mark Dickinson574b1d62009-10-01 20:20:09 +0000183 .. versionadded:: 3.2
Christian Heimes8dc226f2008-05-06 23:45:46 +0000184
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000185
Éric Araujode4f05b2011-08-06 01:51:07 +0200186The :mod:`site` module also provides a way to get the user directories from the
187command line:
188
189.. code-block:: sh
190
191 $ python3 -m site --user-site
192 /home/user/.local/lib/python3.3/site-packages
193
194.. program:: site
195
196If it is called without arguments, it will print the contents of
197:data:`sys.path` on the standard output, followed by the value of
198:data:`USER_BASE` and whether the directory exists, then the same thing for
199:data:`USER_SITE`, and finally the value of :data:`ENABLE_USER_SITE`.
200
201.. cmdoption:: --user-base
202
203 Print the path to the user base directory.
204
205.. cmdoption:: --user-site
206
207 Print the path to the user site-packages directory.
208
209If both options are given, user base and user site will be printed (always in
210this order), separated by :data:`os.pathsep`.
211
212If any option is given, the script will exit with one of these values: ``O`` if
213the user site-packages directory is enabled, ``1`` if it was disabled by the
214user, ``2`` if it is disabled for security reasons or by an administrator, and a
215value greater than 2 if there is an error.
216
217.. seealso::
218
219 :pep:`370` -- Per user site-packages directory