1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<span id="site-site-specific-configuration-hook"></span><h1>site — Site-specific configuration hook</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/site.py">Lib/site.py</a></p> <p><strong>This module is automatically imported during initialization.</strong> The automatic import can be suppressed using the interpreter’s <a class="reference internal" href="../using/cmdline#cmdoption-S"><code>-S</code></a> option.</p> <p id="index-0">Importing this module will append site-specific paths to the module search path and add a few builtins, unless <a class="reference internal" href="../using/cmdline#cmdoption-S"><code>-S</code></a> was used. In that case, this module can be safely imported with no automatic modifications to the module search path or additions to the builtins. To explicitly trigger the usual site-specific additions, call the <a class="reference internal" href="#site.main" title="site.main"><code>main()</code></a> function.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>Importing the module used to trigger paths manipulation even when using <a class="reference internal" href="../using/cmdline#cmdoption-S"><code>-S</code></a>.</p> </div> <p id="index-1">It starts by constructing up to four directories from a head and a tail part. For the head part, it uses <code>sys.prefix</code> and <code>sys.exec_prefix</code>; empty heads are skipped. For the tail part, it uses the empty string and then <code>lib/site-packages</code> (on Windows) or <code>lib/python<em>X.Y</em>/site-packages</code> (on Unix and macOS). For each of the distinct head-tail combinations, it sees if it refers to an existing directory, and if so, adds it to <code>sys.path</code> and also inspects the newly added path for configuration files.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>Support for the “site-python” directory has been removed.</p> </div> <p>If a file named “pyvenv.cfg” exists one directory above sys.executable, sys.prefix and sys.exec_prefix are set to that directory and it is also checked for site-packages (sys.base_prefix and sys.base_exec_prefix will always be the “real” prefixes of the Python installation). If “pyvenv.cfg” (a bootstrap configuration file) contains the key “include-system-site-packages” set to anything other than “true” (case-insensitive), the system-level prefixes will not be searched for site-packages; otherwise they will.</p> <p id="index-2">A path configuration file is a file whose name has the form <code><em>name</em>.pth</code> and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to <code>sys.path</code>. Non-existing items are never added to <code>sys.path</code>, and no check is made that the item refers to a directory rather than a file. No item is added to <code>sys.path</code> more than once. Blank lines and lines beginning with <code>#</code> are skipped. Lines starting with <code>import</code> (followed by space or tab) are executed.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>An executable line in a <code>.pth</code> file is run at every Python startup, regardless of whether a particular module is actually going to be used. Its impact should thus be kept to a minimum. The primary intended purpose of executable lines is to make the corresponding module(s) importable (load 3rd-party import hooks, adjust <span class="target" id="index-3"></span><code>PATH</code> etc). Any other initialization is supposed to be done upon a module’s actual import, if and when it happens. Limiting a code chunk to a single line is a deliberate measure to discourage putting anything more complex here.</p> </div> <p id="index-4">For example, suppose <code>sys.prefix</code> and <code>sys.exec_prefix</code> are set to <code>/usr/local</code>. The Python X.Y library is then installed in <code>/usr/local/lib/python<em>X.Y</em></code>. Suppose this has a subdirectory <code>/usr/local/lib/python<em>X.Y</em>/site-packages</code> with three subsubdirectories, <code>foo</code>, <code>bar</code> and <code>spam</code>, and two path configuration files, <code>foo.pth</code> and <code>bar.pth</code>. Assume <code>foo.pth</code> contains the following:</p> <pre data-language="none"># foo package configuration
foo
bar
bletch
</pre> <p>and <code>bar.pth</code> contains:</p> <pre data-language="none"># bar package configuration
bar
</pre> <p>Then the following version-specific directories are added to <code>sys.path</code>, in this order:</p> <pre data-language="none">/usr/local/lib/pythonX.Y/site-packages/bar
/usr/local/lib/pythonX.Y/site-packages/foo
</pre> <p>Note that <code>bletch</code> is omitted because it doesn’t exist; the <code>bar</code> directory precedes the <code>foo</code> directory because <code>bar.pth</code> comes alphabetically before <code>foo.pth</code>; and <code>spam</code> is omitted because it is not mentioned in either path configuration file.</p> <section id="module-sitecustomize"> <span id="sitecustomize"></span><h2>sitecustomize</h2> <p>After these path manipulations, an attempt is made to import a module named <a class="reference internal" href="#module-sitecustomize" title="sitecustomize"><code>sitecustomize</code></a>, which can perform arbitrary site-specific customizations. It is typically created by a system administrator in the site-packages directory. If this import fails with an <a class="reference internal" href="exceptions#ImportError" title="ImportError"><code>ImportError</code></a> or its subclass exception, and the exception’s <a class="reference internal" href="exceptions#ImportError.name" title="ImportError.name"><code>name</code></a> attribute equals to <code>'sitecustomize'</code>, it is silently ignored. If Python is started without output streams available, as with <code>pythonw.exe</code> on Windows (which is used by default to start IDLE), attempted output from <a class="reference internal" href="#module-sitecustomize" title="sitecustomize"><code>sitecustomize</code></a> is ignored. Any other exception causes a silent and perhaps mysterious failure of the process.</p> </section> <section id="module-usercustomize"> <span id="usercustomize"></span><h2>usercustomize</h2> <p>After this, an attempt is made to import a module named <a class="reference internal" href="#module-usercustomize" title="usercustomize"><code>usercustomize</code></a>, which can perform arbitrary user-specific customizations, if <a class="reference internal" href="#site.ENABLE_USER_SITE" title="site.ENABLE_USER_SITE"><code>ENABLE_USER_SITE</code></a> is true. This file is intended to be created in the user site-packages directory (see below), which is part of <code>sys.path</code> unless disabled by <a class="reference internal" href="../using/cmdline#cmdoption-s"><code>-s</code></a>. If this import fails with an <a class="reference internal" href="exceptions#ImportError" title="ImportError"><code>ImportError</code></a> or its subclass exception, and the exception’s <a class="reference internal" href="exceptions#ImportError.name" title="ImportError.name"><code>name</code></a> attribute equals to <code>'usercustomize'</code>, it is silently ignored.</p> <p>Note that for some non-Unix systems, <code>sys.prefix</code> and <code>sys.exec_prefix</code> are empty, and the path manipulations are skipped; however the import of <a class="reference internal" href="#module-sitecustomize" title="sitecustomize"><code>sitecustomize</code></a> and <a class="reference internal" href="#module-usercustomize" title="usercustomize"><code>usercustomize</code></a> is still attempted.</p> </section> <section id="readline-configuration"> <span id="rlcompleter-config"></span><h2>Readline configuration</h2> <p>On systems that support <a class="reference internal" href="readline#module-readline" title="readline: GNU readline support for Python. (Unix)"><code>readline</code></a>, this module will also import and configure the <a class="reference internal" href="rlcompleter#module-rlcompleter" title="rlcompleter: Python identifier completion, suitable for the GNU readline library."><code>rlcompleter</code></a> module, if Python is started in <a class="reference internal" href="../tutorial/interpreter#tut-interactive"><span class="std std-ref">interactive mode</span></a> and without the <a class="reference internal" href="../using/cmdline#cmdoption-S"><code>-S</code></a> option. The default behavior is enable tab-completion and to use <code>~/.python_history</code> as the history save file. To disable it, delete (or override) the <a class="reference internal" href="sys#sys.__interactivehook__" title="sys.__interactivehook__"><code>sys.__interactivehook__</code></a> attribute in your <a class="reference internal" href="#module-sitecustomize" title="sitecustomize"><code>sitecustomize</code></a> or <a class="reference internal" href="#module-usercustomize" title="usercustomize"><code>usercustomize</code></a> module or your <span class="target" id="index-5"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONSTARTUP"><code>PYTHONSTARTUP</code></a> file.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>Activation of rlcompleter and history was made automatic.</p> </div> </section> <section id="module-contents"> <h2>Module contents</h2> <dl class="py data"> <dt class="sig sig-object py" id="site.PREFIXES">
<code>site.PREFIXES</code> </dt> <dd>
<p>A list of prefixes for site-packages directories.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="site.ENABLE_USER_SITE">
<code>site.ENABLE_USER_SITE</code> </dt> <dd>
<p>Flag showing the status of the user site-packages directory. <code>True</code> means that it is enabled and was added to <code>sys.path</code>. <code>False</code> means that it was disabled by user request (with <a class="reference internal" href="../using/cmdline#cmdoption-s"><code>-s</code></a> or <span class="target" id="index-6"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONNOUSERSITE"><code>PYTHONNOUSERSITE</code></a>). <code>None</code> means it was disabled for security reasons (mismatch between user or group id and effective id) or by an administrator.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="site.USER_SITE">
<code>site.USER_SITE</code> </dt> <dd>
<p>Path to the user site-packages for the running Python. Can be <code>None</code> if <a class="reference internal" href="#site.getusersitepackages" title="site.getusersitepackages"><code>getusersitepackages()</code></a> hasn’t been called yet. Default value is <code>~/.local/lib/python<em>X.Y</em>/site-packages</code> for UNIX and non-framework macOS builds, <code>~/Library/Python/<em>X.Y</em>/lib/python/site-packages</code> for macOS framework builds, and <code><em>%APPDATA%</em>\Python\Python<em>XY</em>\site-packages</code> on Windows. This directory is a site directory, which means that <code>.pth</code> files in it will be processed.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="site.USER_BASE">
<code>site.USER_BASE</code> </dt> <dd>
<p>Path to the base directory for the user site-packages. Can be <code>None</code> if <a class="reference internal" href="#site.getuserbase" title="site.getuserbase"><code>getuserbase()</code></a> hasn’t been called yet. Default value is <code>~/.local</code> for UNIX and macOS non-framework builds, <code>~/Library/Python/<em>X.Y</em></code> for macOS framework builds, and <code><em>%APPDATA%</em>\Python</code> for Windows. This value is used to compute the installation directories for scripts, data files, Python modules, etc. for the <a class="reference internal" href="sysconfig#sysconfig-user-scheme"><span class="std std-ref">user installation scheme</span></a>. See also <span class="target" id="index-7"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONUSERBASE"><code>PYTHONUSERBASE</code></a>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="site.main">
<code>site.main()</code> </dt> <dd>
<p>Adds all the standard site-specific directories to the module search path. This function is called automatically when this module is imported, unless the Python interpreter was started with the <a class="reference internal" href="../using/cmdline#cmdoption-S"><code>-S</code></a> flag.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>This function used to be called unconditionally.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="site.addsitedir">
<code>site.addsitedir(sitedir, known_paths=None)</code> </dt> <dd>
<p>Add a directory to sys.path and process its <code>.pth</code> files. Typically used in <a class="reference internal" href="#module-sitecustomize" title="sitecustomize"><code>sitecustomize</code></a> or <a class="reference internal" href="#module-usercustomize" title="usercustomize"><code>usercustomize</code></a> (see above).</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="site.getsitepackages">
<code>site.getsitepackages()</code> </dt> <dd>
<p>Return a list containing all global site-packages directories.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="site.getuserbase">
<code>site.getuserbase()</code> </dt> <dd>
<p>Return the path of the user base directory, <a class="reference internal" href="#site.USER_BASE" title="site.USER_BASE"><code>USER_BASE</code></a>. If it is not initialized yet, this function will also set it, respecting <span class="target" id="index-8"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONUSERBASE"><code>PYTHONUSERBASE</code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="site.getusersitepackages">
<code>site.getusersitepackages()</code> </dt> <dd>
<p>Return the path of the user-specific site-packages directory, <a class="reference internal" href="#site.USER_SITE" title="site.USER_SITE"><code>USER_SITE</code></a>. If it is not initialized yet, this function will also set it, respecting <a class="reference internal" href="#site.USER_BASE" title="site.USER_BASE"><code>USER_BASE</code></a>. To determine if the user-specific site-packages was added to <code>sys.path</code> <a class="reference internal" href="#site.ENABLE_USER_SITE" title="site.ENABLE_USER_SITE"><code>ENABLE_USER_SITE</code></a> should be used.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd>
</dl> </section> <section id="command-line-interface"> <span id="site-commandline"></span><h2>Command Line Interface</h2> <p>The <a class="reference internal" href="#module-site" title="site: Module responsible for site-specific configuration."><code>site</code></a> module also provides a way to get the user directories from the command line:</p> <pre data-language="shell">$ python -m site --user-site
/home/user/.local/lib/python3.11/site-packages
</pre> <p>If it is called without arguments, it will print the contents of <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</code></a> on the standard output, followed by the value of <a class="reference internal" href="#site.USER_BASE" title="site.USER_BASE"><code>USER_BASE</code></a> and whether the directory exists, then the same thing for <a class="reference internal" href="#site.USER_SITE" title="site.USER_SITE"><code>USER_SITE</code></a>, and finally the value of <a class="reference internal" href="#site.ENABLE_USER_SITE" title="site.ENABLE_USER_SITE"><code>ENABLE_USER_SITE</code></a>.</p> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-site-user-base">
<code>--user-base</code> </dt> <dd>
<p>Print the path to the user base directory.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-site-user-site">
<code>--user-site</code> </dt> <dd>
<p>Print the path to the user site-packages directory.</p> </dd>
</dl> <p>If both options are given, user base and user site will be printed (always in this order), separated by <a class="reference internal" href="os#os.pathsep" title="os.pathsep"><code>os.pathsep</code></a>.</p> <p>If any option is given, the script will exit with one of these values: <code>0</code> if the user site-packages directory is enabled, <code>1</code> if it was disabled by the user, <code>2</code> if it is disabled for security reasons or by an administrator, and a value greater than 2 if there is an error.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <ul class="simple"> <li>
<span class="target" id="index-9"></span><a class="pep reference external" href="https://peps.python.org/pep-0370/"><strong>PEP 370</strong></a> – Per user site-packages directory</li> <li>
<a class="reference internal" href="sys_path_init#sys-path-init"><span class="std std-ref">The initialization of the sys.path module search path</span></a> – The initialization of <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</code></a>.</li> </ul> </div> </section> <div class="_attribution">
<p class="_attribution-p">
© 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br>
<a href="https://docs.python.org/3.12/library/site.html" class="_attribution-link">https://docs.python.org/3.12/library/site.html</a>
</p>
</div>
|