blob: 36b80c379cf7a575a96e229d08cefb84a3f1b8df [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
Vinay Sajipabd344c2012-07-03 16:33:57 +010041If a file named "pyvenv.cfg" exists one directory above sys.executable,
42sys.prefix and sys.exec_prefix are set to that directory and
43it is also checked for site-packages and site-python (sys.base_prefix and
44sys.base_exec_prefix will always be the "real" prefixes of the Python
45installation). If "pyvenv.cfg" (a bootstrap configuration file) contains
46the key "include-system-site-packages" set to anything other than "false"
47(case-insensitive), the system-level prefixes will still also be
48searched for site-packages; otherwise they won't.
49
Éric Araujode4f05b2011-08-06 01:51:07 +020050A path configuration file is a file whose name has the form :file:`{name}.pth`
Georg Brandl116aa622007-08-15 14:28:22 +000051and exists in one of the four directories mentioned above; its contents are
52additional items (one per line) to be added to ``sys.path``. Non-existing items
Éric Araujode4f05b2011-08-06 01:51:07 +020053are never added to ``sys.path``, and no check is made that the item refers to a
54directory rather than a file. No item is added to ``sys.path`` more than
Georg Brandl116aa622007-08-15 14:28:22 +000055once. Blank lines and lines beginning with ``#`` are skipped. Lines starting
56with ``import`` (followed by space or tab) are executed.
57
Georg Brandl116aa622007-08-15 14:28:22 +000058.. index::
59 single: package
60 triple: path; configuration; file
61
62For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
63:file:`/usr/local`. The Python X.Y library is then installed in
Éric Araujode4f05b2011-08-06 01:51:07 +020064:file:`/usr/local/lib/python{X.Y}`. Suppose this has
Georg Brandl116aa622007-08-15 14:28:22 +000065a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
66subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
67configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume
68:file:`foo.pth` contains the following::
69
70 # foo package configuration
71
72 foo
73 bar
74 bletch
75
76and :file:`bar.pth` contains::
77
78 # bar package configuration
79
80 bar
81
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000082Then the following version-specific directories are added to
83``sys.path``, in this order::
Georg Brandl116aa622007-08-15 14:28:22 +000084
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000085 /usr/local/lib/pythonX.Y/site-packages/bar
86 /usr/local/lib/pythonX.Y/site-packages/foo
Georg Brandl116aa622007-08-15 14:28:22 +000087
88Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar`
89directory precedes the :file:`foo` directory because :file:`bar.pth` comes
90alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is
91not mentioned in either path configuration file.
92
93.. index:: module: sitecustomize
94
95After these path manipulations, an attempt is made to import a module named
96:mod:`sitecustomize`, which can perform arbitrary site-specific customizations.
Éric Araujode4f05b2011-08-06 01:51:07 +020097It is typically created by a system administrator in the site-packages
98directory. If this import fails with an :exc:`ImportError` exception, it is
99silently ignored.
Georg Brandl116aa622007-08-15 14:28:22 +0000100
Éric Araujode4f05b2011-08-06 01:51:07 +0200101.. index:: module: usercustomize
102
103After this, an attempt is made to import a module named :mod:`usercustomize`,
104which can perform arbitrary user-specific customizations, if
105:data:`ENABLE_USER_SITE` is true. This file is intended to be created in the
106user site-packages directory (see below), which is part of ``sys.path`` unless
107disabled by :option:`-s`. An :exc:`ImportError` will be silently ignored.
Georg Brandl116aa622007-08-15 14:28:22 +0000108
109Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are
110empty, and the path manipulations are skipped; however the import of
Éric Araujode4f05b2011-08-06 01:51:07 +0200111:mod:`sitecustomize` and :mod:`usercustomize` is still attempted.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
Christian Heimes8dc226f2008-05-06 23:45:46 +0000113
114.. data:: PREFIXES
115
Éric Araujode4f05b2011-08-06 01:51:07 +0200116 A list of prefixes for site-packages directories.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000117
Christian Heimes8dc226f2008-05-06 23:45:46 +0000118
119.. data:: ENABLE_USER_SITE
120
Éric Araujode4f05b2011-08-06 01:51:07 +0200121 Flag showing the status of the user site-packages directory. ``True`` means
122 that it is enabled and was added to ``sys.path``. ``False`` means that it
123 was disabled by user request (with :option:`-s` or
124 :envvar:`PYTHONNOUSERSITE`). ``None`` means it was disabled for security
125 reasons (mismatch between user or group id and effective id) or by an
126 administrator.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000127
Christian Heimes8dc226f2008-05-06 23:45:46 +0000128
129.. data:: USER_SITE
130
Éric Araujode4f05b2011-08-06 01:51:07 +0200131 Path to the user site-packages for the running Python. Can be ``None`` if
132 :func:`getusersitepackages` hasn't been called yet. Default value is
133 :file:`~/.local/lib/python{X.Y}/site-packages` for UNIX and non-framework Mac
134 OS X builds, :file:`~/Library/Python/{X.Y}/lib/python/site-packages` for Mac
135 framework builds, and :file:`{%APPDATA%}\\Python\\Python{XY}\\site-packages`
136 on Windows. This directory is a site directory, which means that
137 :file:`.pth` files in it will be processed.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000138
Christian Heimes8dc226f2008-05-06 23:45:46 +0000139
140.. data:: USER_BASE
141
Éric Araujode4f05b2011-08-06 01:51:07 +0200142 Path to the base directory for the user site-packages. Can be ``None`` if
143 :func:`getuserbase` hasn't been called yet. Default value is
144 :file:`~/.local` for UNIX and Mac OS X non-framework builds,
145 :file:`~/Library/Python/{X.Y}` for Mac framework builds, and
Éric Araujo859aad62012-06-24 00:07:41 -0400146 :file:`{%APPDATA%}\\Python` for Windows. This value is used by Distutils to
Éric Araujode4f05b2011-08-06 01:51:07 +0200147 compute the installation directories for scripts, data files, Python modules,
Éric Araujo859aad62012-06-24 00:07:41 -0400148 etc. for the :ref:`user installation scheme <inst-alt-install-user>`.
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200149 See also :envvar:`PYTHONUSERBASE`.
Christian Heimes8dc226f2008-05-06 23:45:46 +0000150
Christian Heimes8dc226f2008-05-06 23:45:46 +0000151
Éric Araujoc09fca62011-03-23 02:06:24 +0100152.. function:: main()
153
154 Adds all the standard site-specific directories to the module search
155 path. This function is called automatically when this module is imported,
156 unless the :program:`python` interpreter was started with the :option:`-S`
157 flag.
158
Éric Araujo17b60f02011-05-06 18:50:08 +0200159 .. versionchanged:: 3.3
160 This function used to be called unconditionnally.
161
162
Christian Heimes8dc226f2008-05-06 23:45:46 +0000163.. function:: addsitedir(sitedir, known_paths=None)
164
Éric Araujo6ef038e2011-08-06 16:30:42 +0200165 Add a directory to sys.path and process its :file:`.pth` files. Typically
166 used in :mod:`sitecustomize` or :mod:`usercustomize` (see above).
Éric Araujode4f05b2011-08-06 01:51:07 +0200167
Christian Heimes8dc226f2008-05-06 23:45:46 +0000168
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000169.. function:: getsitepackages()
170
Éric Araujode4f05b2011-08-06 01:51:07 +0200171 Return a list containing all global site-packages directories (and possibly
172 site-python).
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:: getuserbase()
178
Éric Araujode4f05b2011-08-06 01:51:07 +0200179 Return the path of the user base directory, :data:`USER_BASE`. If it is not
180 initialized yet, this function will also set it, respecting
181 :envvar:`PYTHONUSERBASE`.
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000182
Mark Dickinson574b1d62009-10-01 20:20:09 +0000183 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000184
Éric Araujode4f05b2011-08-06 01:51:07 +0200185
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000186.. function:: getusersitepackages()
187
Éric Araujode4f05b2011-08-06 01:51:07 +0200188 Return the path of the user-specific site-packages directory,
189 :data:`USER_SITE`. If it is not initialized yet, this function will also set
190 it, respecting :envvar:`PYTHONNOUSERSITE` and :data:`USER_BASE`.
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000191
Mark Dickinson574b1d62009-10-01 20:20:09 +0000192 .. versionadded:: 3.2
Christian Heimes8dc226f2008-05-06 23:45:46 +0000193
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000194
Éric Araujode4f05b2011-08-06 01:51:07 +0200195The :mod:`site` module also provides a way to get the user directories from the
196command line:
197
198.. code-block:: sh
199
200 $ python3 -m site --user-site
201 /home/user/.local/lib/python3.3/site-packages
202
203.. program:: site
204
205If it is called without arguments, it will print the contents of
206:data:`sys.path` on the standard output, followed by the value of
207:data:`USER_BASE` and whether the directory exists, then the same thing for
208:data:`USER_SITE`, and finally the value of :data:`ENABLE_USER_SITE`.
209
210.. cmdoption:: --user-base
211
212 Print the path to the user base directory.
213
214.. cmdoption:: --user-site
215
216 Print the path to the user site-packages directory.
217
218If both options are given, user base and user site will be printed (always in
219this order), separated by :data:`os.pathsep`.
220
221If any option is given, the script will exit with one of these values: ``O`` if
222the user site-packages directory is enabled, ``1`` if it was disabled by the
223user, ``2`` if it is disabled for security reasons or by an administrator, and a
224value greater than 2 if there is an error.
225
226.. seealso::
227
228 :pep:`370` -- Per user site-packages directory