import.html 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  6. <meta property="og:title" content="Importing Modules" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/c-api/import.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  11. <meta property="og:image:alt" content="Python documentation" />
  12. <meta property="og:image:width" content="200" />
  13. <meta property="og:image:height" content="200" />
  14. <meta name="theme-color" content="#3776ab" />
  15. <title>Importing Modules &#8212; Python 3.12.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  16. <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
  17. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
  18. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
  19. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  20. <script src="../_static/jquery.js"></script>
  21. <script src="../_static/underscore.js"></script>
  22. <script src="../_static/doctools.js"></script>
  23. <script src="../_static/sidebar.js"></script>
  24. <link rel="search" type="application/opensearchdescription+xml"
  25. title="Search within Python 3.12.0 documentation"
  26. href="../_static/opensearch.xml"/>
  27. <link rel="author" title="About these documents" href="../about.html" />
  28. <link rel="index" title="Index" href="../genindex.html" />
  29. <link rel="search" title="Search" href="../search.html" />
  30. <link rel="copyright" title="Copyright" href="../copyright.html" />
  31. <link rel="next" title="Data marshalling support" href="marshal.html" />
  32. <link rel="prev" title="Operating System Utilities" href="sys.html" />
  33. <link rel="canonical" href="https://docs.python.org/3/c-api/import.html" />
  34. <style>
  35. @media only screen {
  36. table.full-width-table {
  37. width: 100%;
  38. }
  39. }
  40. </style>
  41. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  42. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  43. <script type="text/javascript" src="../_static/copybutton.js"></script>
  44. <script type="text/javascript" src="../_static/menu.js"></script>
  45. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  46. </head>
  47. <body>
  48. <div class="mobile-nav">
  49. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  50. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  51. <nav class="nav-content" role="navigation">
  52. <label for="menuToggler" class="toggler__label">
  53. <span></span>
  54. </label>
  55. <span class="nav-items-wrapper">
  56. <a href="https://www.python.org/" class="nav-logo">
  57. <img src="../_static/py.svg" alt="Logo"/>
  58. </a>
  59. <span class="version_switcher_placeholder"></span>
  60. <form role="search" class="search" action="../search.html" method="get">
  61. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  62. <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>
  63. </svg>
  64. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  65. <input type="submit" value="Go"/>
  66. </form>
  67. </span>
  68. </nav>
  69. <div class="menu-wrapper">
  70. <nav class="menu" role="navigation" aria-label="main navigation">
  71. <div class="language_switcher_placeholder"></div>
  72. <label class="theme-selector-label">
  73. Theme
  74. <select class="theme-selector" oninput="activateTheme(this.value)">
  75. <option value="auto" selected>Auto</option>
  76. <option value="light">Light</option>
  77. <option value="dark">Dark</option>
  78. </select>
  79. </label>
  80. <div>
  81. <h4>Previous topic</h4>
  82. <p class="topless"><a href="sys.html"
  83. title="previous chapter">Operating System Utilities</a></p>
  84. </div>
  85. <div>
  86. <h4>Next topic</h4>
  87. <p class="topless"><a href="marshal.html"
  88. title="next chapter">Data marshalling support</a></p>
  89. </div>
  90. <div role="note" aria-label="source link">
  91. <h3>This Page</h3>
  92. <ul class="this-page-menu">
  93. <li><a href="../bugs.html">Report a Bug</a></li>
  94. <li>
  95. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/import.rst"
  96. rel="nofollow">Show Source
  97. </a>
  98. </li>
  99. </ul>
  100. </div>
  101. </nav>
  102. </div>
  103. </div>
  104. <div class="related" role="navigation" aria-label="related navigation">
  105. <h3>Navigation</h3>
  106. <ul>
  107. <li class="right" style="margin-right: 10px">
  108. <a href="../genindex.html" title="General Index"
  109. accesskey="I">index</a></li>
  110. <li class="right" >
  111. <a href="../py-modindex.html" title="Python Module Index"
  112. >modules</a> |</li>
  113. <li class="right" >
  114. <a href="marshal.html" title="Data marshalling support"
  115. accesskey="N">next</a> |</li>
  116. <li class="right" >
  117. <a href="sys.html" title="Operating System Utilities"
  118. accesskey="P">previous</a> |</li>
  119. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  120. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  121. <li class="switchers">
  122. <div class="language_switcher_placeholder"></div>
  123. <div class="version_switcher_placeholder"></div>
  124. </li>
  125. <li>
  126. </li>
  127. <li id="cpython-language-and-version">
  128. <a href="../index.html">3.12.0 Documentation</a> &#187;
  129. </li>
  130. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  131. <li class="nav-item nav-item-2"><a href="utilities.html" accesskey="U">Utilities</a> &#187;</li>
  132. <li class="nav-item nav-item-this"><a href="">Importing Modules</a></li>
  133. <li class="right">
  134. <div class="inline-search" role="search">
  135. <form class="inline-search" action="../search.html" method="get">
  136. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  137. <input type="submit" value="Go" />
  138. </form>
  139. </div>
  140. |
  141. </li>
  142. <li class="right">
  143. <label class="theme-selector-label">
  144. Theme
  145. <select class="theme-selector" oninput="activateTheme(this.value)">
  146. <option value="auto" selected>Auto</option>
  147. <option value="light">Light</option>
  148. <option value="dark">Dark</option>
  149. </select>
  150. </label> |</li>
  151. </ul>
  152. </div>
  153. <div class="document">
  154. <div class="documentwrapper">
  155. <div class="bodywrapper">
  156. <div class="body" role="main">
  157. <section id="importing-modules">
  158. <span id="importing"></span><h1>Importing Modules<a class="headerlink" href="#importing-modules" title="Permalink to this headline">¶</a></h1>
  159. <dl class="c function">
  160. <dt class="sig sig-object c" id="c.PyImport_ImportModule">
  161. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_ImportModule</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ImportModule" title="Permalink to this definition">¶</a><br /></dt>
  162. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p id="index-0">This is a simplified interface to <a class="reference internal" href="#c.PyImport_ImportModuleEx" title="PyImport_ImportModuleEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModuleEx()</span></code></a> below,
  163. leaving the <em>globals</em> and <em>locals</em> arguments set to <code class="docutils literal notranslate"><span class="pre">NULL</span></code> and <em>level</em> set
  164. to 0. When the <em>name</em>
  165. argument contains a dot (when it specifies a submodule of a package), the
  166. <em>fromlist</em> argument is set to the list <code class="docutils literal notranslate"><span class="pre">['*']</span></code> so that the return value is the
  167. named module rather than the top-level package containing it as would otherwise
  168. be the case. (Unfortunately, this has an additional side effect when <em>name</em> in
  169. fact specifies a subpackage instead of a submodule: the submodules specified in
  170. the package’s <code class="docutils literal notranslate"><span class="pre">__all__</span></code> variable are loaded.) Return a new reference to the
  171. imported module, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> with an exception set on failure. A failing
  172. import of a module doesn’t leave the module in <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>.</p>
  173. <p>This function always uses absolute imports.</p>
  174. </dd></dl>
  175. <dl class="c function">
  176. <dt class="sig sig-object c" id="c.PyImport_ImportModuleNoBlock">
  177. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_ImportModuleNoBlock</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ImportModuleNoBlock" title="Permalink to this definition">¶</a><br /></dt>
  178. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>This function is a deprecated alias of <a class="reference internal" href="#c.PyImport_ImportModule" title="PyImport_ImportModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModule()</span></code></a>.</p>
  179. <div class="versionchanged">
  180. <p><span class="versionmodified changed">Changed in version 3.3: </span>This function used to fail immediately when the import lock was held
  181. by another thread. In Python 3.3 though, the locking scheme switched
  182. to per-module locks for most purposes, so this function’s special
  183. behaviour isn’t needed anymore.</p>
  184. </div>
  185. </dd></dl>
  186. <dl class="c function">
  187. <dt class="sig sig-object c" id="c.PyImport_ImportModuleEx">
  188. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_ImportModuleEx</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">globals</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">locals</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">fromlist</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ImportModuleEx" title="Permalink to this definition">¶</a><br /></dt>
  189. <dd><em class="refcount">Return value: New reference.</em><p id="index-1">Import a module. This is best described by referring to the built-in Python
  190. function <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a>.</p>
  191. <p>The return value is a new reference to the imported module or top-level
  192. package, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> with an exception set on failure. Like for
  193. <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a>, the return value when a submodule of a package was
  194. requested is normally the top-level package, unless a non-empty <em>fromlist</em>
  195. was given.</p>
  196. <p>Failing imports remove incomplete module objects, like with
  197. <a class="reference internal" href="#c.PyImport_ImportModule" title="PyImport_ImportModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModule()</span></code></a>.</p>
  198. </dd></dl>
  199. <dl class="c function">
  200. <dt class="sig sig-object c" id="c.PyImport_ImportModuleLevelObject">
  201. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_ImportModuleLevelObject</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">globals</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">locals</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">fromlist</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">level</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ImportModuleLevelObject" title="Permalink to this definition">¶</a><br /></dt>
  202. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p>Import a module. This is best described by referring to the built-in Python
  203. function <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a>, as the standard <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> function calls
  204. this function directly.</p>
  205. <p>The return value is a new reference to the imported module or top-level package,
  206. or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> with an exception set on failure. Like for <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a>,
  207. the return value when a submodule of a package was requested is normally the
  208. top-level package, unless a non-empty <em>fromlist</em> was given.</p>
  209. <div class="versionadded">
  210. <p><span class="versionmodified added">New in version 3.3.</span></p>
  211. </div>
  212. </dd></dl>
  213. <dl class="c function">
  214. <dt class="sig sig-object c" id="c.PyImport_ImportModuleLevel">
  215. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_ImportModuleLevel</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">globals</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">locals</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">fromlist</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">level</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ImportModuleLevel" title="Permalink to this definition">¶</a><br /></dt>
  216. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Similar to <a class="reference internal" href="#c.PyImport_ImportModuleLevelObject" title="PyImport_ImportModuleLevelObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModuleLevelObject()</span></code></a>, but the name is a
  217. UTF-8 encoded string instead of a Unicode object.</p>
  218. <div class="versionchanged">
  219. <p><span class="versionmodified changed">Changed in version 3.3: </span>Negative values for <em>level</em> are no longer accepted.</p>
  220. </div>
  221. </dd></dl>
  222. <dl class="c function">
  223. <dt class="sig sig-object c" id="c.PyImport_Import">
  224. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_Import</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_Import" title="Permalink to this definition">¶</a><br /></dt>
  225. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>This is a higher-level interface that calls the current “import hook
  226. function” (with an explicit <em>level</em> of 0, meaning absolute import). It
  227. invokes the <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> function from the <code class="docutils literal notranslate"><span class="pre">__builtins__</span></code> of the
  228. current globals. This means that the import is done using whatever import
  229. hooks are installed in the current environment.</p>
  230. <p>This function always uses absolute imports.</p>
  231. </dd></dl>
  232. <dl class="c function">
  233. <dt class="sig sig-object c" id="c.PyImport_ReloadModule">
  234. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_ReloadModule</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">m</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ReloadModule" title="Permalink to this definition">¶</a><br /></dt>
  235. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Reload a module. Return a new reference to the reloaded module, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> with
  236. an exception set on failure (the module still exists in this case).</p>
  237. </dd></dl>
  238. <dl class="c function">
  239. <dt class="sig sig-object c" id="c.PyImport_AddModuleObject">
  240. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_AddModuleObject</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_AddModuleObject" title="Permalink to this definition">¶</a><br /></dt>
  241. <dd><em class="refcount">Return value: Borrowed reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p>Return the module object corresponding to a module name. The <em>name</em> argument
  242. may be of the form <code class="docutils literal notranslate"><span class="pre">package.module</span></code>. First check the modules dictionary if
  243. there’s one there, and if not, create a new one and insert it in the modules
  244. dictionary. Return <code class="docutils literal notranslate"><span class="pre">NULL</span></code> with an exception set on failure.</p>
  245. <div class="admonition note">
  246. <p class="admonition-title">Note</p>
  247. <p>This function does not load or import the module; if the module wasn’t already
  248. loaded, you will get an empty module object. Use <a class="reference internal" href="#c.PyImport_ImportModule" title="PyImport_ImportModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModule()</span></code></a>
  249. or one of its variants to import a module. Package structures implied by a
  250. dotted name for <em>name</em> are not created if not already present.</p>
  251. </div>
  252. <div class="versionadded">
  253. <p><span class="versionmodified added">New in version 3.3.</span></p>
  254. </div>
  255. </dd></dl>
  256. <dl class="c function">
  257. <dt class="sig sig-object c" id="c.PyImport_AddModule">
  258. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_AddModule</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_AddModule" title="Permalink to this definition">¶</a><br /></dt>
  259. <dd><em class="refcount">Return value: Borrowed reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Similar to <a class="reference internal" href="#c.PyImport_AddModuleObject" title="PyImport_AddModuleObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_AddModuleObject()</span></code></a>, but the name is a UTF-8
  260. encoded string instead of a Unicode object.</p>
  261. </dd></dl>
  262. <dl class="c function">
  263. <dt class="sig sig-object c" id="c.PyImport_ExecCodeModule">
  264. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_ExecCodeModule</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">co</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ExecCodeModule" title="Permalink to this definition">¶</a><br /></dt>
  265. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p id="index-2">Given a module name (possibly of the form <code class="docutils literal notranslate"><span class="pre">package.module</span></code>) and a code object
  266. read from a Python bytecode file or obtained from the built-in function
  267. <a class="reference internal" href="../library/functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a>, load the module. Return a new reference to the module object,
  268. or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> with an exception set if an error occurred. <em>name</em>
  269. is removed from <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> in error cases, even if <em>name</em> was already
  270. in <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> on entry to <a class="reference internal" href="#c.PyImport_ExecCodeModule" title="PyImport_ExecCodeModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExecCodeModule()</span></code></a>. Leaving
  271. incompletely initialized modules in <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> is dangerous, as imports of
  272. such modules have no way to know that the module object is an unknown (and
  273. probably damaged with respect to the module author’s intents) state.</p>
  274. <p>The module’s <a class="reference internal" href="../reference/import.html#spec__" title="__spec__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__spec__</span></code></a> and <a class="reference internal" href="../reference/import.html#loader__" title="__loader__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__loader__</span></code></a> will be set, if
  275. not set already, with the appropriate values. The spec’s loader will
  276. be set to the module’s <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> (if set) and to an instance of
  277. <a class="reference internal" href="../library/importlib.html#importlib.machinery.SourceFileLoader" title="importlib.machinery.SourceFileLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">SourceFileLoader</span></code></a> otherwise.</p>
  278. <p>The module’s <a class="reference internal" href="../reference/import.html#file__" title="__file__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__file__</span></code></a> attribute will be set to the code object’s
  279. <code class="xref py py-attr docutils literal notranslate"><span class="pre">co_filename</span></code>. If applicable, <a class="reference internal" href="../reference/import.html#cached__" title="__cached__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cached__</span></code></a> will also
  280. be set.</p>
  281. <p>This function will reload the module if it was already imported. See
  282. <a class="reference internal" href="#c.PyImport_ReloadModule" title="PyImport_ReloadModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ReloadModule()</span></code></a> for the intended way to reload a module.</p>
  283. <p>If <em>name</em> points to a dotted name of the form <code class="docutils literal notranslate"><span class="pre">package.module</span></code>, any package
  284. structures not already created will still not be created.</p>
  285. <p>See also <a class="reference internal" href="#c.PyImport_ExecCodeModuleEx" title="PyImport_ExecCodeModuleEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExecCodeModuleEx()</span></code></a> and
  286. <a class="reference internal" href="#c.PyImport_ExecCodeModuleWithPathnames" title="PyImport_ExecCodeModuleWithPathnames"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExecCodeModuleWithPathnames()</span></code></a>.</p>
  287. <div class="versionchanged">
  288. <p><span class="versionmodified changed">Changed in version 3.12: </span>The setting of <a class="reference internal" href="../reference/import.html#cached__" title="__cached__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cached__</span></code></a> and <a class="reference internal" href="../reference/import.html#loader__" title="__loader__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__loader__</span></code></a> is
  289. deprecated. See <a class="reference internal" href="../library/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
  290. alternatives.</p>
  291. </div>
  292. </dd></dl>
  293. <dl class="c function">
  294. <dt class="sig sig-object c" id="c.PyImport_ExecCodeModuleEx">
  295. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_ExecCodeModuleEx</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">co</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">pathname</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ExecCodeModuleEx" title="Permalink to this definition">¶</a><br /></dt>
  296. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Like <a class="reference internal" href="#c.PyImport_ExecCodeModule" title="PyImport_ExecCodeModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExecCodeModule()</span></code></a>, but the <a class="reference internal" href="../reference/import.html#file__" title="__file__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__file__</span></code></a> attribute of
  297. the module object is set to <em>pathname</em> if it is non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  298. <p>See also <a class="reference internal" href="#c.PyImport_ExecCodeModuleWithPathnames" title="PyImport_ExecCodeModuleWithPathnames"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExecCodeModuleWithPathnames()</span></code></a>.</p>
  299. </dd></dl>
  300. <dl class="c function">
  301. <dt class="sig sig-object c" id="c.PyImport_ExecCodeModuleObject">
  302. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_ExecCodeModuleObject</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">co</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">pathname</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">cpathname</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ExecCodeModuleObject" title="Permalink to this definition">¶</a><br /></dt>
  303. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p>Like <a class="reference internal" href="#c.PyImport_ExecCodeModuleEx" title="PyImport_ExecCodeModuleEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExecCodeModuleEx()</span></code></a>, but the <a class="reference internal" href="../reference/import.html#cached__" title="__cached__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cached__</span></code></a>
  304. attribute of the module object is set to <em>cpathname</em> if it is
  305. non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code>. Of the three functions, this is the preferred one to use.</p>
  306. <div class="versionadded">
  307. <p><span class="versionmodified added">New in version 3.3.</span></p>
  308. </div>
  309. <div class="versionchanged">
  310. <p><span class="versionmodified changed">Changed in version 3.12: </span>Setting <a class="reference internal" href="../reference/import.html#cached__" title="__cached__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cached__</span></code></a> is deprecated. See
  311. <a class="reference internal" href="../library/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>
  312. </div>
  313. </dd></dl>
  314. <dl class="c function">
  315. <dt class="sig sig-object c" id="c.PyImport_ExecCodeModuleWithPathnames">
  316. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_ExecCodeModuleWithPathnames</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">co</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">pathname</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">cpathname</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ExecCodeModuleWithPathnames" title="Permalink to this definition">¶</a><br /></dt>
  317. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Like <a class="reference internal" href="#c.PyImport_ExecCodeModuleObject" title="PyImport_ExecCodeModuleObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExecCodeModuleObject()</span></code></a>, but <em>name</em>, <em>pathname</em> and
  318. <em>cpathname</em> are UTF-8 encoded strings. Attempts are also made to figure out
  319. what the value for <em>pathname</em> should be from <em>cpathname</em> if the former is
  320. set to <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  321. <div class="versionadded">
  322. <p><span class="versionmodified added">New in version 3.2.</span></p>
  323. </div>
  324. <div class="versionchanged">
  325. <p><span class="versionmodified changed">Changed in version 3.3: </span>Uses <code class="xref py py-func docutils literal notranslate"><span class="pre">imp.source_from_cache()</span></code> in calculating the source path if
  326. only the bytecode path is provided.</p>
  327. </div>
  328. <div class="versionchanged">
  329. <p><span class="versionmodified changed">Changed in version 3.12: </span>No longer uses the removed <code class="xref py py-mod docutils literal notranslate"><span class="pre">imp</span></code> module.</p>
  330. </div>
  331. </dd></dl>
  332. <dl class="c function">
  333. <dt class="sig sig-object c" id="c.PyImport_GetMagicNumber">
  334. <span class="kt"><span class="pre">long</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_GetMagicNumber</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_GetMagicNumber" title="Permalink to this definition">¶</a><br /></dt>
  335. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Return the magic number for Python bytecode files (a.k.a. <code class="file docutils literal notranslate"><span class="pre">.pyc</span></code> file).
  336. The magic number should be present in the first four bytes of the bytecode
  337. file, in little-endian byte order. Returns <code class="docutils literal notranslate"><span class="pre">-1</span></code> on error.</p>
  338. <div class="versionchanged">
  339. <p><span class="versionmodified changed">Changed in version 3.3: </span>Return value of <code class="docutils literal notranslate"><span class="pre">-1</span></code> upon failure.</p>
  340. </div>
  341. </dd></dl>
  342. <dl class="c function">
  343. <dt class="sig sig-object c" id="c.PyImport_GetMagicTag">
  344. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_GetMagicTag</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_GetMagicTag" title="Permalink to this definition">¶</a><br /></dt>
  345. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Return the magic tag string for <span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-3147/"><strong>PEP 3147</strong></a> format Python bytecode file
  346. names. Keep in mind that the value at <code class="docutils literal notranslate"><span class="pre">sys.implementation.cache_tag</span></code> is
  347. authoritative and should be used instead of this function.</p>
  348. <div class="versionadded">
  349. <p><span class="versionmodified added">New in version 3.2.</span></p>
  350. </div>
  351. </dd></dl>
  352. <dl class="c function">
  353. <dt class="sig sig-object c" id="c.PyImport_GetModuleDict">
  354. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_GetModuleDict</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_GetModuleDict" title="Permalink to this definition">¶</a><br /></dt>
  355. <dd><em class="refcount">Return value: Borrowed reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Return the dictionary used for the module administration (a.k.a.
  356. <code class="docutils literal notranslate"><span class="pre">sys.modules</span></code>). Note that this is a per-interpreter variable.</p>
  357. </dd></dl>
  358. <dl class="c function">
  359. <dt class="sig sig-object c" id="c.PyImport_GetModule">
  360. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_GetModule</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_GetModule" title="Permalink to this definition">¶</a><br /></dt>
  361. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.8.</em><p>Return the already imported module with the given name. If the
  362. module has not been imported yet then returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> but does not set
  363. an error. Returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> and sets an error if the lookup failed.</p>
  364. <div class="versionadded">
  365. <p><span class="versionmodified added">New in version 3.7.</span></p>
  366. </div>
  367. </dd></dl>
  368. <dl class="c function">
  369. <dt class="sig sig-object c" id="c.PyImport_GetImporter">
  370. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_GetImporter</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">path</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_GetImporter" title="Permalink to this definition">¶</a><br /></dt>
  371. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Return a finder object for a <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>/<code class="xref py py-attr docutils literal notranslate"><span class="pre">pkg.__path__</span></code> item
  372. <em>path</em>, possibly by fetching it from the <a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a>
  373. dict. If it wasn’t yet cached, traverse <a class="reference internal" href="../library/sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a> until a hook
  374. is found that can handle the path item. Return <code class="docutils literal notranslate"><span class="pre">None</span></code> if no hook could;
  375. this tells our caller that the <a class="reference internal" href="../glossary.html#term-path-based-finder"><span class="xref std std-term">path based finder</span></a> could not find a
  376. finder for this path item. Cache the result in <a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a>.
  377. Return a new reference to the finder object.</p>
  378. </dd></dl>
  379. <dl class="c function">
  380. <dt class="sig sig-object c" id="c.PyImport_ImportFrozenModuleObject">
  381. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_ImportFrozenModuleObject</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ImportFrozenModuleObject" title="Permalink to this definition">¶</a><br /></dt>
  382. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p>Load a frozen module named <em>name</em>. Return <code class="docutils literal notranslate"><span class="pre">1</span></code> for success, <code class="docutils literal notranslate"><span class="pre">0</span></code> if the
  383. module is not found, and <code class="docutils literal notranslate"><span class="pre">-1</span></code> with an exception set if the initialization
  384. failed. To access the imported module on a successful load, use
  385. <a class="reference internal" href="#c.PyImport_ImportModule" title="PyImport_ImportModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModule()</span></code></a>. (Note the misnomer — this function would
  386. reload the module if it was already imported.)</p>
  387. <div class="versionadded">
  388. <p><span class="versionmodified added">New in version 3.3.</span></p>
  389. </div>
  390. <div class="versionchanged">
  391. <p><span class="versionmodified changed">Changed in version 3.4: </span>The <code class="docutils literal notranslate"><span class="pre">__file__</span></code> attribute is no longer set on the module.</p>
  392. </div>
  393. </dd></dl>
  394. <dl class="c function">
  395. <dt class="sig sig-object c" id="c.PyImport_ImportFrozenModule">
  396. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_ImportFrozenModule</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ImportFrozenModule" title="Permalink to this definition">¶</a><br /></dt>
  397. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Similar to <a class="reference internal" href="#c.PyImport_ImportFrozenModuleObject" title="PyImport_ImportFrozenModuleObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportFrozenModuleObject()</span></code></a>, but the name is a
  398. UTF-8 encoded string instead of a Unicode object.</p>
  399. </dd></dl>
  400. <dl class="c struct">
  401. <dt class="sig sig-object c" id="c._frozen">
  402. <span class="k"><span class="pre">struct</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">_frozen</span></span></span><a class="headerlink" href="#c._frozen" title="Permalink to this definition">¶</a><br /></dt>
  403. <dd><p id="index-4">This is the structure type definition for frozen module descriptors, as
  404. generated by the <strong class="program">freeze</strong> utility (see <code class="file docutils literal notranslate"><span class="pre">Tools/freeze/</span></code> in the
  405. Python source distribution). Its definition, found in <code class="file docutils literal notranslate"><span class="pre">Include/import.h</span></code>,
  406. is:</p>
  407. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span><span class="w"> </span><span class="nc">_frozen</span><span class="w"> </span><span class="p">{</span>
  408. <span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">name</span><span class="p">;</span>
  409. <span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">code</span><span class="p">;</span>
  410. <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">size</span><span class="p">;</span>
  411. <span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="n">is_package</span><span class="p">;</span>
  412. <span class="p">};</span>
  413. </pre></div>
  414. </div>
  415. <div class="versionchanged">
  416. <p><span class="versionmodified changed">Changed in version 3.11: </span>The new <code class="docutils literal notranslate"><span class="pre">is_package</span></code> field indicates whether the module is a package or not.
  417. This replaces setting the <code class="docutils literal notranslate"><span class="pre">size</span></code> field to a negative value.</p>
  418. </div>
  419. </dd></dl>
  420. <dl class="c var">
  421. <dt class="sig sig-object c" id="c.PyImport_FrozenModules">
  422. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="k"><span class="pre">struct</span></span><span class="w"> </span><a class="reference internal" href="#c._frozen" title="_frozen"><span class="n"><span class="pre">_frozen</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_FrozenModules</span></span></span><a class="headerlink" href="#c.PyImport_FrozenModules" title="Permalink to this definition">¶</a><br /></dt>
  423. <dd><p>This pointer is initialized to point to an array of <a class="reference internal" href="#c._frozen" title="_frozen"><code class="xref c c-struct docutils literal notranslate"><span class="pre">_frozen</span></code></a>
  424. records, terminated by one whose members are all <code class="docutils literal notranslate"><span class="pre">NULL</span></code> or zero. When a frozen
  425. module is imported, it is searched in this table. Third-party code could play
  426. tricks with this to provide a dynamically created collection of frozen modules.</p>
  427. </dd></dl>
  428. <dl class="c function">
  429. <dt class="sig sig-object c" id="c.PyImport_AppendInittab">
  430. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_AppendInittab</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">(</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">initfunc</span></span><span class="p"><span class="pre">)</span></span><span class="p"><span class="pre">(</span></span><span class="kt"><span class="pre">void</span></span><span class="p"><span class="pre">)</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_AppendInittab" title="Permalink to this definition">¶</a><br /></dt>
  431. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Add a single module to the existing table of built-in modules. This is a
  432. convenience wrapper around <a class="reference internal" href="#c.PyImport_ExtendInittab" title="PyImport_ExtendInittab"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExtendInittab()</span></code></a>, returning <code class="docutils literal notranslate"><span class="pre">-1</span></code> if
  433. the table could not be extended. The new module can be imported by the name
  434. <em>name</em>, and uses the function <em>initfunc</em> as the initialization function called
  435. on the first attempted import. This should be called before
  436. <a class="reference internal" href="init.html#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>.</p>
  437. </dd></dl>
  438. <dl class="c struct">
  439. <dt class="sig sig-object c" id="c._inittab">
  440. <span class="k"><span class="pre">struct</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">_inittab</span></span></span><a class="headerlink" href="#c._inittab" title="Permalink to this definition">¶</a><br /></dt>
  441. <dd><p>Structure describing a single entry in the list of built-in modules.
  442. Programs which
  443. embed Python may use an array of these structures in conjunction with
  444. <a class="reference internal" href="#c.PyImport_ExtendInittab" title="PyImport_ExtendInittab"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExtendInittab()</span></code></a> to provide additional built-in modules.
  445. The structure consists of two members:</p>
  446. <dl class="c member">
  447. <dt class="sig sig-object c" id="c._inittab.name">
  448. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">name</span></span></span><a class="headerlink" href="#c._inittab.name" title="Permalink to this definition">¶</a><br /></dt>
  449. <dd><p>The module name, as an ASCII encoded string.</p>
  450. </dd></dl>
  451. </dd></dl>
  452. <dl class="c function">
  453. <dt class="sig sig-object c" id="c.PyImport_ExtendInittab">
  454. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyImport_ExtendInittab</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">struct</span></span><span class="w"> </span><a class="reference internal" href="#c._inittab" title="_inittab"><span class="n"><span class="pre">_inittab</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">newtab</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyImport_ExtendInittab" title="Permalink to this definition">¶</a><br /></dt>
  455. <dd><p>Add a collection of modules to the table of built-in modules. The <em>newtab</em>
  456. array must end with a sentinel entry which contains <code class="docutils literal notranslate"><span class="pre">NULL</span></code> for the <a class="reference internal" href="#c._inittab.name" title="_inittab.name"><code class="xref c c-member docutils literal notranslate"><span class="pre">name</span></code></a>
  457. field; failure to provide the sentinel value can result in a memory fault.
  458. Returns <code class="docutils literal notranslate"><span class="pre">0</span></code> on success or <code class="docutils literal notranslate"><span class="pre">-1</span></code> if insufficient memory could be allocated to
  459. extend the internal table. In the event of failure, no modules are added to the
  460. internal table. This must be called before <a class="reference internal" href="init.html#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>.</p>
  461. <p>If Python is initialized multiple times, <a class="reference internal" href="#c.PyImport_AppendInittab" title="PyImport_AppendInittab"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_AppendInittab()</span></code></a> or
  462. <a class="reference internal" href="#c.PyImport_ExtendInittab" title="PyImport_ExtendInittab"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExtendInittab()</span></code></a> must be called before each Python
  463. initialization.</p>
  464. </dd></dl>
  465. </section>
  466. <div class="clearer"></div>
  467. </div>
  468. </div>
  469. </div>
  470. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  471. <div class="sphinxsidebarwrapper">
  472. <div>
  473. <h4>Previous topic</h4>
  474. <p class="topless"><a href="sys.html"
  475. title="previous chapter">Operating System Utilities</a></p>
  476. </div>
  477. <div>
  478. <h4>Next topic</h4>
  479. <p class="topless"><a href="marshal.html"
  480. title="next chapter">Data marshalling support</a></p>
  481. </div>
  482. <div role="note" aria-label="source link">
  483. <h3>This Page</h3>
  484. <ul class="this-page-menu">
  485. <li><a href="../bugs.html">Report a Bug</a></li>
  486. <li>
  487. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/import.rst"
  488. rel="nofollow">Show Source
  489. </a>
  490. </li>
  491. </ul>
  492. </div>
  493. </div>
  494. </div>
  495. <div class="clearer"></div>
  496. </div>
  497. <div class="related" role="navigation" aria-label="related navigation">
  498. <h3>Navigation</h3>
  499. <ul>
  500. <li class="right" style="margin-right: 10px">
  501. <a href="../genindex.html" title="General Index"
  502. >index</a></li>
  503. <li class="right" >
  504. <a href="../py-modindex.html" title="Python Module Index"
  505. >modules</a> |</li>
  506. <li class="right" >
  507. <a href="marshal.html" title="Data marshalling support"
  508. >next</a> |</li>
  509. <li class="right" >
  510. <a href="sys.html" title="Operating System Utilities"
  511. >previous</a> |</li>
  512. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  513. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  514. <li class="switchers">
  515. <div class="language_switcher_placeholder"></div>
  516. <div class="version_switcher_placeholder"></div>
  517. </li>
  518. <li>
  519. </li>
  520. <li id="cpython-language-and-version">
  521. <a href="../index.html">3.12.0 Documentation</a> &#187;
  522. </li>
  523. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  524. <li class="nav-item nav-item-2"><a href="utilities.html" >Utilities</a> &#187;</li>
  525. <li class="nav-item nav-item-this"><a href="">Importing Modules</a></li>
  526. <li class="right">
  527. <div class="inline-search" role="search">
  528. <form class="inline-search" action="../search.html" method="get">
  529. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  530. <input type="submit" value="Go" />
  531. </form>
  532. </div>
  533. |
  534. </li>
  535. <li class="right">
  536. <label class="theme-selector-label">
  537. Theme
  538. <select class="theme-selector" oninput="activateTheme(this.value)">
  539. <option value="auto" selected>Auto</option>
  540. <option value="light">Light</option>
  541. <option value="dark">Dark</option>
  542. </select>
  543. </label> |</li>
  544. </ul>
  545. </div>
  546. <div class="footer">
  547. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  548. <br />
  549. This page is licensed under the Python Software Foundation License Version 2.
  550. <br />
  551. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  552. <br />
  553. See <a href="/license.html">History and License</a> for more information.<br />
  554. <br />
  555. The Python Software Foundation is a non-profit corporation.
  556. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  557. <br />
  558. <br />
  559. Last updated on Oct 02, 2023.
  560. <a href="/bugs.html">Found a bug</a>?
  561. <br />
  562. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  563. </div>
  564. </body>
  565. </html>