summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Fsys_path_init.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Fsys_path_init.html
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Fsys_path_init.html')
-rw-r--r--devdocs/python~3.12/library%2Fsys_path_init.html8
1 files changed, 8 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fsys_path_init.html b/devdocs/python~3.12/library%2Fsys_path_init.html
new file mode 100644
index 00000000..04e77007
--- /dev/null
+++ b/devdocs/python~3.12/library%2Fsys_path_init.html
@@ -0,0 +1,8 @@
+ <span id="sys-path-init"></span><h1>The initialization of the sys.path module search path</h1> <p>A module search path is initialized when Python starts. This module search path may be accessed at <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</code></a>.</p> <p>The first entry in the module search path is the directory that contains the input script, if there is one. Otherwise, the first entry is the current directory, which is the case when executing the interactive shell, a <a class="reference internal" href="../using/cmdline#cmdoption-c"><code>-c</code></a> command, or <a class="reference internal" href="../using/cmdline#cmdoption-m"><code>-m</code></a> module.</p> <p>The <span class="target" id="index-0"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONPATH"><code>PYTHONPATH</code></a> environment variable is often used to add directories to the search path. If this environment variable is found then the contents are added to the module search path.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p><span class="target" id="index-1"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONPATH"><code>PYTHONPATH</code></a> will affect all installed Python versions/environments. Be wary of setting this in your shell profile or global environment variables. The <a class="reference internal" href="site#module-site" title="site: Module responsible for site-specific configuration."><code>site</code></a> module offers more nuanced techniques as mentioned below.</p> </div> <p>The next items added are the directories containing standard Python modules as well as any <a class="reference internal" href="../glossary#term-extension-module"><span class="xref std std-term">extension module</span></a>s that these modules depend on. Extension modules are <code>.pyd</code> files on Windows and <code>.so</code> files on other platforms. The directory with the platform-independent Python modules is called <code>prefix</code>. The directory with the extension modules is called <code>exec_prefix</code>.</p> <p>The <span class="target" id="index-2"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONHOME"><code>PYTHONHOME</code></a> environment variable may be used to set the <code>prefix</code> and <code>exec_prefix</code> locations. Otherwise these directories are found by using the Python executable as a starting point and then looking for various ‘landmark’ files and directories. Note that any symbolic links are followed so the real Python executable location is used as the search starting point. The Python executable location is called <code>home</code>.</p> <p>Once <code>home</code> is determined, the <code>prefix</code> directory is found by first looking for <code>python<em>majorversion</em><em>minorversion</em>.zip</code> (<code>python311.zip</code>). On Windows the zip archive is searched for in <code>home</code> and on Unix the archive is expected to be in <code>lib</code>. Note that the expected zip archive location is added to the module search path even if the archive does not exist. If no archive was found, Python on Windows will continue the search for <code>prefix</code> by looking for <code>Lib\os.py</code>. Python on Unix will look for <code>lib/python<em>majorversion</em>.<em>minorversion</em>/os.py</code> (<code>lib/python3.11/os.py</code>). On Windows <code>prefix</code> and <code>exec_prefix</code> are the same, however on other platforms <code>lib/python<em>majorversion</em>.<em>minorversion</em>/lib-dynload</code> (<code>lib/python3.11/lib-dynload</code>) is searched for and used as an anchor for <code>exec_prefix</code>. On some platforms <code>lib</code> may be <code>lib64</code> or another value, see <a class="reference internal" href="sys#sys.platlibdir" title="sys.platlibdir"><code>sys.platlibdir</code></a> and <span class="target" id="index-3"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONPLATLIBDIR"><code>PYTHONPLATLIBDIR</code></a>.</p> <p>Once found, <code>prefix</code> and <code>exec_prefix</code> are available at <a class="reference internal" href="sys#sys.prefix" title="sys.prefix"><code>sys.prefix</code></a> and <a class="reference internal" href="sys#sys.exec_prefix" title="sys.exec_prefix"><code>sys.exec_prefix</code></a> respectively.</p> <p>Finally, the <a class="reference internal" href="site#module-site" title="site: Module responsible for site-specific configuration."><code>site</code></a> module is processed and <code>site-packages</code> directories are added to the module search path. A common way to customize the search path is to create <a class="reference internal" href="site#module-sitecustomize" title="sitecustomize"><code>sitecustomize</code></a> or <a class="reference internal" href="site#module-usercustomize" title="usercustomize"><code>usercustomize</code></a> modules as described in the <a class="reference internal" href="site#module-site" title="site: Module responsible for site-specific configuration."><code>site</code></a> module documentation.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Certain command line options may further affect path calculations. See <a class="reference internal" href="../using/cmdline#cmdoption-E"><code>-E</code></a>, <a class="reference internal" href="../using/cmdline#cmdoption-I"><code>-I</code></a>, <a class="reference internal" href="../using/cmdline#cmdoption-s"><code>-s</code></a> and <a class="reference internal" href="../using/cmdline#cmdoption-S"><code>-S</code></a> for further details.</p> </div> <section id="virtual-environments"> <h2>Virtual environments</h2> <p>If Python is run in a virtual environment (as described at <a class="reference internal" href="../tutorial/venv#tut-venv"><span class="std std-ref">Virtual Environments and Packages</span></a>) then <code>prefix</code> and <code>exec_prefix</code> are specific to the virtual environment.</p> <p>If a <code>pyvenv.cfg</code> file is found alongside the main executable, or in the directory one level above the executable, the following variations apply:</p> <ul class="simple"> <li>If <code>home</code> is an absolute path and <span class="target" id="index-4"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONHOME"><code>PYTHONHOME</code></a> is not set, this path is used instead of the path to the main executable when deducing <code>prefix</code> and <code>exec_prefix</code>.</li> </ul> </section> <section id="pth-files"> <h2>_pth files</h2> <p>To completely override <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</code></a> create a <code>._pth</code> file with the same name as the shared library or executable (<code>python._pth</code> or <code>python311._pth</code>). The shared library path is always known on Windows, however it may not be available on other platforms. In the <code>._pth</code> file specify one line for each path to add to <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</code></a>. The file based on the shared library name overrides the one based on the executable, which allows paths to be restricted for any program loading the runtime if desired.</p> <p>When the file exists, all registry and environment variables are ignored, isolated mode is enabled, and <a class="reference internal" href="site#module-site" title="site: Module responsible for site-specific configuration."><code>site</code></a> is not imported unless one line in the file specifies <code>import site</code>. Blank paths and lines starting with <code>#</code> are ignored. Each path may be absolute or relative to the location of the file. Import statements other than to <code>site</code> are not permitted, and arbitrary code cannot be specified.</p> <p>Note that <code>.pth</code> files (without leading underscore) will be processed normally by the <a class="reference internal" href="site#module-site" title="site: Module responsible for site-specific configuration."><code>site</code></a> module when <code>import site</code> has been specified.</p> </section> <section id="embedded-python"> <h2>Embedded Python</h2> <p>If Python is embedded within another application <a class="reference internal" href="../c-api/init_config#c.Py_InitializeFromConfig" title="Py_InitializeFromConfig"><code>Py_InitializeFromConfig()</code></a> and the <a class="reference internal" href="../c-api/init_config#c.PyConfig" title="PyConfig"><code>PyConfig</code></a> structure can be used to initialize Python. The path specific details are described at <a class="reference internal" href="../c-api/init_config#init-path-config"><span class="std std-ref">Python Path Configuration</span></a>. Alternatively the older <a class="reference internal" href="../c-api/init#c.Py_SetPath" title="Py_SetPath"><code>Py_SetPath()</code></a> can be used to bypass the initialization of the module search path.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <ul class="simple"> <li>
+<a class="reference internal" href="../using/windows#windows-finding-modules"><span class="std std-ref">Finding modules</span></a> for detailed Windows notes.</li> <li>
+<a class="reference internal" href="../using/unix#using-on-unix"><span class="std std-ref">Using Python on Unix platforms</span></a> for Unix details.</li> </ul> </div> </section> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
+ <a href="https://docs.python.org/3.12/library/sys_path_init.html" class="_attribution-link">https://docs.python.org/3.12/library/sys_path_init.html</a>
+ </p>
+</div>