diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Frunpy.html | |
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Frunpy.html')
| -rw-r--r-- | devdocs/python~3.12/library%2Frunpy.html | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Frunpy.html b/devdocs/python~3.12/library%2Frunpy.html new file mode 100644 index 00000000..c5abfda7 --- /dev/null +++ b/devdocs/python~3.12/library%2Frunpy.html @@ -0,0 +1,21 @@ + <span id="runpy-locating-and-executing-python-modules"></span><h1>runpy — Locating and executing Python modules</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/runpy.py">Lib/runpy.py</a></p> <p>The <a class="reference internal" href="#module-runpy" title="runpy: Locate and run Python modules without importing them first."><code>runpy</code></a> module is used to locate and run Python modules without importing them first. Its main use is to implement the <a class="reference internal" href="../using/cmdline#cmdoption-m"><code>-m</code></a> command line switch that allows scripts to be located using the Python module namespace rather than the filesystem.</p> <p>Note that this is <em>not</em> a sandbox module - all code is executed in the current process, and any side effects (such as cached imports of other modules) will remain in place after the functions have returned.</p> <p>Furthermore, any functions and classes defined by the executed code are not guaranteed to work correctly after a <a class="reference internal" href="#module-runpy" title="runpy: Locate and run Python modules without importing them first."><code>runpy</code></a> function has returned. If that limitation is not acceptable for a given use case, <a class="reference internal" href="importlib#module-importlib" title="importlib: The implementation of the import machinery."><code>importlib</code></a> is likely to be a more suitable choice than this module.</p> <p>The <a class="reference internal" href="#module-runpy" title="runpy: Locate and run Python modules without importing them first."><code>runpy</code></a> module provides two functions:</p> <dl class="py function"> <dt class="sig sig-object py" id="runpy.run_module"> +<code>runpy.run_module(mod_name, init_globals=None, run_name=None, alter_sys=False)</code> </dt> <dd> +<p id="index-0">Execute the code of the specified module and return the resulting module globals dictionary. The module’s code is first located using the standard import mechanism (refer to <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a> for details) and then executed in a fresh module namespace.</p> <p>The <em>mod_name</em> argument should be an absolute module name. If the module name refers to a package rather than a normal module, then that package is imported and the <a class="reference internal" href="__main__#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code>__main__</code></a> submodule within that package is then executed and the resulting module globals dictionary returned.</p> <p>The optional dictionary argument <em>init_globals</em> may be used to pre-populate the module’s globals dictionary before the code is executed. The supplied dictionary will not be modified. If any of the special global variables below are defined in the supplied dictionary, those definitions are overridden by <a class="reference internal" href="#runpy.run_module" title="runpy.run_module"><code>run_module()</code></a>.</p> <p>The special global variables <code>__name__</code>, <code>__spec__</code>, <code>__file__</code>, <code>__cached__</code>, <code>__loader__</code> and <code>__package__</code> are set in the globals dictionary before the module code is executed (Note that this is a minimal set of variables - other variables may be set implicitly as an interpreter implementation detail).</p> <p><code>__name__</code> is set to <em>run_name</em> if this optional argument is not <a class="reference internal" href="constants#None" title="None"><code>None</code></a>, to <code>mod_name + '.__main__'</code> if the named module is a package and to the <em>mod_name</em> argument otherwise.</p> <p><code>__spec__</code> will be set appropriately for the <em>actually</em> imported module (that is, <code>__spec__.name</code> will always be <em>mod_name</em> or <code>mod_name + '.__main__</code>, never <em>run_name</em>).</p> <p><code>__file__</code>, <code>__cached__</code>, <code>__loader__</code> and <code>__package__</code> are <a class="reference internal" href="../reference/import#import-mod-attrs"><span class="std std-ref">set as normal</span></a> based on the module spec.</p> <p>If the argument <em>alter_sys</em> is supplied and evaluates to <a class="reference internal" href="constants#True" title="True"><code>True</code></a>, then <code>sys.argv[0]</code> is updated with the value of <code>__file__</code> and <code>sys.modules[__name__]</code> is updated with a temporary module object for the module being executed. Both <code>sys.argv[0]</code> and <code>sys.modules[__name__]</code> are restored to their original values before the function returns.</p> <p>Note that this manipulation of <a class="reference internal" href="sys#module-sys" title="sys: Access system-specific parameters and functions."><code>sys</code></a> is not thread-safe. Other threads may see the partially initialised module, as well as the altered list of arguments. It is recommended that the <code>sys</code> module be left alone when invoking this function from threaded code.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p>The <a class="reference internal" href="../using/cmdline#cmdoption-m"><code>-m</code></a> option offering equivalent functionality from the command line.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.1: </span>Added ability to execute packages by looking for a <a class="reference internal" href="__main__#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code>__main__</code></a> submodule.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>Added <code>__cached__</code> global variable (see <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-3147/"><strong>PEP 3147</strong></a>).</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>Updated to take advantage of the module spec feature added by <span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0451/"><strong>PEP 451</strong></a>. This allows <code>__cached__</code> to be set correctly for modules run this way, as well as ensuring the real module name is always accessible as <code>__spec__.name</code>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>The setting of <code>__cached__</code>, <code>__loader__</code>, and <code>__package__</code> are deprecated. See <a class="reference internal" href="importlib#importlib.machinery.ModuleSpec" title="importlib.machinery.ModuleSpec"><code>ModuleSpec</code></a> for alternatives.</p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="runpy.run_path"> +<code>runpy.run_path(path_name, init_globals=None, run_name=None)</code> </dt> <dd> +<p id="index-4">Execute the code at the named filesystem location and return the resulting module globals dictionary. As with a script name supplied to the CPython command line, the supplied path may refer to a Python source file, a compiled bytecode file or a valid <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</code></a> entry containing a <a class="reference internal" href="__main__#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code>__main__</code></a> module (e.g. a zipfile containing a top-level <code>__main__.py</code> file).</p> <p>For a simple script, the specified code is simply executed in a fresh module namespace. For a valid <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</code></a> entry (typically a zipfile or directory), the entry is first added to the beginning of <code>sys.path</code>. The function then looks for and executes a <a class="reference internal" href="__main__#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code>__main__</code></a> module using the updated path. Note that there is no special protection against invoking an existing <code>__main__</code> entry located elsewhere on <code>sys.path</code> if there is no such module at the specified location.</p> <p>The optional dictionary argument <em>init_globals</em> may be used to pre-populate the module’s globals dictionary before the code is executed. The supplied dictionary will not be modified. If any of the special global variables below are defined in the supplied dictionary, those definitions are overridden by <a class="reference internal" href="#runpy.run_path" title="runpy.run_path"><code>run_path()</code></a>.</p> <p>The special global variables <code>__name__</code>, <code>__spec__</code>, <code>__file__</code>, <code>__cached__</code>, <code>__loader__</code> and <code>__package__</code> are set in the globals dictionary before the module code is executed (Note that this is a minimal set of variables - other variables may be set implicitly as an interpreter implementation detail).</p> <p><code>__name__</code> is set to <em>run_name</em> if this optional argument is not <a class="reference internal" href="constants#None" title="None"><code>None</code></a> and to <code>'<run_path>'</code> otherwise.</p> <p>If the supplied path directly references a script file (whether as source or as precompiled byte code), then <code>__file__</code> will be set to the supplied path, and <code>__spec__</code>, <code>__cached__</code>, <code>__loader__</code> and <code>__package__</code> will all be set to <a class="reference internal" href="constants#None" title="None"><code>None</code></a>.</p> <p>If the supplied path is a reference to a valid <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</code></a> entry, then <code>__spec__</code> will be set appropriately for the imported <a class="reference internal" href="__main__#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code>__main__</code></a> module (that is, <code>__spec__.name</code> will always be <code>__main__</code>). <code>__file__</code>, <code>__cached__</code>, <code>__loader__</code> and <code>__package__</code> will be <a class="reference internal" href="../reference/import#import-mod-attrs"><span class="std std-ref">set as normal</span></a> based on the module spec.</p> <p>A number of alterations are also made to the <a class="reference internal" href="sys#module-sys" title="sys: Access system-specific parameters and functions."><code>sys</code></a> module. Firstly, <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</code></a> may be altered as described above. <code>sys.argv[0]</code> is updated with the value of <code>path_name</code> and <code>sys.modules[__name__]</code> is updated with a temporary module object for the module being executed. All modifications to items in <a class="reference internal" href="sys#module-sys" title="sys: Access system-specific parameters and functions."><code>sys</code></a> are reverted before the function returns.</p> <p>Note that, unlike <a class="reference internal" href="#runpy.run_module" title="runpy.run_module"><code>run_module()</code></a>, the alterations made to <a class="reference internal" href="sys#module-sys" title="sys: Access system-specific parameters and functions."><code>sys</code></a> are not optional in this function as these adjustments are essential to allowing the execution of <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</code></a> entries. As the thread-safety limitations still apply, use of this function in threaded code should be either serialised with the import lock or delegated to a separate process.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="../using/cmdline#using-on-interface-options"><span class="std std-ref">Interface options</span></a> for equivalent functionality on the command line (<code>python path/to/script</code>).</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>Updated to take advantage of the module spec feature added by <span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0451/"><strong>PEP 451</strong></a>. This allows <code>__cached__</code> to be set correctly in the case where <code>__main__</code> is imported from a valid <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</code></a> entry rather than being executed directly.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>The setting of <code>__cached__</code>, <code>__loader__</code>, and <code>__package__</code> are deprecated.</p> </div> </dd> +</dl> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt> +<span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0338/"><strong>PEP 338</strong></a> – Executing modules as scripts</dt> +<dd> +<p>PEP written and implemented by Nick Coghlan.</p> </dd> <dt> +<span class="target" id="index-7"></span><a class="pep reference external" href="https://peps.python.org/pep-0366/"><strong>PEP 366</strong></a> – Main module explicit relative imports</dt> +<dd> +<p>PEP written and implemented by Nick Coghlan.</p> </dd> <dt> +<span class="target" id="index-8"></span><a class="pep reference external" href="https://peps.python.org/pep-0451/"><strong>PEP 451</strong></a> – A ModuleSpec Type for the Import System</dt> +<dd> +<p>PEP written and implemented by Eric Snow</p> </dd> </dl> <p><a class="reference internal" href="../using/cmdline#using-on-general"><span class="std std-ref">Command line and environment</span></a> - CPython command line details</p> <p>The <a class="reference internal" href="importlib#importlib.import_module" title="importlib.import_module"><code>importlib.import_module()</code></a> function</p> </div> <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/runpy.html" class="_attribution-link">https://docs.python.org/3.12/library/runpy.html</a> + </p> +</div> |
