blob: ff7195dc23e191fea66de4c09091cc6efd9767e8 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`site` --- Site-specific configuration hook
2================================================
3
4.. module:: site
Éric Araujoafd2fe22011-08-19 08:20:01 +02005 :synopsis: Module responsible for site-specific configuration.
Georg Brandl8ec7f652007-08-15 14:28:01 +00006
Éric Araujo29a0b572011-08-19 02:14:03 +02007**Source code:** :source:`Lib/site.py`
8
9--------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000010
Éric Araujoafd2fe22011-08-19 08:20:01 +020011.. highlightlang:: none
12
Georg Brandl8ec7f652007-08-15 14:28:01 +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 Araujoafd2fe22011-08-19 08:20:01 +020018Importing this module will append site-specific paths to the module search path
19and add a few builtins.
Georg Brandl8ec7f652007-08-15 14:28:01 +000020
21.. index::
22 pair: site-python; directory
23 pair: site-packages; directory
24
25It starts by constructing up to four directories from a head and a tail part.
26For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads
27are skipped. For the tail part, it uses the empty string and then
28:file:`lib/site-packages` (on Windows) or
Ezio Melottib3b32fb2012-09-17 08:59:36 +020029:file:`lib/python{X.Y}/site-packages` and then :file:`lib/site-python` (on
Georg Brandl8ec7f652007-08-15 14:28:01 +000030Unix and Macintosh). For each of the distinct head-tail combinations, it sees
31if it refers to an existing directory, and if so, adds it to ``sys.path`` and
32also inspects the newly added path for configuration files.
33
Éric Araujoafd2fe22011-08-19 08:20:01 +020034A path configuration file is a file whose name has the form :file:`{name}.pth`
Georg Brandl8ec7f652007-08-15 14:28:01 +000035and exists in one of the four directories mentioned above; its contents are
36additional items (one per line) to be added to ``sys.path``. Non-existing items
Éric Araujoafd2fe22011-08-19 08:20:01 +020037are never added to ``sys.path``, and no check is made that the item refers to a
38directory rather than a file. No item is added to ``sys.path`` more than
Georg Brandl8ec7f652007-08-15 14:28:01 +000039once. Blank lines and lines beginning with ``#`` are skipped. Lines starting
40with ``import`` (followed by space or tab) are executed.
41
42.. versionchanged:: 2.6
43 A space or tab is now required after the import keyword.
44
45.. index::
46 single: package
47 triple: path; configuration; file
48
49For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
50:file:`/usr/local`. The Python X.Y library is then installed in
Éric Araujoafd2fe22011-08-19 08:20:01 +020051:file:`/usr/local/lib/python{X.Y}`. Suppose this has
Georg Brandl8ec7f652007-08-15 14:28:01 +000052a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
53subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
54configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume
55:file:`foo.pth` contains the following::
56
57 # foo package configuration
58
59 foo
60 bar
61 bletch
62
63and :file:`bar.pth` contains::
64
65 # bar package configuration
66
67 bar
68
Andrew M. Kuchlinge6896052008-09-27 22:54:08 +000069Then the following version-specific directories are added to
70``sys.path``, in this order::
Georg Brandl8ec7f652007-08-15 14:28:01 +000071
Andrew M. Kuchlinge6896052008-09-27 22:54:08 +000072 /usr/local/lib/pythonX.Y/site-packages/bar
73 /usr/local/lib/pythonX.Y/site-packages/foo
Georg Brandl8ec7f652007-08-15 14:28:01 +000074
75Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar`
76directory precedes the :file:`foo` directory because :file:`bar.pth` comes
77alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is
78not mentioned in either path configuration file.
79
80.. index:: module: sitecustomize
81
82After these path manipulations, an attempt is made to import a module named
83:mod:`sitecustomize`, which can perform arbitrary site-specific customizations.
Éric Araujoafd2fe22011-08-19 08:20:01 +020084It is typically created by a system administrator in the site-packages
85directory. If this import fails with an :exc:`ImportError` exception, it is
Terry Jan Reedy0fe1d0a2014-04-29 00:31:46 -040086silently ignored. If Python is started without output streams available, as
87with :file:`pythonw.exe` on Windows (which is used by default to start IDLE),
88attempted output from :mod:`sitecustomize` is ignored. Any exception other
89than :exc:`ImportError` causes a silent and perhaps mysterious failure of the
90process.
Georg Brandl8ec7f652007-08-15 14:28:01 +000091
Éric Araujoafd2fe22011-08-19 08:20:01 +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 Brandl8ec7f652007-08-15 14:28:01 +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 Araujoafd2fe22011-08-19 08:20:01 +0200102:mod:`sitecustomize` and :mod:`usercustomize` is still attempted.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000103
Christian Heimesaf748c32008-05-06 22:41:46 +0000104
105.. data:: PREFIXES
106
Éric Araujoafd2fe22011-08-19 08:20:01 +0200107 A list of prefixes for site-packages directories.
Christian Heimesaf748c32008-05-06 22:41:46 +0000108
109 .. versionadded:: 2.6
110
111
112.. data:: ENABLE_USER_SITE
113
Éric Araujoafd2fe22011-08-19 08:20:01 +0200114 Flag showing the status of the user site-packages directory. ``True`` means
115 that it is enabled and was added to ``sys.path``. ``False`` means that it
116 was disabled by user request (with :option:`-s` or
117 :envvar:`PYTHONNOUSERSITE`). ``None`` means it was disabled for security
118 reasons (mismatch between user or group id and effective id) or by an
119 administrator.
Christian Heimesaf748c32008-05-06 22:41:46 +0000120
121 .. versionadded:: 2.6
122
123
124.. data:: USER_SITE
125
Éric Araujoafd2fe22011-08-19 08:20:01 +0200126 Path to the user site-packages for the running Python. Can be ``None`` if
127 :func:`getusersitepackages` hasn't been called yet. Default value is
128 :file:`~/.local/lib/python{X.Y}/site-packages` for UNIX and non-framework Mac
129 OS X builds, :file:`~/Library/Python/{X.Y}/lib/python/site-packages` for Mac
130 framework builds, and :file:`{%APPDATA%}\\Python\\Python{XY}\\site-packages`
131 on Windows. This directory is a site directory, which means that
132 :file:`.pth` files in it will be processed.
Christian Heimesaf748c32008-05-06 22:41:46 +0000133
134 .. versionadded:: 2.6
135
136
137.. data:: USER_BASE
138
Éric Araujoafd2fe22011-08-19 08:20:01 +0200139 Path to the base directory for the user site-packages. Can be ``None`` if
140 :func:`getuserbase` hasn't been called yet. Default value is
141 :file:`~/.local` for UNIX and Mac OS X non-framework builds,
142 :file:`~/Library/Python/{X.Y}` for Mac framework builds, and
143 :file:`{%APPDATA%}\\Python` for Windows. This value is used by Distutils to
144 compute the installation directories for scripts, data files, Python modules,
Éric Araujoe68d4502011-08-19 08:34:52 +0200145 etc. for the :ref:`user installation scheme <inst-alt-install-user>`. See
146 also :envvar:`PYTHONUSERBASE`.
Christian Heimesaf748c32008-05-06 22:41:46 +0000147
148 .. versionadded:: 2.6
149
150
151.. function:: addsitedir(sitedir, known_paths=None)
152
Éric Araujoe68d4502011-08-19 08:34:52 +0200153 Add a directory to sys.path and process its :file:`.pth` files. Typically
154 used in :mod:`sitecustomize` or :mod:`usercustomize` (see above).
Éric Araujoafd2fe22011-08-19 08:20:01 +0200155
Christian Heimesaf748c32008-05-06 22:41:46 +0000156
Tarek Ziadé764fc232009-08-20 21:23:13 +0000157.. function:: getsitepackages()
158
Éric Araujoafd2fe22011-08-19 08:20:01 +0200159 Return a list containing all global site-packages directories (and possibly
160 site-python).
Tarek Ziadé764fc232009-08-20 21:23:13 +0000161
162 .. versionadded:: 2.7
163
Éric Araujoafd2fe22011-08-19 08:20:01 +0200164
Tarek Ziadé764fc232009-08-20 21:23:13 +0000165.. function:: getuserbase()
166
Éric Araujoafd2fe22011-08-19 08:20:01 +0200167 Return the path of the user base directory, :data:`USER_BASE`. If it is not
168 initialized yet, this function will also set it, respecting
169 :envvar:`PYTHONUSERBASE`.
Tarek Ziadé764fc232009-08-20 21:23:13 +0000170
171 .. versionadded:: 2.7
172
Éric Araujoafd2fe22011-08-19 08:20:01 +0200173
Tarek Ziadé764fc232009-08-20 21:23:13 +0000174.. function:: getusersitepackages()
175
Éric Araujoafd2fe22011-08-19 08:20:01 +0200176 Return the path of the user-specific site-packages directory,
177 :data:`USER_SITE`. If it is not initialized yet, this function will also set
178 it, respecting :envvar:`PYTHONNOUSERSITE` and :data:`USER_BASE`.
Tarek Ziadé764fc232009-08-20 21:23:13 +0000179
180 .. versionadded:: 2.7
Christian Heimesaf748c32008-05-06 22:41:46 +0000181
Tarek Ziadé764fc232009-08-20 21:23:13 +0000182
Éric Araujoafd2fe22011-08-19 08:20:01 +0200183The :mod:`site` module also provides a way to get the user directories from the
184command line:
185
186.. code-block:: sh
187
Berker Peksage75f5272014-06-26 23:27:26 +0300188 $ python -m site --user-site
189 /home/user/.local/lib/python2.7/site-packages
Éric Araujoafd2fe22011-08-19 08:20:01 +0200190
191.. program:: site
192
193If it is called without arguments, it will print the contents of
194:data:`sys.path` on the standard output, followed by the value of
195:data:`USER_BASE` and whether the directory exists, then the same thing for
196:data:`USER_SITE`, and finally the value of :data:`ENABLE_USER_SITE`.
197
198.. cmdoption:: --user-base
199
200 Print the path to the user base directory.
201
202.. cmdoption:: --user-site
203
204 Print the path to the user site-packages directory.
205
206If both options are given, user base and user site will be printed (always in
207this order), separated by :data:`os.pathsep`.
208
209If any option is given, the script will exit with one of these values: ``O`` if
210the user site-packages directory is enabled, ``1`` if it was disabled by the
211user, ``2`` if it is disabled for security reasons or by an administrator, and a
212value greater than 2 if there is an error.
213
214.. seealso::
215
216 :pep:`370` -- Per user site-packages directory