gooderp18绿色标准版
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

468 rindas
36KB

  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="runpy — Locating and executing Python modules" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/runpy.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/runpy.py The runpy module is used to locate and run Python modules without importing them first. Its main use is to implement the-m command line switch that allows scripts to be lo..." />
  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/runpy.py The runpy module is used to locate and run Python modules without importing them first. Its main use is to implement the-m command line switch that allows scripts to be lo..." />
  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>runpy — Locating and executing Python modules &#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="importlib — The implementation of import" href="importlib.html" />
  33. <link rel="prev" title="modulefinder — Find modules used by a script" href="modulefinder.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/runpy.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. <h4>Previous topic</h4>
  84. <p class="topless"><a href="modulefinder.html"
  85. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">modulefinder</span></code> — Find modules used by a script</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="importlib.html"
  90. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code> — The implementation of <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/library/runpy.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="importlib.html" title="importlib — The implementation of import"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="modulefinder.html" title="modulefinder — Find modules used by a script"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.3 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  133. <li class="nav-item nav-item-2"><a href="modules.html" accesskey="U">Importing Modules</a> &#187;</li>
  134. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">runpy</span></code> — Locating and executing Python modules</a></li>
  135. <li class="right">
  136. <div class="inline-search" role="search">
  137. <form class="inline-search" action="../search.html" method="get">
  138. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  139. <input type="submit" value="Go" />
  140. </form>
  141. </div>
  142. |
  143. </li>
  144. <li class="right">
  145. <label class="theme-selector-label">
  146. Theme
  147. <select class="theme-selector" oninput="activateTheme(this.value)">
  148. <option value="auto" selected>Auto</option>
  149. <option value="light">Light</option>
  150. <option value="dark">Dark</option>
  151. </select>
  152. </label> |</li>
  153. </ul>
  154. </div>
  155. <div class="document">
  156. <div class="documentwrapper">
  157. <div class="bodywrapper">
  158. <div class="body" role="main">
  159. <section id="module-runpy">
  160. <span id="runpy-locating-and-executing-python-modules"></span><h1><a class="reference internal" href="#module-runpy" title="runpy: Locate and run Python modules without importing them first."><code class="xref py py-mod docutils literal notranslate"><span class="pre">runpy</span></code></a> — Locating and executing Python modules<a class="headerlink" href="#module-runpy" title="Link to this heading">¶</a></h1>
  161. <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>
  162. <hr class="docutils" />
  163. <p>The <a class="reference internal" href="#module-runpy" title="runpy: Locate and run Python modules without importing them first."><code class="xref py py-mod docutils literal notranslate"><span class="pre">runpy</span></code></a> module is used to locate and run Python modules without
  164. importing them first. Its main use is to implement the <a class="reference internal" href="../using/cmdline.html#cmdoption-m"><code class="xref std std-option docutils literal notranslate"><span class="pre">-m</span></code></a> command
  165. line switch that allows scripts to be located using the Python module
  166. namespace rather than the filesystem.</p>
  167. <p>Note that this is <em>not</em> a sandbox module - all code is executed in the
  168. current process, and any side effects (such as cached imports of other
  169. modules) will remain in place after the functions have returned.</p>
  170. <p>Furthermore, any functions and classes defined by the executed code are not
  171. 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 class="xref py py-mod docutils literal notranslate"><span class="pre">runpy</span></code></a> function has returned.
  172. If that limitation is not acceptable for a given use case, <a class="reference internal" href="importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a>
  173. is likely to be a more suitable choice than this module.</p>
  174. <p>The <a class="reference internal" href="#module-runpy" title="runpy: Locate and run Python modules without importing them first."><code class="xref py py-mod docutils literal notranslate"><span class="pre">runpy</span></code></a> module provides two functions:</p>
  175. <dl class="py function">
  176. <dt class="sig sig-object py" id="runpy.run_module">
  177. <span class="sig-prename descclassname"><span class="pre">runpy.</span></span><span class="sig-name descname"><span class="pre">run_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mod_name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">init_globals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">run_name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">alter_sys</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#runpy.run_module" title="Link to this definition">¶</a></dt>
  178. <dd><p id="index-0">Execute the code of the specified module and return the resulting module
  179. globals dictionary. The module’s code is first located using the standard
  180. 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
  181. fresh module namespace.</p>
  182. <p>The <em>mod_name</em> argument should be an absolute module name.
  183. If the module name refers to a package rather than a normal
  184. module, then that package is imported and the <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> submodule within
  185. that package is then executed and the resulting module globals dictionary
  186. returned.</p>
  187. <p>The optional dictionary argument <em>init_globals</em> may be used to pre-populate
  188. the module’s globals dictionary before the code is executed. The supplied
  189. dictionary will not be modified. If any of the special global variables
  190. below are defined in the supplied dictionary, those definitions are
  191. overridden by <a class="reference internal" href="#runpy.run_module" title="runpy.run_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">run_module()</span></code></a>.</p>
  192. <p>The special global variables <code class="docutils literal notranslate"><span class="pre">__name__</span></code>, <code class="docutils literal notranslate"><span class="pre">__spec__</span></code>, <code class="docutils literal notranslate"><span class="pre">__file__</span></code>,
  193. <code class="docutils literal notranslate"><span class="pre">__cached__</span></code>, <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> and <code class="docutils literal notranslate"><span class="pre">__package__</span></code> are set in the globals
  194. dictionary before the module code is executed (Note that this is a
  195. minimal set of variables - other variables may be set implicitly as an
  196. interpreter implementation detail).</p>
  197. <p><code class="docutils literal notranslate"><span class="pre">__name__</span></code> is set to <em>run_name</em> if this optional argument is not
  198. <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>, to <code class="docutils literal notranslate"><span class="pre">mod_name</span> <span class="pre">+</span> <span class="pre">'.__main__'</span></code> if the named module is a
  199. package and to the <em>mod_name</em> argument otherwise.</p>
  200. <p><code class="docutils literal notranslate"><span class="pre">__spec__</span></code> will be set appropriately for the <em>actually</em> imported
  201. module (that is, <code class="docutils literal notranslate"><span class="pre">__spec__.name</span></code> will always be <em>mod_name</em> or
  202. <code class="docutils literal notranslate"><span class="pre">mod_name</span> <span class="pre">+</span> <span class="pre">'.__main__</span></code>, never <em>run_name</em>).</p>
  203. <p><code class="docutils literal notranslate"><span class="pre">__file__</span></code>, <code class="docutils literal notranslate"><span class="pre">__cached__</span></code>, <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> and <code class="docutils literal notranslate"><span class="pre">__package__</span></code> are
  204. <a class="reference internal" href="../reference/import.html#import-mod-attrs"><span class="std std-ref">set as normal</span></a> based on the module spec.</p>
  205. <p>If the argument <em>alter_sys</em> is supplied and evaluates to <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>,
  206. then <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> is updated with the value of <code class="docutils literal notranslate"><span class="pre">__file__</span></code> and
  207. <code class="docutils literal notranslate"><span class="pre">sys.modules[__name__]</span></code> is updated with a temporary module object for the
  208. module being executed. Both <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> and <code class="docutils literal notranslate"><span class="pre">sys.modules[__name__]</span></code>
  209. are restored to their original values before the function returns.</p>
  210. <p>Note that this manipulation of <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> is not thread-safe. Other threads
  211. may see the partially initialised module, as well as the altered list of
  212. arguments. It is recommended that the <code class="docutils literal notranslate"><span class="pre">sys</span></code> module be left alone when
  213. invoking this function from threaded code.</p>
  214. <div class="admonition seealso">
  215. <p class="admonition-title">See also</p>
  216. <p>The <a class="reference internal" href="../using/cmdline.html#cmdoption-m"><code class="xref std std-option docutils literal notranslate"><span class="pre">-m</span></code></a> option offering equivalent functionality from the
  217. command line.</p>
  218. </div>
  219. <div class="versionchanged">
  220. <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__.html#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> submodule.</p>
  221. </div>
  222. <div class="versionchanged">
  223. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added <code class="docutils literal notranslate"><span class="pre">__cached__</span></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>
  224. </div>
  225. <div class="versionchanged">
  226. <p><span class="versionmodified changed">Changed in version 3.4: </span>Updated to take advantage of the module spec feature added by
  227. <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 class="docutils literal notranslate"><span class="pre">__cached__</span></code> to be set correctly for modules
  228. run this way, as well as ensuring the real module name is always
  229. accessible as <code class="docutils literal notranslate"><span class="pre">__spec__.name</span></code>.</p>
  230. </div>
  231. <div class="versionchanged">
  232. <p><span class="versionmodified changed">Changed in version 3.12: </span>The setting of <code class="docutils literal notranslate"><span class="pre">__cached__</span></code>, <code class="docutils literal notranslate"><span class="pre">__loader__</span></code>, and
  233. <code class="docutils literal notranslate"><span class="pre">__package__</span></code> are deprecated. See
  234. <a class="reference internal" href="importlib.html#importlib.machinery.ModuleSpec" title="importlib.machinery.ModuleSpec"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModuleSpec</span></code></a> for alternatives.</p>
  235. </div>
  236. </dd></dl>
  237. <dl class="py function">
  238. <dt class="sig sig-object py" id="runpy.run_path">
  239. <span class="sig-prename descclassname"><span class="pre">runpy.</span></span><span class="sig-name descname"><span class="pre">run_path</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path_name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">init_globals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">run_name</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="#runpy.run_path" title="Link to this definition">¶</a></dt>
  240. <dd><p id="index-4">Execute the code at the named filesystem location and return the resulting
  241. module globals dictionary. As with a script name supplied to the CPython
  242. command line, the supplied path may refer to a Python source file, a
  243. compiled bytecode file or a valid <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> entry containing a
  244. <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> module
  245. (e.g. a zipfile containing a top-level <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> file).</p>
  246. <p>For a simple script, the specified code is simply executed in a fresh
  247. module namespace. For a valid <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> entry (typically a zipfile or
  248. directory), the entry is first added to the beginning of <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>. The
  249. function then looks for and executes a <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> module using the
  250. updated path. Note that there is no special protection against invoking
  251. an existing <code class="docutils literal notranslate"><span class="pre">__main__</span></code> entry located elsewhere on <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> if
  252. there is no such module at the specified location.</p>
  253. <p>The optional dictionary argument <em>init_globals</em> may be used to pre-populate
  254. the module’s globals dictionary before the code is executed. The supplied
  255. dictionary will not be modified. If any of the special global variables
  256. below are defined in the supplied dictionary, those definitions are
  257. overridden by <a class="reference internal" href="#runpy.run_path" title="runpy.run_path"><code class="xref py py-func docutils literal notranslate"><span class="pre">run_path()</span></code></a>.</p>
  258. <p>The special global variables <code class="docutils literal notranslate"><span class="pre">__name__</span></code>, <code class="docutils literal notranslate"><span class="pre">__spec__</span></code>, <code class="docutils literal notranslate"><span class="pre">__file__</span></code>,
  259. <code class="docutils literal notranslate"><span class="pre">__cached__</span></code>, <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> and <code class="docutils literal notranslate"><span class="pre">__package__</span></code> are set in the globals
  260. dictionary before the module code is executed (Note that this is a
  261. minimal set of variables - other variables may be set implicitly as an
  262. interpreter implementation detail).</p>
  263. <p><code class="docutils literal notranslate"><span class="pre">__name__</span></code> is set to <em>run_name</em> if this optional argument is not
  264. <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> and to <code class="docutils literal notranslate"><span class="pre">'&lt;run_path&gt;'</span></code> otherwise.</p>
  265. <p>If the supplied path directly references a script file (whether as source
  266. or as precompiled byte code), then <code class="docutils literal notranslate"><span class="pre">__file__</span></code> will be set to the
  267. supplied path, and <code class="docutils literal notranslate"><span class="pre">__spec__</span></code>, <code class="docutils literal notranslate"><span class="pre">__cached__</span></code>, <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> and
  268. <code class="docutils literal notranslate"><span class="pre">__package__</span></code> will all be set to <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>.</p>
  269. <p>If the supplied path is a reference to a valid <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> entry, then
  270. <code class="docutils literal notranslate"><span class="pre">__spec__</span></code> will be set appropriately for the imported <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a>
  271. module (that is, <code class="docutils literal notranslate"><span class="pre">__spec__.name</span></code> will always be <code class="docutils literal notranslate"><span class="pre">__main__</span></code>).
  272. <code class="docutils literal notranslate"><span class="pre">__file__</span></code>, <code class="docutils literal notranslate"><span class="pre">__cached__</span></code>, <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> and <code class="docutils literal notranslate"><span class="pre">__package__</span></code> will be
  273. <a class="reference internal" href="../reference/import.html#import-mod-attrs"><span class="std std-ref">set as normal</span></a> based on the module spec.</p>
  274. <p>A number of alterations are also made to the <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> module. Firstly,
  275. <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> may be altered as described above. <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> is updated
  276. with the value of <code class="docutils literal notranslate"><span class="pre">path_name</span></code> and <code class="docutils literal notranslate"><span class="pre">sys.modules[__name__]</span></code> is updated
  277. with a temporary module object for the module being executed. All
  278. modifications to items in <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> are reverted before the function
  279. returns.</p>
  280. <p>Note that, unlike <a class="reference internal" href="#runpy.run_module" title="runpy.run_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">run_module()</span></code></a>, the alterations made to <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a>
  281. are not optional in this function as these adjustments are essential to
  282. allowing the execution 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> entries. As the thread-safety
  283. limitations still apply, use of this function in threaded code should be
  284. either serialised with the import lock or delegated to a separate process.</p>
  285. <div class="admonition seealso">
  286. <p class="admonition-title">See also</p>
  287. <p><a class="reference internal" href="../using/cmdline.html#using-on-interface-options"><span class="std std-ref">Interface options</span></a> for equivalent functionality on the
  288. command line (<code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">path/to/script</span></code>).</p>
  289. </div>
  290. <div class="versionadded">
  291. <p><span class="versionmodified added">New in version 3.2.</span></p>
  292. </div>
  293. <div class="versionchanged">
  294. <p><span class="versionmodified changed">Changed in version 3.4: </span>Updated to take advantage of the module spec feature added by
  295. <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 class="docutils literal notranslate"><span class="pre">__cached__</span></code> to be set correctly in the
  296. case where <code class="docutils literal notranslate"><span class="pre">__main__</span></code> is imported from a valid <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> entry rather
  297. than being executed directly.</p>
  298. </div>
  299. <div class="versionchanged">
  300. <p><span class="versionmodified changed">Changed in version 3.12: </span>The setting of <code class="docutils literal notranslate"><span class="pre">__cached__</span></code>, <code class="docutils literal notranslate"><span class="pre">__loader__</span></code>, and
  301. <code class="docutils literal notranslate"><span class="pre">__package__</span></code> are deprecated.</p>
  302. </div>
  303. </dd></dl>
  304. <div class="admonition seealso">
  305. <p class="admonition-title">See also</p>
  306. <dl class="simple">
  307. <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>
  308. </dd>
  309. <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>
  310. </dd>
  311. <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>
  312. </dd>
  313. </dl>
  314. <p><a class="reference internal" href="../using/cmdline.html#using-on-general"><span class="std std-ref">Command line and environment</span></a> - CPython command line details</p>
  315. <p>The <a class="reference internal" href="importlib.html#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a> function</p>
  316. </div>
  317. </section>
  318. <div class="clearer"></div>
  319. </div>
  320. </div>
  321. </div>
  322. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  323. <div class="sphinxsidebarwrapper">
  324. <div>
  325. <h4>Previous topic</h4>
  326. <p class="topless"><a href="modulefinder.html"
  327. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">modulefinder</span></code> — Find modules used by a script</a></p>
  328. </div>
  329. <div>
  330. <h4>Next topic</h4>
  331. <p class="topless"><a href="importlib.html"
  332. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code> — The implementation of <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a></p>
  333. </div>
  334. <div role="note" aria-label="source link">
  335. <h3>This Page</h3>
  336. <ul class="this-page-menu">
  337. <li><a href="../bugs.html">Report a Bug</a></li>
  338. <li>
  339. <a href="https://github.com/python/cpython/blob/main/Doc/library/runpy.rst"
  340. rel="nofollow">Show Source
  341. </a>
  342. </li>
  343. </ul>
  344. </div>
  345. </div>
  346. <div id="sidebarbutton" title="Collapse sidebar">
  347. <span>«</span>
  348. </div>
  349. </div>
  350. <div class="clearer"></div>
  351. </div>
  352. <div class="related" role="navigation" aria-label="related navigation">
  353. <h3>Navigation</h3>
  354. <ul>
  355. <li class="right" style="margin-right: 10px">
  356. <a href="../genindex.html" title="General Index"
  357. >index</a></li>
  358. <li class="right" >
  359. <a href="../py-modindex.html" title="Python Module Index"
  360. >modules</a> |</li>
  361. <li class="right" >
  362. <a href="importlib.html" title="importlib — The implementation of import"
  363. >next</a> |</li>
  364. <li class="right" >
  365. <a href="modulefinder.html" title="modulefinder — Find modules used by a script"
  366. >previous</a> |</li>
  367. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  368. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  369. <li class="switchers">
  370. <div class="language_switcher_placeholder"></div>
  371. <div class="version_switcher_placeholder"></div>
  372. </li>
  373. <li>
  374. </li>
  375. <li id="cpython-language-and-version">
  376. <a href="../index.html">3.12.3 Documentation</a> &#187;
  377. </li>
  378. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  379. <li class="nav-item nav-item-2"><a href="modules.html" >Importing Modules</a> &#187;</li>
  380. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">runpy</span></code> — Locating and executing Python modules</a></li>
  381. <li class="right">
  382. <div class="inline-search" role="search">
  383. <form class="inline-search" action="../search.html" method="get">
  384. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  385. <input type="submit" value="Go" />
  386. </form>
  387. </div>
  388. |
  389. </li>
  390. <li class="right">
  391. <label class="theme-selector-label">
  392. Theme
  393. <select class="theme-selector" oninput="activateTheme(this.value)">
  394. <option value="auto" selected>Auto</option>
  395. <option value="light">Light</option>
  396. <option value="dark">Dark</option>
  397. </select>
  398. </label> |</li>
  399. </ul>
  400. </div>
  401. <div class="footer">
  402. &copy;
  403. <a href="../copyright.html">
  404. Copyright
  405. </a>
  406. 2001-2024, Python Software Foundation.
  407. <br />
  408. This page is licensed under the Python Software Foundation License Version 2.
  409. <br />
  410. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  411. <br />
  412. See <a href="/license.html">History and License</a> for more information.<br />
  413. <br />
  414. The Python Software Foundation is a non-profit corporation.
  415. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  416. <br />
  417. <br />
  418. Last updated on Apr 09, 2024 (13:47 UTC).
  419. <a href="/bugs.html">Found a bug</a>?
  420. <br />
  421. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  422. </div>
  423. </body>
  424. </html>
上海开阖软件有限公司 沪ICP备12045867号-1