gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

594 lines
43KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="site — Site-specific configuration hook" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/site.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/site.py This module is automatically imported during initialization. The automatic import can be suppressed using the interpreter’s-S option. Importing this module will append site..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/site.py This module is automatically imported during initialization. The automatic import can be suppressed using the interpreter’s-S option. Importing this module will append site..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>site — Site-specific configuration hook &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Custom Python Interpreters" href="custominterp.html" />
  33. <link rel="prev" title="inspect — Inspect live objects" href="inspect.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/site.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code> — Site-specific configuration hook</a><ul>
  86. <li><a class="reference internal" href="#module-sitecustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sitecustomize</span></code></a></li>
  87. <li><a class="reference internal" href="#module-usercustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">usercustomize</span></code></a></li>
  88. <li><a class="reference internal" href="#readline-configuration">Readline configuration</a></li>
  89. <li><a class="reference internal" href="#module-contents">Module contents</a></li>
  90. <li><a class="reference internal" href="#command-line-interface">Command Line Interface</a></li>
  91. </ul>
  92. </li>
  93. </ul>
  94. </div>
  95. <div>
  96. <h4>Previous topic</h4>
  97. <p class="topless"><a href="inspect.html"
  98. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code> — Inspect live objects</a></p>
  99. </div>
  100. <div>
  101. <h4>Next topic</h4>
  102. <p class="topless"><a href="custominterp.html"
  103. title="next chapter">Custom Python Interpreters</a></p>
  104. </div>
  105. <div role="note" aria-label="source link">
  106. <h3>This Page</h3>
  107. <ul class="this-page-menu">
  108. <li><a href="../bugs.html">Report a Bug</a></li>
  109. <li>
  110. <a href="https://github.com/python/cpython/blob/main/Doc/library/site.rst"
  111. rel="nofollow">Show Source
  112. </a>
  113. </li>
  114. </ul>
  115. </div>
  116. </nav>
  117. </div>
  118. </div>
  119. <div class="related" role="navigation" aria-label="related navigation">
  120. <h3>Navigation</h3>
  121. <ul>
  122. <li class="right" style="margin-right: 10px">
  123. <a href="../genindex.html" title="General Index"
  124. accesskey="I">index</a></li>
  125. <li class="right" >
  126. <a href="../py-modindex.html" title="Python Module Index"
  127. >modules</a> |</li>
  128. <li class="right" >
  129. <a href="custominterp.html" title="Custom Python Interpreters"
  130. accesskey="N">next</a> |</li>
  131. <li class="right" >
  132. <a href="inspect.html" title="inspect — Inspect live objects"
  133. accesskey="P">previous</a> |</li>
  134. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  135. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  136. <li class="switchers">
  137. <div class="language_switcher_placeholder"></div>
  138. <div class="version_switcher_placeholder"></div>
  139. </li>
  140. <li>
  141. </li>
  142. <li id="cpython-language-and-version">
  143. <a href="../index.html">3.12.3 Documentation</a> &#187;
  144. </li>
  145. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  146. <li class="nav-item nav-item-2"><a href="python.html" accesskey="U">Python Runtime Services</a> &#187;</li>
  147. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code> — Site-specific configuration hook</a></li>
  148. <li class="right">
  149. <div class="inline-search" role="search">
  150. <form class="inline-search" action="../search.html" method="get">
  151. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  152. <input type="submit" value="Go" />
  153. </form>
  154. </div>
  155. |
  156. </li>
  157. <li class="right">
  158. <label class="theme-selector-label">
  159. Theme
  160. <select class="theme-selector" oninput="activateTheme(this.value)">
  161. <option value="auto" selected>Auto</option>
  162. <option value="light">Light</option>
  163. <option value="dark">Dark</option>
  164. </select>
  165. </label> |</li>
  166. </ul>
  167. </div>
  168. <div class="document">
  169. <div class="documentwrapper">
  170. <div class="bodywrapper">
  171. <div class="body" role="main">
  172. <section id="module-site">
  173. <span id="site-site-specific-configuration-hook"></span><h1><a class="reference internal" href="#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> — Site-specific configuration hook<a class="headerlink" href="#module-site" title="Link to this heading">¶</a></h1>
  174. <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>
  175. <hr class="docutils" />
  176. <p><strong>This module is automatically imported during initialization.</strong> The automatic
  177. 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 notranslate"><span class="pre">-S</span></code></a> option.</p>
  178. <p id="index-0">Importing this module will append site-specific paths to the module search path
  179. and add a few builtins, unless <a class="reference internal" href="../using/cmdline.html#cmdoption-S"><code class="xref std std-option docutils literal notranslate"><span class="pre">-S</span></code></a> was used. In that case, this module
  180. can be safely imported with no automatic modifications to the module search path
  181. or additions to the builtins. To explicitly trigger the usual site-specific
  182. additions, call the <a class="reference internal" href="#site.main" title="site.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">main()</span></code></a> function.</p>
  183. <div class="versionchanged">
  184. <p><span class="versionmodified changed">Changed in version 3.3: </span>Importing the module used to trigger paths manipulation even when using
  185. <a class="reference internal" href="../using/cmdline.html#cmdoption-S"><code class="xref std std-option docutils literal notranslate"><span class="pre">-S</span></code></a>.</p>
  186. </div>
  187. <p id="index-1">It starts by constructing up to four directories from a head and a tail part.
  188. For the head part, it uses <code class="docutils literal notranslate"><span class="pre">sys.prefix</span></code> and <code class="docutils literal notranslate"><span class="pre">sys.exec_prefix</span></code>; empty heads
  189. are skipped. For the tail part, it uses the empty string and then
  190. <code class="file docutils literal notranslate"><span class="pre">lib/site-packages</span></code> (on Windows) or
  191. <code class="file docutils literal notranslate"><span class="pre">lib/python</span><em><span class="pre">X.Y</span></em><span class="pre">/site-packages</span></code> (on Unix and macOS). For each
  192. of the distinct head-tail combinations, it sees if it refers to an existing
  193. directory, and if so, adds it to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> and also inspects the newly
  194. added path for configuration files.</p>
  195. <div class="versionchanged">
  196. <p><span class="versionmodified changed">Changed in version 3.5: </span>Support for the “site-python” directory has been removed.</p>
  197. </div>
  198. <p>If a file named “pyvenv.cfg” exists one directory above sys.executable,
  199. sys.prefix and sys.exec_prefix are set to that directory and
  200. it is also checked for site-packages (sys.base_prefix and
  201. sys.base_exec_prefix will always be the “real” prefixes of the Python
  202. installation). If “pyvenv.cfg” (a bootstrap configuration file) contains
  203. the key “include-system-site-packages” set to anything other than “true”
  204. (case-insensitive), the system-level prefixes will not be
  205. searched for site-packages; otherwise they will.</p>
  206. <p id="index-2">A path configuration file is a file whose name has the form <code class="file docutils literal notranslate"><em><span class="pre">name</span></em><span class="pre">.pth</span></code>
  207. and exists in one of the four directories mentioned above; its contents are
  208. additional items (one per line) to be added to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>. Non-existing items
  209. are never added to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>, and no check is made that the item refers to a
  210. directory rather than a file. No item is added to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> more than
  211. once. Blank lines and lines beginning with <code class="docutils literal notranslate"><span class="pre">#</span></code> are skipped. Lines starting
  212. with <code class="docutils literal notranslate"><span class="pre">import</span></code> (followed by space or tab) are executed.</p>
  213. <div class="admonition note">
  214. <p class="admonition-title">Note</p>
  215. <p>An executable line in a <code class="file docutils literal notranslate"><span class="pre">.pth</span></code> file is run at every Python startup,
  216. regardless of whether a particular module is actually going to be used.
  217. Its impact should thus be kept to a minimum.
  218. The primary intended purpose of executable lines is to make the
  219. corresponding module(s) importable
  220. (load 3rd-party import hooks, adjust <span class="target" id="index-3"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> etc).
  221. Any other initialization is supposed to be done upon a module’s
  222. actual import, if and when it happens.
  223. Limiting a code chunk to a single line is a deliberate measure
  224. to discourage putting anything more complex here.</p>
  225. </div>
  226. <p id="index-4">For example, suppose <code class="docutils literal notranslate"><span class="pre">sys.prefix</span></code> and <code class="docutils literal notranslate"><span class="pre">sys.exec_prefix</span></code> are set to
  227. <code class="file docutils literal notranslate"><span class="pre">/usr/local</span></code>. The Python X.Y library is then installed in
  228. <code class="file docutils literal notranslate"><span class="pre">/usr/local/lib/python</span><em><span class="pre">X.Y</span></em></code>. Suppose this has
  229. a subdirectory <code class="file docutils literal notranslate"><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
  230. subsubdirectories, <code class="file docutils literal notranslate"><span class="pre">foo</span></code>, <code class="file docutils literal notranslate"><span class="pre">bar</span></code> and <code class="file docutils literal notranslate"><span class="pre">spam</span></code>, and two path
  231. configuration files, <code class="file docutils literal notranslate"><span class="pre">foo.pth</span></code> and <code class="file docutils literal notranslate"><span class="pre">bar.pth</span></code>. Assume
  232. <code class="file docutils literal notranslate"><span class="pre">foo.pth</span></code> contains the following:</p>
  233. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span># foo package configuration
  234. foo
  235. bar
  236. bletch
  237. </pre></div>
  238. </div>
  239. <p>and <code class="file docutils literal notranslate"><span class="pre">bar.pth</span></code> contains:</p>
  240. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span># bar package configuration
  241. bar
  242. </pre></div>
  243. </div>
  244. <p>Then the following version-specific directories are added to
  245. <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>, in this order:</p>
  246. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>/usr/local/lib/pythonX.Y/site-packages/bar
  247. /usr/local/lib/pythonX.Y/site-packages/foo
  248. </pre></div>
  249. </div>
  250. <p>Note that <code class="file docutils literal notranslate"><span class="pre">bletch</span></code> is omitted because it doesn’t exist; the <code class="file docutils literal notranslate"><span class="pre">bar</span></code>
  251. directory precedes the <code class="file docutils literal notranslate"><span class="pre">foo</span></code> directory because <code class="file docutils literal notranslate"><span class="pre">bar.pth</span></code> comes
  252. alphabetically before <code class="file docutils literal notranslate"><span class="pre">foo.pth</span></code>; and <code class="file docutils literal notranslate"><span class="pre">spam</span></code> is omitted because it is
  253. not mentioned in either path configuration file.</p>
  254. <section id="module-sitecustomize">
  255. <span id="sitecustomize"></span><h2><a class="reference internal" href="#module-sitecustomize" title="sitecustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sitecustomize</span></code></a><a class="headerlink" href="#module-sitecustomize" title="Link to this heading">¶</a></h2>
  256. <p>After these path manipulations, an attempt is made to import a module named
  257. <a class="reference internal" href="#module-sitecustomize" title="sitecustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sitecustomize</span></code></a>, which can perform arbitrary site-specific customizations.
  258. It is typically created by a system administrator in the site-packages
  259. directory. If this import fails with an <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> or its subclass
  260. exception, and the exception’s <a class="reference internal" href="exceptions.html#ImportError.name" title="ImportError.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a>
  261. attribute equals to <code class="docutils literal notranslate"><span class="pre">'sitecustomize'</span></code>,
  262. it is silently ignored. If Python is started without output streams available, as
  263. with <code class="file docutils literal notranslate"><span class="pre">pythonw.exe</span></code> on Windows (which is used by default to start IDLE),
  264. attempted output from <a class="reference internal" href="#module-sitecustomize" title="sitecustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sitecustomize</span></code></a> is ignored. Any other exception
  265. causes a silent and perhaps mysterious failure of the process.</p>
  266. </section>
  267. <section id="module-usercustomize">
  268. <span id="usercustomize"></span><h2><a class="reference internal" href="#module-usercustomize" title="usercustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">usercustomize</span></code></a><a class="headerlink" href="#module-usercustomize" title="Link to this heading">¶</a></h2>
  269. <p>After this, an attempt is made to import a module named <a class="reference internal" href="#module-usercustomize" title="usercustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">usercustomize</span></code></a>,
  270. which can perform arbitrary user-specific customizations, if
  271. <a class="reference internal" href="#site.ENABLE_USER_SITE" title="site.ENABLE_USER_SITE"><code class="xref py py-data docutils literal notranslate"><span class="pre">ENABLE_USER_SITE</span></code></a> is true. This file is intended to be created in the
  272. user site-packages directory (see below), which is part of <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> unless
  273. disabled by <a class="reference internal" href="../using/cmdline.html#cmdoption-s"><code class="xref std std-option docutils literal notranslate"><span class="pre">-s</span></code></a>. If this import fails with an <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> or
  274. its subclass exception, and the exception’s <a class="reference internal" href="exceptions.html#ImportError.name" title="ImportError.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a>
  275. attribute equals to <code class="docutils literal notranslate"><span class="pre">'usercustomize'</span></code>, it is silently ignored.</p>
  276. <p>Note that for some non-Unix systems, <code class="docutils literal notranslate"><span class="pre">sys.prefix</span></code> and <code class="docutils literal notranslate"><span class="pre">sys.exec_prefix</span></code> are
  277. empty, and the path manipulations are skipped; however the import of
  278. <a class="reference internal" href="#module-sitecustomize" title="sitecustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sitecustomize</span></code></a> and <a class="reference internal" href="#module-usercustomize" title="usercustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">usercustomize</span></code></a> is still attempted.</p>
  279. </section>
  280. <section id="readline-configuration">
  281. <span id="rlcompleter-config"></span><h2>Readline configuration<a class="headerlink" href="#readline-configuration" title="Link to this heading">¶</a></h2>
  282. <p>On systems that support <a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a>, this module will also import and
  283. configure the <a class="reference internal" href="rlcompleter.html#module-rlcompleter" title="rlcompleter: Python identifier completion, suitable for the GNU readline library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">rlcompleter</span></code></a> module, if Python is started in
  284. <a class="reference internal" href="../tutorial/interpreter.html#tut-interactive"><span class="std std-ref">interactive mode</span></a> and without the <a class="reference internal" href="../using/cmdline.html#cmdoption-S"><code class="xref std std-option docutils literal notranslate"><span class="pre">-S</span></code></a> option.
  285. The default behavior is enable tab-completion and to use
  286. <code class="file docutils literal notranslate"><span class="pre">~/.python_history</span></code> as the history save file. To disable it, delete (or
  287. override) the <a class="reference internal" href="sys.html#sys.__interactivehook__" title="sys.__interactivehook__"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.__interactivehook__</span></code></a> attribute in your
  288. <a class="reference internal" href="#module-sitecustomize" title="sitecustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sitecustomize</span></code></a> or <a class="reference internal" href="#module-usercustomize" title="usercustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">usercustomize</span></code></a> module or your
  289. <span class="target" id="index-5"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONSTARTUP"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONSTARTUP</span></code></a> file.</p>
  290. <div class="versionchanged">
  291. <p><span class="versionmodified changed">Changed in version 3.4: </span>Activation of rlcompleter and history was made automatic.</p>
  292. </div>
  293. </section>
  294. <section id="module-contents">
  295. <h2>Module contents<a class="headerlink" href="#module-contents" title="Link to this heading">¶</a></h2>
  296. <dl class="py data">
  297. <dt class="sig sig-object py" id="site.PREFIXES">
  298. <span class="sig-prename descclassname"><span class="pre">site.</span></span><span class="sig-name descname"><span class="pre">PREFIXES</span></span><a class="headerlink" href="#site.PREFIXES" title="Link to this definition">¶</a></dt>
  299. <dd><p>A list of prefixes for site-packages directories.</p>
  300. </dd></dl>
  301. <dl class="py data">
  302. <dt class="sig sig-object py" id="site.ENABLE_USER_SITE">
  303. <span class="sig-prename descclassname"><span class="pre">site.</span></span><span class="sig-name descname"><span class="pre">ENABLE_USER_SITE</span></span><a class="headerlink" href="#site.ENABLE_USER_SITE" title="Link to this definition">¶</a></dt>
  304. <dd><p>Flag showing the status of the user site-packages directory. <code class="docutils literal notranslate"><span class="pre">True</span></code> means
  305. that it is enabled and was added to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>. <code class="docutils literal notranslate"><span class="pre">False</span></code> means that it
  306. was disabled by user request (with <a class="reference internal" href="../using/cmdline.html#cmdoption-s"><code class="xref std std-option docutils literal notranslate"><span class="pre">-s</span></code></a> or
  307. <span class="target" id="index-6"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONNOUSERSITE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONNOUSERSITE</span></code></a>). <code class="docutils literal notranslate"><span class="pre">None</span></code> means it was disabled for security
  308. reasons (mismatch between user or group id and effective id) or by an
  309. administrator.</p>
  310. </dd></dl>
  311. <dl class="py data">
  312. <dt class="sig sig-object py" id="site.USER_SITE">
  313. <span class="sig-prename descclassname"><span class="pre">site.</span></span><span class="sig-name descname"><span class="pre">USER_SITE</span></span><a class="headerlink" href="#site.USER_SITE" title="Link to this definition">¶</a></dt>
  314. <dd><p>Path to the user site-packages for the running Python. Can be <code class="docutils literal notranslate"><span class="pre">None</span></code> if
  315. <a class="reference internal" href="#site.getusersitepackages" title="site.getusersitepackages"><code class="xref py py-func docutils literal notranslate"><span class="pre">getusersitepackages()</span></code></a> hasn’t been called yet. Default value is
  316. <code class="file docutils literal notranslate"><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
  317. macOS builds, <code class="file docutils literal notranslate"><span class="pre">~/Library/Python/</span><em><span class="pre">X.Y</span></em><span class="pre">/lib/python/site-packages</span></code> for macOS
  318. framework builds, and <code class="file docutils literal notranslate"><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>
  319. on Windows. This directory is a site directory, which means that
  320. <code class="file docutils literal notranslate"><span class="pre">.pth</span></code> files in it will be processed.</p>
  321. </dd></dl>
  322. <dl class="py data">
  323. <dt class="sig sig-object py" id="site.USER_BASE">
  324. <span class="sig-prename descclassname"><span class="pre">site.</span></span><span class="sig-name descname"><span class="pre">USER_BASE</span></span><a class="headerlink" href="#site.USER_BASE" title="Link to this definition">¶</a></dt>
  325. <dd><p>Path to the base directory for the user site-packages. Can be <code class="docutils literal notranslate"><span class="pre">None</span></code> if
  326. <a class="reference internal" href="#site.getuserbase" title="site.getuserbase"><code class="xref py py-func docutils literal notranslate"><span class="pre">getuserbase()</span></code></a> hasn’t been called yet. Default value is
  327. <code class="file docutils literal notranslate"><span class="pre">~/.local</span></code> for UNIX and macOS non-framework builds,
  328. <code class="file docutils literal notranslate"><span class="pre">~/Library/Python/</span><em><span class="pre">X.Y</span></em></code> for macOS framework builds, and
  329. <code class="file docutils literal notranslate"><em><span class="pre">%APPDATA%</span></em><span class="pre">\Python</span></code> for Windows. This value is used to
  330. compute the installation directories for scripts, data files, Python modules,
  331. etc. for the <a class="reference internal" href="sysconfig.html#sysconfig-user-scheme"><span class="std std-ref">user installation scheme</span></a>.
  332. See also <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 notranslate"><span class="pre">PYTHONUSERBASE</span></code></a>.</p>
  333. </dd></dl>
  334. <dl class="py function">
  335. <dt class="sig sig-object py" id="site.main">
  336. <span class="sig-prename descclassname"><span class="pre">site.</span></span><span class="sig-name descname"><span class="pre">main</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#site.main" title="Link to this definition">¶</a></dt>
  337. <dd><p>Adds all the standard site-specific directories to the module search
  338. path. This function is called automatically when this module is imported,
  339. unless the Python interpreter was started with the <a class="reference internal" href="../using/cmdline.html#cmdoption-S"><code class="xref std std-option docutils literal notranslate"><span class="pre">-S</span></code></a> flag.</p>
  340. <div class="versionchanged">
  341. <p><span class="versionmodified changed">Changed in version 3.3: </span>This function used to be called unconditionally.</p>
  342. </div>
  343. </dd></dl>
  344. <dl class="py function">
  345. <dt class="sig sig-object py" id="site.addsitedir">
  346. <span class="sig-prename descclassname"><span class="pre">site.</span></span><span class="sig-name descname"><span class="pre">addsitedir</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sitedir</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">known_paths</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#site.addsitedir" title="Link to this definition">¶</a></dt>
  347. <dd><p>Add a directory to sys.path and process its <code class="file docutils literal notranslate"><span class="pre">.pth</span></code> files. Typically
  348. used in <a class="reference internal" href="#module-sitecustomize" title="sitecustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sitecustomize</span></code></a> or <a class="reference internal" href="#module-usercustomize" title="usercustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">usercustomize</span></code></a> (see above).</p>
  349. </dd></dl>
  350. <dl class="py function">
  351. <dt class="sig sig-object py" id="site.getsitepackages">
  352. <span class="sig-prename descclassname"><span class="pre">site.</span></span><span class="sig-name descname"><span class="pre">getsitepackages</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#site.getsitepackages" title="Link to this definition">¶</a></dt>
  353. <dd><p>Return a list containing all global site-packages directories.</p>
  354. <div class="versionadded">
  355. <p><span class="versionmodified added">New in version 3.2.</span></p>
  356. </div>
  357. </dd></dl>
  358. <dl class="py function">
  359. <dt class="sig sig-object py" id="site.getuserbase">
  360. <span class="sig-prename descclassname"><span class="pre">site.</span></span><span class="sig-name descname"><span class="pre">getuserbase</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#site.getuserbase" title="Link to this definition">¶</a></dt>
  361. <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 notranslate"><span class="pre">USER_BASE</span></code></a>. If it is not
  362. initialized yet, this function will also set it, respecting
  363. <span class="target" id="index-8"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONUSERBASE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONUSERBASE</span></code></a>.</p>
  364. <div class="versionadded">
  365. <p><span class="versionmodified added">New in version 3.2.</span></p>
  366. </div>
  367. </dd></dl>
  368. <dl class="py function">
  369. <dt class="sig sig-object py" id="site.getusersitepackages">
  370. <span class="sig-prename descclassname"><span class="pre">site.</span></span><span class="sig-name descname"><span class="pre">getusersitepackages</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#site.getusersitepackages" title="Link to this definition">¶</a></dt>
  371. <dd><p>Return the path of the user-specific site-packages directory,
  372. <a class="reference internal" href="#site.USER_SITE" title="site.USER_SITE"><code class="xref py py-data docutils literal notranslate"><span class="pre">USER_SITE</span></code></a>. If it is not initialized yet, this function will also set
  373. it, respecting <a class="reference internal" href="#site.USER_BASE" title="site.USER_BASE"><code class="xref py py-data docutils literal notranslate"><span class="pre">USER_BASE</span></code></a>. To determine if the user-specific
  374. site-packages was added to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> <a class="reference internal" href="#site.ENABLE_USER_SITE" title="site.ENABLE_USER_SITE"><code class="xref py py-data docutils literal notranslate"><span class="pre">ENABLE_USER_SITE</span></code></a> should be
  375. used.</p>
  376. <div class="versionadded">
  377. <p><span class="versionmodified added">New in version 3.2.</span></p>
  378. </div>
  379. </dd></dl>
  380. </section>
  381. <section id="command-line-interface">
  382. <span id="site-commandline"></span><h2>Command Line Interface<a class="headerlink" href="#command-line-interface" title="Link to this heading">¶</a></h2>
  383. <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 notranslate"><span class="pre">site</span></code></a> module also provides a way to get the user directories from the
  384. command line:</p>
  385. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>site<span class="w"> </span>--user-site
  386. <span class="go">/home/user/.local/lib/python3.11/site-packages</span>
  387. </pre></div>
  388. </div>
  389. <p>If it is called without arguments, it will print the contents of
  390. <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> on the standard output, followed by the value of
  391. <a class="reference internal" href="#site.USER_BASE" title="site.USER_BASE"><code class="xref py py-data docutils literal notranslate"><span class="pre">USER_BASE</span></code></a> and whether the directory exists, then the same thing for
  392. <a class="reference internal" href="#site.USER_SITE" title="site.USER_SITE"><code class="xref py py-data docutils literal notranslate"><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 notranslate"><span class="pre">ENABLE_USER_SITE</span></code></a>.</p>
  393. <dl class="std option">
  394. <dt class="sig sig-object std" id="cmdoption-site-user-base">
  395. <span class="sig-name descname"><span class="pre">--user-base</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-site-user-base" title="Link to this definition">¶</a></dt>
  396. <dd><p>Print the path to the user base directory.</p>
  397. </dd></dl>
  398. <dl class="std option">
  399. <dt class="sig sig-object std" id="cmdoption-site-user-site">
  400. <span class="sig-name descname"><span class="pre">--user-site</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-site-user-site" title="Link to this definition">¶</a></dt>
  401. <dd><p>Print the path to the user site-packages directory.</p>
  402. </dd></dl>
  403. <p>If both options are given, user base and user site will be printed (always in
  404. this order), separated by <a class="reference internal" href="os.html#os.pathsep" title="os.pathsep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.pathsep</span></code></a>.</p>
  405. <p>If any option is given, the script will exit with one of these values: <code class="docutils literal notranslate"><span class="pre">0</span></code> if
  406. the user site-packages directory is enabled, <code class="docutils literal notranslate"><span class="pre">1</span></code> if it was disabled by the
  407. user, <code class="docutils literal notranslate"><span class="pre">2</span></code> if it is disabled for security reasons or by an administrator, and a
  408. value greater than 2 if there is an error.</p>
  409. <div class="admonition seealso">
  410. <p class="admonition-title">See also</p>
  411. <ul class="simple">
  412. <li><p><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</p></li>
  413. <li><p><a class="reference internal" href="sys_path_init.html#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.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>.</p></li>
  414. </ul>
  415. </div>
  416. </section>
  417. </section>
  418. <div class="clearer"></div>
  419. </div>
  420. </div>
  421. </div>
  422. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  423. <div class="sphinxsidebarwrapper">
  424. <div>
  425. <h3><a href="../contents.html">Table of Contents</a></h3>
  426. <ul>
  427. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code> — Site-specific configuration hook</a><ul>
  428. <li><a class="reference internal" href="#module-sitecustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sitecustomize</span></code></a></li>
  429. <li><a class="reference internal" href="#module-usercustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">usercustomize</span></code></a></li>
  430. <li><a class="reference internal" href="#readline-configuration">Readline configuration</a></li>
  431. <li><a class="reference internal" href="#module-contents">Module contents</a></li>
  432. <li><a class="reference internal" href="#command-line-interface">Command Line Interface</a></li>
  433. </ul>
  434. </li>
  435. </ul>
  436. </div>
  437. <div>
  438. <h4>Previous topic</h4>
  439. <p class="topless"><a href="inspect.html"
  440. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code> — Inspect live objects</a></p>
  441. </div>
  442. <div>
  443. <h4>Next topic</h4>
  444. <p class="topless"><a href="custominterp.html"
  445. title="next chapter">Custom Python Interpreters</a></p>
  446. </div>
  447. <div role="note" aria-label="source link">
  448. <h3>This Page</h3>
  449. <ul class="this-page-menu">
  450. <li><a href="../bugs.html">Report a Bug</a></li>
  451. <li>
  452. <a href="https://github.com/python/cpython/blob/main/Doc/library/site.rst"
  453. rel="nofollow">Show Source
  454. </a>
  455. </li>
  456. </ul>
  457. </div>
  458. </div>
  459. <div id="sidebarbutton" title="Collapse sidebar">
  460. <span>«</span>
  461. </div>
  462. </div>
  463. <div class="clearer"></div>
  464. </div>
  465. <div class="related" role="navigation" aria-label="related navigation">
  466. <h3>Navigation</h3>
  467. <ul>
  468. <li class="right" style="margin-right: 10px">
  469. <a href="../genindex.html" title="General Index"
  470. >index</a></li>
  471. <li class="right" >
  472. <a href="../py-modindex.html" title="Python Module Index"
  473. >modules</a> |</li>
  474. <li class="right" >
  475. <a href="custominterp.html" title="Custom Python Interpreters"
  476. >next</a> |</li>
  477. <li class="right" >
  478. <a href="inspect.html" title="inspect — Inspect live objects"
  479. >previous</a> |</li>
  480. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  481. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  482. <li class="switchers">
  483. <div class="language_switcher_placeholder"></div>
  484. <div class="version_switcher_placeholder"></div>
  485. </li>
  486. <li>
  487. </li>
  488. <li id="cpython-language-and-version">
  489. <a href="../index.html">3.12.3 Documentation</a> &#187;
  490. </li>
  491. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  492. <li class="nav-item nav-item-2"><a href="python.html" >Python Runtime Services</a> &#187;</li>
  493. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code> — Site-specific configuration hook</a></li>
  494. <li class="right">
  495. <div class="inline-search" role="search">
  496. <form class="inline-search" action="../search.html" method="get">
  497. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  498. <input type="submit" value="Go" />
  499. </form>
  500. </div>
  501. |
  502. </li>
  503. <li class="right">
  504. <label class="theme-selector-label">
  505. Theme
  506. <select class="theme-selector" oninput="activateTheme(this.value)">
  507. <option value="auto" selected>Auto</option>
  508. <option value="light">Light</option>
  509. <option value="dark">Dark</option>
  510. </select>
  511. </label> |</li>
  512. </ul>
  513. </div>
  514. <div class="footer">
  515. &copy;
  516. <a href="../copyright.html">
  517. Copyright
  518. </a>
  519. 2001-2024, Python Software Foundation.
  520. <br />
  521. This page is licensed under the Python Software Foundation License Version 2.
  522. <br />
  523. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  524. <br />
  525. See <a href="/license.html">History and License</a> for more information.<br />
  526. <br />
  527. The Python Software Foundation is a non-profit corporation.
  528. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  529. <br />
  530. <br />
  531. Last updated on Apr 09, 2024 (13:47 UTC).
  532. <a href="/bugs.html">Found a bug</a>?
  533. <br />
  534. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  535. </div>
  536. </body>
  537. </html>
上海开阖软件有限公司 沪ICP备12045867号-1