1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>28.14. site — Site-specific configuration hook — Python 2.7.11 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.11 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.11 documentation" href="../contents.html" />
33
<link rel="up" title="28. Python Runtime Services" href="python.html" />
34
<link rel="next" title="28.15. user — User-specific configuration hook" href="user.html" />
35
<link rel="prev" title="28.13. inspect — Inspect live objects" href="inspect.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="user.html" title="28.15. user — User-specific configuration hook"
55
accesskey="N">next</a> |</li>
57
<a href="inspect.html" title="28.13. inspect — Inspect live objects"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.11</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="python.html" accesskey="U">28. Python Runtime Services</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="module-site">
78
<span id="site-site-specific-configuration-hook"></span><h1>28.14. <a class="reference internal" href="#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal"><span class="pre">site</span></code></a> — Site-specific configuration hook<a class="headerlink" href="#module-site" title="Permalink to this headline">¶</a></h1>
79
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/2.7/Lib/site.py">Lib/site.py</a></p>
80
<hr class="docutils" />
81
<p><strong>This module is automatically imported during initialization.</strong> The automatic
82
import can be suppressed using the interpreter’s <a class="reference internal" href="../using/cmdline.html#cmdoption-S"><code class="xref std std-option docutils literal"><span class="pre">-S</span></code></a> option.</p>
83
<p id="index-0">Importing this module will append site-specific paths to the module search path
84
and add a few builtins.</p>
85
<p id="index-1">It starts by constructing up to four directories from a head and a tail part.
86
For the head part, it uses <code class="docutils literal"><span class="pre">sys.prefix</span></code> and <code class="docutils literal"><span class="pre">sys.exec_prefix</span></code>; empty heads
87
are skipped. For the tail part, it uses the empty string and then
88
<code class="file docutils literal"><span class="pre">lib/site-packages</span></code> (on Windows) or
89
<code class="file docutils literal"><span class="pre">lib/python</span><em><span class="pre">X.Y</span></em><span class="pre">/site-packages</span></code> and then <code class="file docutils literal"><span class="pre">lib/site-python</span></code> (on
90
Unix and Macintosh). For each of the distinct head-tail combinations, it sees
91
if it refers to an existing directory, and if so, adds it to <code class="docutils literal"><span class="pre">sys.path</span></code> and
92
also inspects the newly added path for configuration files.</p>
93
<p>A path configuration file is a file whose name has the form <code class="file docutils literal"><em><span class="pre">name</span></em><span class="pre">.pth</span></code>
94
and exists in one of the four directories mentioned above; its contents are
95
additional items (one per line) to be added to <code class="docutils literal"><span class="pre">sys.path</span></code>. Non-existing items
96
are never added to <code class="docutils literal"><span class="pre">sys.path</span></code>, and no check is made that the item refers to a
97
directory rather than a file. No item is added to <code class="docutils literal"><span class="pre">sys.path</span></code> more than
98
once. Blank lines and lines beginning with <code class="docutils literal"><span class="pre">#</span></code> are skipped. Lines starting
99
with <code class="docutils literal"><span class="pre">import</span></code> (followed by space or tab) are executed.</p>
100
<div class="versionchanged">
101
<p><span class="versionmodified">Changed in version 2.6: </span>A space or tab is now required after the import keyword.</p>
103
<p id="index-2">For example, suppose <code class="docutils literal"><span class="pre">sys.prefix</span></code> and <code class="docutils literal"><span class="pre">sys.exec_prefix</span></code> are set to
104
<code class="file docutils literal"><span class="pre">/usr/local</span></code>. The Python X.Y library is then installed in
105
<code class="file docutils literal"><span class="pre">/usr/local/lib/python</span><em><span class="pre">X.Y</span></em></code>. Suppose this has
106
a subdirectory <code class="file docutils literal"><span class="pre">/usr/local/lib/python</span><em><span class="pre">X.Y</span></em><span class="pre">/site-packages</span></code> with three
107
subsubdirectories, <code class="file docutils literal"><span class="pre">foo</span></code>, <code class="file docutils literal"><span class="pre">bar</span></code> and <code class="file docutils literal"><span class="pre">spam</span></code>, and two path
108
configuration files, <code class="file docutils literal"><span class="pre">foo.pth</span></code> and <code class="file docutils literal"><span class="pre">bar.pth</span></code>. Assume
109
<code class="file docutils literal"><span class="pre">foo.pth</span></code> contains the following:</p>
110
<div class="highlight-none"><div class="highlight"><pre># foo package configuration
117
<p>and <code class="file docutils literal"><span class="pre">bar.pth</span></code> contains:</p>
118
<div class="highlight-none"><div class="highlight"><pre># bar package configuration
123
<p>Then the following version-specific directories are added to
124
<code class="docutils literal"><span class="pre">sys.path</span></code>, in this order:</p>
125
<div class="highlight-none"><div class="highlight"><pre>/usr/local/lib/pythonX.Y/site-packages/bar
126
/usr/local/lib/pythonX.Y/site-packages/foo
129
<p>Note that <code class="file docutils literal"><span class="pre">bletch</span></code> is omitted because it doesn’t exist; the <code class="file docutils literal"><span class="pre">bar</span></code>
130
directory precedes the <code class="file docutils literal"><span class="pre">foo</span></code> directory because <code class="file docutils literal"><span class="pre">bar.pth</span></code> comes
131
alphabetically before <code class="file docutils literal"><span class="pre">foo.pth</span></code>; and <code class="file docutils literal"><span class="pre">spam</span></code> is omitted because it is
132
not mentioned in either path configuration file.</p>
133
<p id="index-3">After these path manipulations, an attempt is made to import a module named
134
<code class="xref py py-mod docutils literal"><span class="pre">sitecustomize</span></code>, which can perform arbitrary site-specific customizations.
135
It is typically created by a system administrator in the site-packages
136
directory. If this import fails with an <a class="reference internal" href="exceptions.html#exceptions.ImportError" title="exceptions.ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a> exception, it is
137
silently ignored. If Python is started without output streams available, as
138
with <code class="file docutils literal"><span class="pre">pythonw.exe</span></code> on Windows (which is used by default to start IDLE),
139
attempted output from <code class="xref py py-mod docutils literal"><span class="pre">sitecustomize</span></code> is ignored. Any exception other
140
than <a class="reference internal" href="exceptions.html#exceptions.ImportError" title="exceptions.ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a> causes a silent and perhaps mysterious failure of the
142
<p id="index-4">After this, an attempt is made to import a module named <code class="xref py py-mod docutils literal"><span class="pre">usercustomize</span></code>,
143
which can perform arbitrary user-specific customizations, if
144
<a class="reference internal" href="#site.ENABLE_USER_SITE" title="site.ENABLE_USER_SITE"><code class="xref py py-data docutils literal"><span class="pre">ENABLE_USER_SITE</span></code></a> is true. This file is intended to be created in the
145
user site-packages directory (see below), which is part of <code class="docutils literal"><span class="pre">sys.path</span></code> unless
146
disabled by <a class="reference internal" href="../using/cmdline.html#cmdoption-s"><code class="xref std std-option docutils literal"><span class="pre">-s</span></code></a>. An <a class="reference internal" href="exceptions.html#exceptions.ImportError" title="exceptions.ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a> will be silently ignored.</p>
147
<p>Note that for some non-Unix systems, <code class="docutils literal"><span class="pre">sys.prefix</span></code> and <code class="docutils literal"><span class="pre">sys.exec_prefix</span></code> are
148
empty, and the path manipulations are skipped; however the import of
149
<code class="xref py py-mod docutils literal"><span class="pre">sitecustomize</span></code> and <code class="xref py py-mod docutils literal"><span class="pre">usercustomize</span></code> is still attempted.</p>
151
<dt id="site.PREFIXES">
152
<code class="descclassname">site.</code><code class="descname">PREFIXES</code><a class="headerlink" href="#site.PREFIXES" title="Permalink to this definition">¶</a></dt>
153
<dd><p>A list of prefixes for site-packages directories.</p>
154
<div class="versionadded">
155
<p><span class="versionmodified">New in version 2.6.</span></p>
160
<dt id="site.ENABLE_USER_SITE">
161
<code class="descclassname">site.</code><code class="descname">ENABLE_USER_SITE</code><a class="headerlink" href="#site.ENABLE_USER_SITE" title="Permalink to this definition">¶</a></dt>
162
<dd><p>Flag showing the status of the user site-packages directory. <code class="docutils literal"><span class="pre">True</span></code> means
163
that it is enabled and was added to <code class="docutils literal"><span class="pre">sys.path</span></code>. <code class="docutils literal"><span class="pre">False</span></code> means that it
164
was disabled by user request (with <a class="reference internal" href="../using/cmdline.html#cmdoption-s"><code class="xref std std-option docutils literal"><span class="pre">-s</span></code></a> or
165
<span class="target" id="index-5"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONNOUSERSITE"><code class="xref std std-envvar docutils literal"><span class="pre">PYTHONNOUSERSITE</span></code></a>). <code class="docutils literal"><span class="pre">None</span></code> means it was disabled for security
166
reasons (mismatch between user or group id and effective id) or by an
168
<div class="versionadded">
169
<p><span class="versionmodified">New in version 2.6.</span></p>
174
<dt id="site.USER_SITE">
175
<code class="descclassname">site.</code><code class="descname">USER_SITE</code><a class="headerlink" href="#site.USER_SITE" title="Permalink to this definition">¶</a></dt>
176
<dd><p>Path to the user site-packages for the running Python. Can be <code class="docutils literal"><span class="pre">None</span></code> if
177
<a class="reference internal" href="#site.getusersitepackages" title="site.getusersitepackages"><code class="xref py py-func docutils literal"><span class="pre">getusersitepackages()</span></code></a> hasn’t been called yet. Default value is
178
<code class="file docutils literal"><span class="pre">~/.local/lib/python</span><em><span class="pre">X.Y</span></em><span class="pre">/site-packages</span></code> for UNIX and non-framework Mac
179
OS X builds, <code class="file docutils literal"><span class="pre">~/Library/Python/</span><em><span class="pre">X.Y</span></em><span class="pre">/lib/python/site-packages</span></code> for Mac
180
framework builds, and <code class="file docutils literal"><em><span class="pre">%APPDATA%</span></em><span class="pre">\Python\Python</span><em><span class="pre">XY</span></em><span class="pre">\site-packages</span></code>
181
on Windows. This directory is a site directory, which means that
182
<code class="file docutils literal"><span class="pre">.pth</span></code> files in it will be processed.</p>
183
<div class="versionadded">
184
<p><span class="versionmodified">New in version 2.6.</span></p>
189
<dt id="site.USER_BASE">
190
<code class="descclassname">site.</code><code class="descname">USER_BASE</code><a class="headerlink" href="#site.USER_BASE" title="Permalink to this definition">¶</a></dt>
191
<dd><p>Path to the base directory for the user site-packages. Can be <code class="docutils literal"><span class="pre">None</span></code> if
192
<a class="reference internal" href="#site.getuserbase" title="site.getuserbase"><code class="xref py py-func docutils literal"><span class="pre">getuserbase()</span></code></a> hasn’t been called yet. Default value is
193
<code class="file docutils literal"><span class="pre">~/.local</span></code> for UNIX and Mac OS X non-framework builds,
194
<code class="file docutils literal"><span class="pre">~/Library/Python/</span><em><span class="pre">X.Y</span></em></code> for Mac framework builds, and
195
<code class="file docutils literal"><em><span class="pre">%APPDATA%</span></em><span class="pre">\Python</span></code> for Windows. This value is used by Distutils to
196
compute the installation directories for scripts, data files, Python modules,
197
etc. for the <a class="reference internal" href="../install/index.html#inst-alt-install-user"><span>user installation scheme</span></a>. See
198
also <span class="target" id="index-6"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONUSERBASE"><code class="xref std std-envvar docutils literal"><span class="pre">PYTHONUSERBASE</span></code></a>.</p>
199
<div class="versionadded">
200
<p><span class="versionmodified">New in version 2.6.</span></p>
204
<dl class="function">
205
<dt id="site.addsitedir">
206
<code class="descclassname">site.</code><code class="descname">addsitedir</code><span class="sig-paren">(</span><em>sitedir</em>, <em>known_paths=None</em><span class="sig-paren">)</span><a class="headerlink" href="#site.addsitedir" title="Permalink to this definition">¶</a></dt>
207
<dd><p>Add a directory to sys.path and process its <code class="file docutils literal"><span class="pre">.pth</span></code> files. Typically
208
used in <code class="xref py py-mod docutils literal"><span class="pre">sitecustomize</span></code> or <code class="xref py py-mod docutils literal"><span class="pre">usercustomize</span></code> (see above).</p>
211
<dl class="function">
212
<dt id="site.getsitepackages">
213
<code class="descclassname">site.</code><code class="descname">getsitepackages</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#site.getsitepackages" title="Permalink to this definition">¶</a></dt>
214
<dd><p>Return a list containing all global site-packages directories (and possibly
216
<div class="versionadded">
217
<p><span class="versionmodified">New in version 2.7.</span></p>
221
<dl class="function">
222
<dt id="site.getuserbase">
223
<code class="descclassname">site.</code><code class="descname">getuserbase</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#site.getuserbase" title="Permalink to this definition">¶</a></dt>
224
<dd><p>Return the path of the user base directory, <a class="reference internal" href="#site.USER_BASE" title="site.USER_BASE"><code class="xref py py-data docutils literal"><span class="pre">USER_BASE</span></code></a>. If it is not
225
initialized yet, this function will also set it, respecting
226
<span class="target" id="index-7"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONUSERBASE"><code class="xref std std-envvar docutils literal"><span class="pre">PYTHONUSERBASE</span></code></a>.</p>
227
<div class="versionadded">
228
<p><span class="versionmodified">New in version 2.7.</span></p>
232
<dl class="function">
233
<dt id="site.getusersitepackages">
234
<code class="descclassname">site.</code><code class="descname">getusersitepackages</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#site.getusersitepackages" title="Permalink to this definition">¶</a></dt>
235
<dd><p>Return the path of the user-specific site-packages directory,
236
<a class="reference internal" href="#site.USER_SITE" title="site.USER_SITE"><code class="xref py py-data docutils literal"><span class="pre">USER_SITE</span></code></a>. If it is not initialized yet, this function will also set
237
it, respecting <span class="target" id="index-8"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONNOUSERSITE"><code class="xref std std-envvar docutils literal"><span class="pre">PYTHONNOUSERSITE</span></code></a> and <a class="reference internal" href="#site.USER_BASE" title="site.USER_BASE"><code class="xref py py-data docutils literal"><span class="pre">USER_BASE</span></code></a>.</p>
238
<div class="versionadded">
239
<p><span class="versionmodified">New in version 2.7.</span></p>
243
<p>The <a class="reference internal" href="#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal"><span class="pre">site</span></code></a> module also provides a way to get the user directories from the
245
<div class="highlight-sh"><div class="highlight"><pre>$ python -m site --user-site
246
/home/user/.local/lib/python2.7/site-packages
249
<p>If it is called without arguments, it will print the contents of
250
<a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal"><span class="pre">sys.path</span></code></a> on the standard output, followed by the value of
251
<a class="reference internal" href="#site.USER_BASE" title="site.USER_BASE"><code class="xref py py-data docutils literal"><span class="pre">USER_BASE</span></code></a> and whether the directory exists, then the same thing for
252
<a class="reference internal" href="#site.USER_SITE" title="site.USER_SITE"><code class="xref py py-data docutils literal"><span class="pre">USER_SITE</span></code></a>, and finally the value of <a class="reference internal" href="#site.ENABLE_USER_SITE" title="site.ENABLE_USER_SITE"><code class="xref py py-data docutils literal"><span class="pre">ENABLE_USER_SITE</span></code></a>.</p>
253
<dl class="cmdoption">
254
<dt id="cmdoption-site--user-base">
255
<code class="descname">--user-base</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-site--user-base" title="Permalink to this definition">¶</a></dt>
256
<dd><p>Print the path to the user base directory.</p>
259
<dl class="cmdoption">
260
<dt id="cmdoption-site--user-site">
261
<code class="descname">--user-site</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-site--user-site" title="Permalink to this definition">¶</a></dt>
262
<dd><p>Print the path to the user site-packages directory.</p>
265
<p>If both options are given, user base and user site will be printed (always in
266
this order), separated by <a class="reference internal" href="os.html#os.pathsep" title="os.pathsep"><code class="xref py py-data docutils literal"><span class="pre">os.pathsep</span></code></a>.</p>
267
<p>If any option is given, the script will exit with one of these values: <code class="docutils literal"><span class="pre">O</span></code> if
268
the user site-packages directory is enabled, <code class="docutils literal"><span class="pre">1</span></code> if it was disabled by the
269
user, <code class="docutils literal"><span class="pre">2</span></code> if it is disabled for security reasons or by an administrator, and a
270
value greater than 2 if there is an error.</p>
271
<div class="admonition seealso">
272
<p class="first admonition-title">See also</p>
273
<p class="last"><span class="target" id="index-9"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0370"><strong>PEP 370</strong></a> – Per user site-packages directory</p>
281
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
282
<div class="sphinxsidebarwrapper">
283
<h4>Previous topic</h4>
284
<p class="topless"><a href="inspect.html"
285
title="previous chapter">28.13. <code class="docutils literal"><span class="pre">inspect</span></code> — Inspect live objects</a></p>
287
<p class="topless"><a href="user.html"
288
title="next chapter">28.15. <code class="docutils literal"><span class="pre">user</span></code> — User-specific configuration hook</a></p>
290
<ul class="this-page-menu">
291
<li><a href="../bugs.html">Report a Bug</a></li>
292
<li><a href="../_sources/library/site.txt"
293
rel="nofollow">Show Source</a></li>
296
<div id="searchbox" style="display: none" role="search">
297
<h3>Quick search</h3>
298
<form class="search" action="../search.html" method="get">
299
<input type="text" name="q" />
300
<input type="submit" value="Go" />
301
<input type="hidden" name="check_keywords" value="yes" />
302
<input type="hidden" name="area" value="default" />
304
<p class="searchtip" style="font-size: 90%">
305
Enter search terms or a module, class or function name.
308
<script type="text/javascript">$('#searchbox').show(0);</script>
311
<div class="clearer"></div>
313
<div class="related" role="navigation" aria-label="related navigation">
316
<li class="right" style="margin-right: 10px">
317
<a href="../genindex.html" title="General Index"
320
<a href="../py-modindex.html" title="Python Module Index"
323
<a href="user.html" title="28.15. user — User-specific configuration hook"
326
<a href="inspect.html" title="28.13. inspect — Inspect live objects"
328
<li><img src="../_static/py.png" alt=""
329
style="vertical-align: middle; margin-top: -1px"/></li>
330
<li><a href="https://www.python.org/">Python</a> »</li>
332
<span class="version_switcher_placeholder">2.7.11</span>
333
<a href="../index.html">Documentation</a> »
336
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
337
<li class="nav-item nav-item-2"><a href="python.html" >28. Python Runtime Services</a> »</li>
341
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
343
The Python Software Foundation is a non-profit corporation.
344
<a href="https://www.python.org/psf/donations/">Please donate.</a>
346
Last updated on Jan 23, 2016.
347
<a href="../bugs.html">Found a bug</a>?
349
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'