gooderp18绿色标准版
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

2156 lines
212KB

  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="importlib — The implementation of import" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/importlib.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/importlib/__init__.py Introduction: The purpose of the importlib package is three-fold. One is to provide the implementation of the import statement (and thus, by extension, the__i..." />
  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/importlib/__init__.py Introduction: The purpose of the importlib package is three-fold. One is to provide the implementation of the import statement (and thus, by extension, the__i..." />
  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>importlib — The implementation of import &#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.resources – Package resource reading, opening and access" href="importlib.resources.html" />
  33. <link rel="prev" title="runpy — Locating and executing Python modules" href="runpy.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/importlib.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code> — The implementation of <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a><ul>
  86. <li><a class="reference internal" href="#introduction">Introduction</a></li>
  87. <li><a class="reference internal" href="#functions">Functions</a></li>
  88. <li><a class="reference internal" href="#module-importlib.abc"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.abc</span></code> – Abstract base classes related to import</a></li>
  89. <li><a class="reference internal" href="#module-importlib.machinery"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.machinery</span></code> – Importers and path hooks</a></li>
  90. <li><a class="reference internal" href="#module-importlib.util"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.util</span></code> – Utility code for importers</a></li>
  91. <li><a class="reference internal" href="#examples">Examples</a><ul>
  92. <li><a class="reference internal" href="#importing-programmatically">Importing programmatically</a></li>
  93. <li><a class="reference internal" href="#checking-if-a-module-can-be-imported">Checking if a module can be imported</a></li>
  94. <li><a class="reference internal" href="#importing-a-source-file-directly">Importing a source file directly</a></li>
  95. <li><a class="reference internal" href="#implementing-lazy-imports">Implementing lazy imports</a></li>
  96. <li><a class="reference internal" href="#setting-up-an-importer">Setting up an importer</a></li>
  97. <li><a class="reference internal" href="#approximating-importlib-import-module">Approximating <code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a></li>
  98. </ul>
  99. </li>
  100. </ul>
  101. </li>
  102. </ul>
  103. </div>
  104. <div>
  105. <h4>Previous topic</h4>
  106. <p class="topless"><a href="runpy.html"
  107. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">runpy</span></code> — Locating and executing Python modules</a></p>
  108. </div>
  109. <div>
  110. <h4>Next topic</h4>
  111. <p class="topless"><a href="importlib.resources.html"
  112. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.resources</span></code> – Package resource reading, opening and access</a></p>
  113. </div>
  114. <div role="note" aria-label="source link">
  115. <h3>This Page</h3>
  116. <ul class="this-page-menu">
  117. <li><a href="../bugs.html">Report a Bug</a></li>
  118. <li>
  119. <a href="https://github.com/python/cpython/blob/main/Doc/library/importlib.rst"
  120. rel="nofollow">Show Source
  121. </a>
  122. </li>
  123. </ul>
  124. </div>
  125. </nav>
  126. </div>
  127. </div>
  128. <div class="related" role="navigation" aria-label="related navigation">
  129. <h3>Navigation</h3>
  130. <ul>
  131. <li class="right" style="margin-right: 10px">
  132. <a href="../genindex.html" title="General Index"
  133. accesskey="I">index</a></li>
  134. <li class="right" >
  135. <a href="../py-modindex.html" title="Python Module Index"
  136. >modules</a> |</li>
  137. <li class="right" >
  138. <a href="importlib.resources.html" title="importlib.resources – Package resource reading, opening and access"
  139. accesskey="N">next</a> |</li>
  140. <li class="right" >
  141. <a href="runpy.html" title="runpy — Locating and executing Python modules"
  142. accesskey="P">previous</a> |</li>
  143. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  144. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  145. <li class="switchers">
  146. <div class="language_switcher_placeholder"></div>
  147. <div class="version_switcher_placeholder"></div>
  148. </li>
  149. <li>
  150. </li>
  151. <li id="cpython-language-and-version">
  152. <a href="../index.html">3.12.3 Documentation</a> &#187;
  153. </li>
  154. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  155. <li class="nav-item nav-item-2"><a href="modules.html" accesskey="U">Importing Modules</a> &#187;</li>
  156. <li class="nav-item nav-item-this"><a href=""><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></li>
  157. <li class="right">
  158. <div class="inline-search" role="search">
  159. <form class="inline-search" action="../search.html" method="get">
  160. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  161. <input type="submit" value="Go" />
  162. </form>
  163. </div>
  164. |
  165. </li>
  166. <li class="right">
  167. <label class="theme-selector-label">
  168. Theme
  169. <select class="theme-selector" oninput="activateTheme(this.value)">
  170. <option value="auto" selected>Auto</option>
  171. <option value="light">Light</option>
  172. <option value="dark">Dark</option>
  173. </select>
  174. </label> |</li>
  175. </ul>
  176. </div>
  177. <div class="document">
  178. <div class="documentwrapper">
  179. <div class="bodywrapper">
  180. <div class="body" role="main">
  181. <section id="module-importlib">
  182. <span id="importlib-the-implementation-of-import"></span><h1><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 class="headerlink" href="#module-importlib" title="Link to this heading">¶</a></h1>
  183. <div class="versionadded">
  184. <p><span class="versionmodified added">New in version 3.1.</span></p>
  185. </div>
  186. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/importlib/__init__.py">Lib/importlib/__init__.py</a></p>
  187. <hr class="docutils" />
  188. <section id="introduction">
  189. <h2>Introduction<a class="headerlink" href="#introduction" title="Link to this heading">¶</a></h2>
  190. <p>The purpose of the <a class="reference internal" href="#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> package is three-fold.</p>
  191. <p>One is to provide the
  192. implementation of the <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement (and thus, by extension, the
  193. <a class="reference internal" href="functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> function) in Python source code. This provides an
  194. implementation of <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code> which is portable to any Python
  195. interpreter. This also provides an implementation which is easier to
  196. comprehend than one implemented in a programming language other than Python.</p>
  197. <p>Two, the components to implement <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> are exposed in this
  198. package, making it easier for users to create their own custom objects (known
  199. generically as an <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a>) to participate in the import process.</p>
  200. <p>Three, the package contains modules exposing additional functionality for
  201. managing aspects of Python packages:</p>
  202. <ul class="simple">
  203. <li><p><a class="reference internal" href="importlib.metadata.html#module-importlib.metadata" title="importlib.metadata: Accessing package metadata"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.metadata</span></code></a> presents access to metadata from third-party
  204. distributions.</p></li>
  205. <li><p><a class="reference internal" href="importlib.resources.html#module-importlib.resources" title="importlib.resources: Package resource reading, opening, and access"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.resources</span></code></a> provides routines for accessing non-code
  206. “resources” from Python packages.</p></li>
  207. </ul>
  208. <div class="admonition seealso">
  209. <p class="admonition-title">See also</p>
  210. <dl class="simple">
  211. <dt><a class="reference internal" href="../reference/simple_stmts.html#import"><span class="std std-ref">The import statement</span></a></dt><dd><p>The language reference for the <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement.</p>
  212. </dd>
  213. <dt><a class="reference external" href="https://www.python.org/doc/essays/packages/">Packages specification</a></dt><dd><p>Original specification of packages. Some semantics have changed since
  214. the writing of this document (e.g. redirecting based on <code class="docutils literal notranslate"><span class="pre">None</span></code>
  215. in <a class="reference internal" href="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>
  216. </dd>
  217. <dt>The <a class="reference internal" href="#importlib.__import__" title="importlib.__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> function</dt><dd><p>The <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement is syntactic sugar for this function.</p>
  218. </dd>
  219. <dt><a class="reference internal" href="sys_path_init.html#sys-path-init"><span class="std std-ref">The initialization of the sys.path module search path</span></a></dt><dd><p>The initialization of <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>.</p>
  220. </dd>
  221. <dt><span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0235/"><strong>PEP 235</strong></a></dt><dd><p>Import on Case-Insensitive Platforms</p>
  222. </dd>
  223. <dt><span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0263/"><strong>PEP 263</strong></a></dt><dd><p>Defining Python Source Code Encodings</p>
  224. </dd>
  225. <dt><span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a></dt><dd><p>New Import Hooks</p>
  226. </dd>
  227. <dt><span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0328/"><strong>PEP 328</strong></a></dt><dd><p>Imports: Multi-Line and Absolute/Relative</p>
  228. </dd>
  229. <dt><span class="target" id="index-4"></span><a class="pep reference external" href="https://peps.python.org/pep-0366/"><strong>PEP 366</strong></a></dt><dd><p>Main module explicit relative imports</p>
  230. </dd>
  231. <dt><span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0420/"><strong>PEP 420</strong></a></dt><dd><p>Implicit namespace packages</p>
  232. </dd>
  233. <dt><span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0451/"><strong>PEP 451</strong></a></dt><dd><p>A ModuleSpec Type for the Import System</p>
  234. </dd>
  235. <dt><span class="target" id="index-7"></span><a class="pep reference external" href="https://peps.python.org/pep-0488/"><strong>PEP 488</strong></a></dt><dd><p>Elimination of PYO files</p>
  236. </dd>
  237. <dt><span class="target" id="index-8"></span><a class="pep reference external" href="https://peps.python.org/pep-0489/"><strong>PEP 489</strong></a></dt><dd><p>Multi-phase extension module initialization</p>
  238. </dd>
  239. <dt><span class="target" id="index-9"></span><a class="pep reference external" href="https://peps.python.org/pep-0552/"><strong>PEP 552</strong></a></dt><dd><p>Deterministic pycs</p>
  240. </dd>
  241. <dt><span class="target" id="index-10"></span><a class="pep reference external" href="https://peps.python.org/pep-3120/"><strong>PEP 3120</strong></a></dt><dd><p>Using UTF-8 as the Default Source Encoding</p>
  242. </dd>
  243. <dt><span class="target" id="index-11"></span><a class="pep reference external" href="https://peps.python.org/pep-3147/"><strong>PEP 3147</strong></a></dt><dd><p>PYC Repository Directories</p>
  244. </dd>
  245. </dl>
  246. </div>
  247. </section>
  248. <section id="functions">
  249. <h2>Functions<a class="headerlink" href="#functions" title="Link to this heading">¶</a></h2>
  250. <dl class="py function">
  251. <dt class="sig sig-object py" id="importlib.__import__">
  252. <span class="sig-prename descclassname"><span class="pre">importlib.</span></span><span class="sig-name descname"><span class="pre">__import__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">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">locals</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">fromlist</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">level</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.__import__" title="Link to this definition">¶</a></dt>
  253. <dd><p>An implementation of the built-in <a class="reference internal" href="functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> function.</p>
  254. <div class="admonition note">
  255. <p class="admonition-title">Note</p>
  256. <p>Programmatic importing of modules should use <a class="reference internal" href="#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">import_module()</span></code></a>
  257. instead of this function.</p>
  258. </div>
  259. </dd></dl>
  260. <dl class="py function">
  261. <dt class="sig sig-object py" id="importlib.import_module">
  262. <span class="sig-prename descclassname"><span class="pre">importlib.</span></span><span class="sig-name descname"><span class="pre">import_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">package</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="#importlib.import_module" title="Link to this definition">¶</a></dt>
  263. <dd><p>Import a module. The <em>name</em> argument specifies what module to
  264. import in absolute or relative terms
  265. (e.g. either <code class="docutils literal notranslate"><span class="pre">pkg.mod</span></code> or <code class="docutils literal notranslate"><span class="pre">..mod</span></code>). If the name is
  266. specified in relative terms, then the <em>package</em> argument must be set to
  267. the name of the package which is to act as the anchor for resolving the
  268. package name (e.g. <code class="docutils literal notranslate"><span class="pre">import_module('..mod',</span> <span class="pre">'pkg.subpkg')</span></code> will import
  269. <code class="docutils literal notranslate"><span class="pre">pkg.mod</span></code>).</p>
  270. <p>The <a class="reference internal" href="#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">import_module()</span></code></a> function acts as a simplifying wrapper around
  271. <a class="reference internal" href="#importlib.__import__" title="importlib.__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.__import__()</span></code></a>. This means all semantics of the function are
  272. derived from <a class="reference internal" href="#importlib.__import__" title="importlib.__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.__import__()</span></code></a>. The most important difference
  273. between these two functions is that <a class="reference internal" href="#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">import_module()</span></code></a> returns the
  274. specified package or module (e.g. <code class="docutils literal notranslate"><span class="pre">pkg.mod</span></code>), while <a class="reference internal" href="functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a>
  275. returns the top-level package or module (e.g. <code class="docutils literal notranslate"><span class="pre">pkg</span></code>).</p>
  276. <p>If you are dynamically importing a module that was created since the
  277. interpreter began execution (e.g., created a Python source file), you may
  278. need to call <a class="reference internal" href="#importlib.invalidate_caches" title="importlib.invalidate_caches"><code class="xref py py-func docutils literal notranslate"><span class="pre">invalidate_caches()</span></code></a> in order for the new module to be
  279. noticed by the import system.</p>
  280. <div class="versionchanged">
  281. <p><span class="versionmodified changed">Changed in version 3.3: </span>Parent packages are automatically imported.</p>
  282. </div>
  283. </dd></dl>
  284. <dl class="py function">
  285. <dt class="sig sig-object py" id="importlib.invalidate_caches">
  286. <span class="sig-prename descclassname"><span class="pre">importlib.</span></span><span class="sig-name descname"><span class="pre">invalidate_caches</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.invalidate_caches" title="Link to this definition">¶</a></dt>
  287. <dd><p>Invalidate the internal caches of finders stored at
  288. <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a>. If a finder implements <code class="docutils literal notranslate"><span class="pre">invalidate_caches()</span></code> then it
  289. will be called to perform the invalidation. This function should be called
  290. if any modules are created/installed while your program is running to
  291. guarantee all finders will notice the new module’s existence.</p>
  292. <div class="versionadded">
  293. <p><span class="versionmodified added">New in version 3.3.</span></p>
  294. </div>
  295. <div class="versionchanged">
  296. <p><span class="versionmodified changed">Changed in version 3.10: </span>Namespace packages created/installed in a different <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>
  297. location after the same namespace was already imported are noticed.</p>
  298. </div>
  299. </dd></dl>
  300. <dl class="py function">
  301. <dt class="sig sig-object py" id="importlib.reload">
  302. <span class="sig-prename descclassname"><span class="pre">importlib.</span></span><span class="sig-name descname"><span class="pre">reload</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.reload" title="Link to this definition">¶</a></dt>
  303. <dd><p>Reload a previously imported <em>module</em>. The argument must be a module object,
  304. so it must have been successfully imported before. This is useful if you
  305. have edited the module source file using an external editor and want to try
  306. out the new version without leaving the Python interpreter. The return value
  307. is the module object (which can be different if re-importing causes a
  308. different object to be placed in <a class="reference internal" href="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>
  309. <p>When <a class="reference internal" href="#importlib.reload" title="importlib.reload"><code class="xref py py-func docutils literal notranslate"><span class="pre">reload()</span></code></a> is executed:</p>
  310. <ul class="simple">
  311. <li><p>Python module’s code is recompiled and the module-level code re-executed,
  312. defining a new set of objects which are bound to names in the module’s
  313. dictionary by reusing the <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> which originally loaded the
  314. module. The <code class="docutils literal notranslate"><span class="pre">init</span></code> function of extension modules is not called a second
  315. time.</p></li>
  316. <li><p>As with all other objects in Python the old objects are only reclaimed
  317. after their reference counts drop to zero.</p></li>
  318. <li><p>The names in the module namespace are updated to point to any new or
  319. changed objects.</p></li>
  320. <li><p>Other references to the old objects (such as names external to the module) are
  321. not rebound to refer to the new objects and must be updated in each namespace
  322. where they occur if that is desired.</p></li>
  323. </ul>
  324. <p>There are a number of other caveats:</p>
  325. <p>When a module is reloaded, its dictionary (containing the module’s global
  326. variables) is retained. Redefinitions of names will override the old
  327. definitions, so this is generally not a problem. If the new version of a
  328. module does not define a name that was defined by the old version, the old
  329. definition remains. This feature can be used to the module’s advantage if it
  330. maintains a global table or cache of objects — with a <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a>
  331. statement it can test for the table’s presence and skip its initialization if
  332. desired:</p>
  333. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
  334. <span class="n">cache</span>
  335. <span class="k">except</span> <span class="ne">NameError</span><span class="p">:</span>
  336. <span class="n">cache</span> <span class="o">=</span> <span class="p">{}</span>
  337. </pre></div>
  338. </div>
  339. <p>It is generally not very useful to reload built-in or dynamically loaded
  340. modules. Reloading <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>, <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>, <a class="reference internal" href="builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a> and other
  341. key modules is not recommended. In many cases extension modules are not
  342. designed to be initialized more than once, and may fail in arbitrary ways
  343. when reloaded.</p>
  344. <p>If a module imports objects from another module using <a class="reference internal" href="../reference/simple_stmts.html#from"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> …
  345. <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> …, calling <a class="reference internal" href="#importlib.reload" title="importlib.reload"><code class="xref py py-func docutils literal notranslate"><span class="pre">reload()</span></code></a> for the other module does not
  346. redefine the objects imported from it — one way around this is to
  347. re-execute the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code> statement, another is to use <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code>
  348. and qualified names (<em>module.name</em>) instead.</p>
  349. <p>If a module instantiates instances of a class, reloading the module that
  350. defines the class does not affect the method definitions of the instances —
  351. they continue to use the old class definition. The same is true for derived
  352. classes.</p>
  353. <div class="versionadded">
  354. <p><span class="versionmodified added">New in version 3.4.</span></p>
  355. </div>
  356. <div class="versionchanged">
  357. <p><span class="versionmodified changed">Changed in version 3.7: </span><a class="reference internal" href="exceptions.html#ModuleNotFoundError" title="ModuleNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ModuleNotFoundError</span></code></a> is raised when the module being reloaded lacks
  358. a <a class="reference internal" href="#importlib.machinery.ModuleSpec" title="importlib.machinery.ModuleSpec"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModuleSpec</span></code></a>.</p>
  359. </div>
  360. </dd></dl>
  361. </section>
  362. <section id="module-importlib.abc">
  363. <span id="importlib-abc-abstract-base-classes-related-to-import"></span><h2><a class="reference internal" href="#module-importlib.abc" title="importlib.abc: Abstract base classes related to import"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.abc</span></code></a> – Abstract base classes related to import<a class="headerlink" href="#module-importlib.abc" title="Link to this heading">¶</a></h2>
  364. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/importlib/abc.py">Lib/importlib/abc.py</a></p>
  365. <hr class="docutils" />
  366. <p>The <a class="reference internal" href="#module-importlib.abc" title="importlib.abc: Abstract base classes related to import"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.abc</span></code></a> module contains all of the core abstract base classes
  367. used by <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a>. Some subclasses of the core abstract base classes
  368. are also provided to help in implementing the core ABCs.</p>
  369. <p>ABC hierarchy:</p>
  370. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">object</span>
  371. <span class="o">+--</span> <span class="n">MetaPathFinder</span>
  372. <span class="o">+--</span> <span class="n">PathEntryFinder</span>
  373. <span class="o">+--</span> <span class="n">Loader</span>
  374. <span class="o">+--</span> <span class="n">ResourceLoader</span> <span class="o">--------+</span>
  375. <span class="o">+--</span> <span class="n">InspectLoader</span> <span class="o">|</span>
  376. <span class="o">+--</span> <span class="n">ExecutionLoader</span> <span class="o">--+</span>
  377. <span class="o">+--</span> <span class="n">FileLoader</span>
  378. <span class="o">+--</span> <span class="n">SourceLoader</span>
  379. </pre></div>
  380. </div>
  381. <dl class="py class">
  382. <dt class="sig sig-object py" id="importlib.abc.MetaPathFinder">
  383. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.abc.</span></span><span class="sig-name descname"><span class="pre">MetaPathFinder</span></span><a class="headerlink" href="#importlib.abc.MetaPathFinder" title="Link to this definition">¶</a></dt>
  384. <dd><p>An abstract base class representing a <a class="reference internal" href="../glossary.html#term-meta-path-finder"><span class="xref std std-term">meta path finder</span></a>.</p>
  385. <div class="versionadded">
  386. <p><span class="versionmodified added">New in version 3.3.</span></p>
  387. </div>
  388. <div class="versionchanged">
  389. <p><span class="versionmodified changed">Changed in version 3.10: </span>No longer a subclass of <code class="xref py py-class docutils literal notranslate"><span class="pre">Finder</span></code>.</p>
  390. </div>
  391. <dl class="py method">
  392. <dt class="sig sig-object py" id="importlib.abc.MetaPathFinder.find_spec">
  393. <span class="sig-name descname"><span class="pre">find_spec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">target</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="#importlib.abc.MetaPathFinder.find_spec" title="Link to this definition">¶</a></dt>
  394. <dd><p>An abstract method for finding a <a class="reference internal" href="../glossary.html#term-module-spec"><span class="xref std std-term">spec</span></a> for
  395. the specified module. If this is a top-level import, <em>path</em> will
  396. be <code class="docutils literal notranslate"><span class="pre">None</span></code>. Otherwise, this is a search for a subpackage or
  397. module and <em>path</em> will be the value of <a class="reference internal" href="../reference/import.html#path__" title="__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__path__</span></code></a> from the
  398. parent package. If a spec cannot be found, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned.
  399. When passed in, <code class="docutils literal notranslate"><span class="pre">target</span></code> is a module object that the finder may
  400. use to make a more educated guess about what spec to return.
  401. <a class="reference internal" href="#importlib.util.spec_from_loader" title="importlib.util.spec_from_loader"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.spec_from_loader()</span></code></a> may be useful for implementing
  402. concrete <code class="docutils literal notranslate"><span class="pre">MetaPathFinders</span></code>.</p>
  403. <div class="versionadded">
  404. <p><span class="versionmodified added">New in version 3.4.</span></p>
  405. </div>
  406. </dd></dl>
  407. <dl class="py method">
  408. <dt class="sig sig-object py" id="importlib.abc.MetaPathFinder.invalidate_caches">
  409. <span class="sig-name descname"><span class="pre">invalidate_caches</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.MetaPathFinder.invalidate_caches" title="Link to this definition">¶</a></dt>
  410. <dd><p>An optional method which, when called, should invalidate any internal
  411. cache used by the finder. Used by <a class="reference internal" href="#importlib.invalidate_caches" title="importlib.invalidate_caches"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.invalidate_caches()</span></code></a>
  412. when invalidating the caches of all finders on <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a>.</p>
  413. <div class="versionchanged">
  414. <p><span class="versionmodified changed">Changed in version 3.4: </span>Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> when called instead of <a class="reference internal" href="constants.html#NotImplemented" title="NotImplemented"><code class="xref py py-data docutils literal notranslate"><span class="pre">NotImplemented</span></code></a>.</p>
  415. </div>
  416. </dd></dl>
  417. </dd></dl>
  418. <dl class="py class">
  419. <dt class="sig sig-object py" id="importlib.abc.PathEntryFinder">
  420. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.abc.</span></span><span class="sig-name descname"><span class="pre">PathEntryFinder</span></span><a class="headerlink" href="#importlib.abc.PathEntryFinder" title="Link to this definition">¶</a></dt>
  421. <dd><p>An abstract base class representing a <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a>. Though
  422. it bears some similarities to <a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">MetaPathFinder</span></code></a>, <code class="docutils literal notranslate"><span class="pre">PathEntryFinder</span></code>
  423. is meant for use only within the path-based import subsystem provided
  424. by <a class="reference internal" href="#importlib.machinery.PathFinder" title="importlib.machinery.PathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.PathFinder</span></code></a>.</p>
  425. <div class="versionadded">
  426. <p><span class="versionmodified added">New in version 3.3.</span></p>
  427. </div>
  428. <div class="versionchanged">
  429. <p><span class="versionmodified changed">Changed in version 3.10: </span>No longer a subclass of <code class="xref py py-class docutils literal notranslate"><span class="pre">Finder</span></code>.</p>
  430. </div>
  431. <dl class="py method">
  432. <dt class="sig sig-object py" id="importlib.abc.PathEntryFinder.find_spec">
  433. <span class="sig-name descname"><span class="pre">find_spec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">target</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="#importlib.abc.PathEntryFinder.find_spec" title="Link to this definition">¶</a></dt>
  434. <dd><p>An abstract method for finding a <a class="reference internal" href="../glossary.html#term-module-spec"><span class="xref std std-term">spec</span></a> for
  435. the specified module. The finder will search for the module only
  436. within the <a class="reference internal" href="../glossary.html#term-path-entry"><span class="xref std std-term">path entry</span></a> to which it is assigned. If a spec
  437. cannot be found, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned. When passed in, <code class="docutils literal notranslate"><span class="pre">target</span></code>
  438. is a module object that the finder may use to make a more educated
  439. guess about what spec to return. <a class="reference internal" href="#importlib.util.spec_from_loader" title="importlib.util.spec_from_loader"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.spec_from_loader()</span></code></a>
  440. may be useful for implementing concrete <code class="docutils literal notranslate"><span class="pre">PathEntryFinders</span></code>.</p>
  441. <div class="versionadded">
  442. <p><span class="versionmodified added">New in version 3.4.</span></p>
  443. </div>
  444. </dd></dl>
  445. <dl class="py method">
  446. <dt class="sig sig-object py" id="importlib.abc.PathEntryFinder.invalidate_caches">
  447. <span class="sig-name descname"><span class="pre">invalidate_caches</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.PathEntryFinder.invalidate_caches" title="Link to this definition">¶</a></dt>
  448. <dd><p>An optional method which, when called, should invalidate any internal
  449. cache used by the finder. Used by
  450. <a class="reference internal" href="#importlib.machinery.PathFinder.invalidate_caches" title="importlib.machinery.PathFinder.invalidate_caches"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.machinery.PathFinder.invalidate_caches()</span></code></a>
  451. when invalidating the caches of all cached finders.</p>
  452. </dd></dl>
  453. </dd></dl>
  454. <dl class="py class">
  455. <dt class="sig sig-object py" id="importlib.abc.Loader">
  456. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.abc.</span></span><span class="sig-name descname"><span class="pre">Loader</span></span><a class="headerlink" href="#importlib.abc.Loader" title="Link to this definition">¶</a></dt>
  457. <dd><p>An abstract base class for a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a>.
  458. See <span class="target" id="index-12"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a> for the exact definition for a loader.</p>
  459. <p>Loaders that wish to support resource reading should implement a
  460. <code class="xref py py-meth docutils literal notranslate"><span class="pre">get_resource_reader()</span></code> method as specified by
  461. <a class="reference internal" href="importlib.resources.abc.html#importlib.resources.abc.ResourceReader" title="importlib.resources.abc.ResourceReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.resources.abc.ResourceReader</span></code></a>.</p>
  462. <div class="versionchanged">
  463. <p><span class="versionmodified changed">Changed in version 3.7: </span>Introduced the optional <code class="xref py py-meth docutils literal notranslate"><span class="pre">get_resource_reader()</span></code> method.</p>
  464. </div>
  465. <dl class="py method">
  466. <dt class="sig sig-object py" id="importlib.abc.Loader.create_module">
  467. <span class="sig-name descname"><span class="pre">create_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">spec</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Loader.create_module" title="Link to this definition">¶</a></dt>
  468. <dd><p>A method that returns the module object to use when
  469. importing a module. This method may return <code class="docutils literal notranslate"><span class="pre">None</span></code>,
  470. indicating that default module creation semantics should take place.</p>
  471. <div class="versionadded">
  472. <p><span class="versionmodified added">New in version 3.4.</span></p>
  473. </div>
  474. <div class="versionchanged">
  475. <p><span class="versionmodified changed">Changed in version 3.6: </span>This method is no longer optional when
  476. <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> is defined.</p>
  477. </div>
  478. </dd></dl>
  479. <dl class="py method">
  480. <dt class="sig sig-object py" id="importlib.abc.Loader.exec_module">
  481. <span class="sig-name descname"><span class="pre">exec_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Loader.exec_module" title="Link to this definition">¶</a></dt>
  482. <dd><p>An abstract method that executes the module in its own namespace
  483. when a module is imported or reloaded. The module should already
  484. be initialized when <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> is called. When this method exists,
  485. <a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</span></code></a> must be defined.</p>
  486. <div class="versionadded">
  487. <p><span class="versionmodified added">New in version 3.4.</span></p>
  488. </div>
  489. <div class="versionchanged">
  490. <p><span class="versionmodified changed">Changed in version 3.6: </span><a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</span></code></a> must also be defined.</p>
  491. </div>
  492. </dd></dl>
  493. <dl class="py method">
  494. <dt class="sig sig-object py" id="importlib.abc.Loader.load_module">
  495. <span class="sig-name descname"><span class="pre">load_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Loader.load_module" title="Link to this definition">¶</a></dt>
  496. <dd><p>A legacy method for loading a module. If the module cannot be
  497. loaded, <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> is raised, otherwise the loaded module is
  498. returned.</p>
  499. <p>If the requested module already exists in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>, that
  500. module should be used and reloaded.
  501. Otherwise the loader should create a new module and insert it into
  502. <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> before any loading begins, to prevent recursion
  503. from the import. If the loader inserted a module and the load fails, it
  504. must be removed by the loader from <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>; modules already
  505. in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> before the loader began execution should be left
  506. alone.</p>
  507. <p>The loader should set several attributes on the module
  508. (note that some of these attributes can change when a module is
  509. reloaded):</p>
  510. <ul class="simple">
  511. <li><dl class="simple">
  512. <dt><a class="reference internal" href="../reference/import.html#name__" title="__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a></dt><dd><p>The module’s fully qualified name.
  513. It is <code class="docutils literal notranslate"><span class="pre">'__main__'</span></code> for an executed module.</p>
  514. </dd>
  515. </dl>
  516. </li>
  517. <li><dl class="simple">
  518. <dt><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></dt><dd><p>The location the <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> used to load the module.
  519. For example, for modules loaded from a .py file this is the filename.
  520. It is not set on all modules (e.g. built-in modules).</p>
  521. </dd>
  522. </dl>
  523. </li>
  524. <li><dl class="simple">
  525. <dt><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></dt><dd><p>The filename of a compiled version of the module’s code.
  526. It is not set on all modules (e.g. built-in modules).</p>
  527. </dd>
  528. </dl>
  529. </li>
  530. <li><dl class="simple">
  531. <dt><a class="reference internal" href="../reference/import.html#path__" title="__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__path__</span></code></a></dt><dd><p>The list of locations where the package’s submodules will be found.
  532. Most of the time this is a single directory.
  533. The import system passes this attribute to <code class="docutils literal notranslate"><span class="pre">__import__()</span></code> and to finders
  534. in the same way as <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> but just for the package.
  535. It is not set on non-package modules so it can be used
  536. as an indicator that the module is a package.</p>
  537. </dd>
  538. </dl>
  539. </li>
  540. <li><dl class="simple">
  541. <dt><a class="reference internal" href="../reference/import.html#package__" title="__package__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__package__</span></code></a></dt><dd><p>The fully qualified name of the package the module is in (or the
  542. empty string for a top-level module).
  543. If the module is a package then this is the same as <a class="reference internal" href="../reference/import.html#name__" title="__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a>.</p>
  544. </dd>
  545. </dl>
  546. </li>
  547. <li><dl class="simple">
  548. <dt><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></dt><dd><p>The <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> used to load the module.</p>
  549. </dd>
  550. </dl>
  551. </li>
  552. </ul>
  553. <p>When <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> is available then backwards-compatible
  554. functionality is provided.</p>
  555. <div class="versionchanged">
  556. <p><span class="versionmodified changed">Changed in version 3.4: </span>Raise <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> when called instead of
  557. <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>. Functionality provided when
  558. <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> is available.</p>
  559. </div>
  560. <div class="deprecated">
  561. <p><span class="versionmodified deprecated">Deprecated since version 3.4: </span>The recommended API for loading a module is <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a>
  562. (and <a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</span></code></a>). Loaders should implement it instead of
  563. <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">load_module()</span></code></a>. The import machinery takes care of all the
  564. other responsibilities of <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">load_module()</span></code></a> when
  565. <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> is implemented.</p>
  566. </div>
  567. </dd></dl>
  568. </dd></dl>
  569. <dl class="py class">
  570. <dt class="sig sig-object py" id="importlib.abc.ResourceLoader">
  571. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.abc.</span></span><span class="sig-name descname"><span class="pre">ResourceLoader</span></span><a class="headerlink" href="#importlib.abc.ResourceLoader" title="Link to this definition">¶</a></dt>
  572. <dd><p>An abstract base class for a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> which implements the optional
  573. <span class="target" id="index-13"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a> protocol for loading arbitrary resources from the storage
  574. back-end.</p>
  575. <div class="deprecated">
  576. <p><span class="versionmodified deprecated">Deprecated since version 3.7: </span>This ABC is deprecated in favour of supporting resource loading
  577. through <a class="reference internal" href="importlib.resources.abc.html#importlib.resources.abc.ResourceReader" title="importlib.resources.abc.ResourceReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.resources.abc.ResourceReader</span></code></a>.</p>
  578. </div>
  579. <dl class="py method">
  580. <dt class="sig sig-object py" id="importlib.abc.ResourceLoader.get_data">
  581. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">get_data</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.ResourceLoader.get_data" title="Link to this definition">¶</a></dt>
  582. <dd><p>An abstract method to return the bytes for the data located at <em>path</em>.
  583. Loaders that have a file-like storage back-end
  584. that allows storing arbitrary data
  585. can implement this abstract method to give direct access
  586. to the data stored. <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is to be raised if the <em>path</em> cannot
  587. be found. The <em>path</em> is expected to be constructed using a module’s
  588. <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 or an item from a package’s <a class="reference internal" href="../reference/import.html#path__" title="__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__path__</span></code></a>.</p>
  589. <div class="versionchanged">
  590. <p><span class="versionmodified changed">Changed in version 3.4: </span>Raises <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
  591. </div>
  592. </dd></dl>
  593. </dd></dl>
  594. <dl class="py class">
  595. <dt class="sig sig-object py" id="importlib.abc.InspectLoader">
  596. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.abc.</span></span><span class="sig-name descname"><span class="pre">InspectLoader</span></span><a class="headerlink" href="#importlib.abc.InspectLoader" title="Link to this definition">¶</a></dt>
  597. <dd><p>An abstract base class for a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> which implements the optional
  598. <span class="target" id="index-14"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a> protocol for loaders that inspect modules.</p>
  599. <dl class="py method">
  600. <dt class="sig sig-object py" id="importlib.abc.InspectLoader.get_code">
  601. <span class="sig-name descname"><span class="pre">get_code</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.get_code" title="Link to this definition">¶</a></dt>
  602. <dd><p>Return the code object for a module, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the module does not
  603. have a code object (as would be the case, for example, for a built-in
  604. module). Raise an <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> if loader cannot find the
  605. requested module.</p>
  606. <div class="admonition note">
  607. <p class="admonition-title">Note</p>
  608. <p>While the method has a default implementation, it is suggested that
  609. it be overridden if possible for performance.</p>
  610. </div>
  611. <div class="versionchanged" id="index-15">
  612. <p><span class="versionmodified changed">Changed in version 3.4: </span>No longer abstract and a concrete implementation is provided.</p>
  613. </div>
  614. </dd></dl>
  615. <dl class="py method">
  616. <dt class="sig sig-object py" id="importlib.abc.InspectLoader.get_source">
  617. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">get_source</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.get_source" title="Link to this definition">¶</a></dt>
  618. <dd><p>An abstract method to return the source of a module. It is returned as
  619. a text string using <a class="reference internal" href="../glossary.html#term-universal-newlines"><span class="xref std std-term">universal newlines</span></a>, translating all
  620. recognized line separators into <code class="docutils literal notranslate"><span class="pre">'\n'</span></code> characters. Returns <code class="docutils literal notranslate"><span class="pre">None</span></code>
  621. if no source is available (e.g. a built-in module). Raises
  622. <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> if the loader cannot find the module specified.</p>
  623. <div class="versionchanged">
  624. <p><span class="versionmodified changed">Changed in version 3.4: </span>Raises <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
  625. </div>
  626. </dd></dl>
  627. <dl class="py method">
  628. <dt class="sig sig-object py" id="importlib.abc.InspectLoader.is_package">
  629. <span class="sig-name descname"><span class="pre">is_package</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.is_package" title="Link to this definition">¶</a></dt>
  630. <dd><p>An optional method to return a true value if the module is a package, a
  631. false value otherwise. <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> is raised if the
  632. <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> cannot find the module.</p>
  633. <div class="versionchanged">
  634. <p><span class="versionmodified changed">Changed in version 3.4: </span>Raises <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
  635. </div>
  636. </dd></dl>
  637. <dl class="py method">
  638. <dt class="sig sig-object py" id="importlib.abc.InspectLoader.source_to_code">
  639. <em class="property"><span class="pre">static</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">source_to_code</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'&lt;string&gt;'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.source_to_code" title="Link to this definition">¶</a></dt>
  640. <dd><p>Create a code object from Python source.</p>
  641. <p>The <em>data</em> argument can be whatever the <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> function
  642. supports (i.e. string or bytes). The <em>path</em> argument should be
  643. the “path” to where the source code originated from, which can be an
  644. abstract concept (e.g. location in a zip file).</p>
  645. <p>With the subsequent code object one can execute it in a module by
  646. running <code class="docutils literal notranslate"><span class="pre">exec(code,</span> <span class="pre">module.__dict__)</span></code>.</p>
  647. <div class="versionadded">
  648. <p><span class="versionmodified added">New in version 3.4.</span></p>
  649. </div>
  650. <div class="versionchanged">
  651. <p><span class="versionmodified changed">Changed in version 3.5: </span>Made the method static.</p>
  652. </div>
  653. </dd></dl>
  654. <dl class="py method">
  655. <dt class="sig sig-object py" id="importlib.abc.InspectLoader.exec_module">
  656. <span class="sig-name descname"><span class="pre">exec_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.exec_module" title="Link to this definition">¶</a></dt>
  657. <dd><p>Implementation of <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Loader.exec_module()</span></code></a>.</p>
  658. <div class="versionadded">
  659. <p><span class="versionmodified added">New in version 3.4.</span></p>
  660. </div>
  661. </dd></dl>
  662. <dl class="py method">
  663. <dt class="sig sig-object py" id="importlib.abc.InspectLoader.load_module">
  664. <span class="sig-name descname"><span class="pre">load_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.load_module" title="Link to this definition">¶</a></dt>
  665. <dd><p>Implementation of <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Loader.load_module()</span></code></a>.</p>
  666. <div class="deprecated">
  667. <p><span class="versionmodified deprecated">Deprecated since version 3.4: </span>use <a class="reference internal" href="#importlib.abc.InspectLoader.exec_module" title="importlib.abc.InspectLoader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> instead.</p>
  668. </div>
  669. </dd></dl>
  670. </dd></dl>
  671. <dl class="py class">
  672. <dt class="sig sig-object py" id="importlib.abc.ExecutionLoader">
  673. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.abc.</span></span><span class="sig-name descname"><span class="pre">ExecutionLoader</span></span><a class="headerlink" href="#importlib.abc.ExecutionLoader" title="Link to this definition">¶</a></dt>
  674. <dd><p>An abstract base class which inherits from <a class="reference internal" href="#importlib.abc.InspectLoader" title="importlib.abc.InspectLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">InspectLoader</span></code></a> that,
  675. when implemented, helps a module to be executed as a script. The ABC
  676. represents an optional <span class="target" id="index-16"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a> protocol.</p>
  677. <dl class="py method">
  678. <dt class="sig sig-object py" id="importlib.abc.ExecutionLoader.get_filename">
  679. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">get_filename</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.ExecutionLoader.get_filename" title="Link to this definition">¶</a></dt>
  680. <dd><p>An abstract method that is to return the value of <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> for
  681. the specified module. If no path is available, <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> is
  682. raised.</p>
  683. <p>If source code is available, then the method should return the path to
  684. the source file, regardless of whether a bytecode was used to load the
  685. module.</p>
  686. <div class="versionchanged">
  687. <p><span class="versionmodified changed">Changed in version 3.4: </span>Raises <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
  688. </div>
  689. </dd></dl>
  690. </dd></dl>
  691. <dl class="py class">
  692. <dt class="sig sig-object py" id="importlib.abc.FileLoader">
  693. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.abc.</span></span><span class="sig-name descname"><span class="pre">FileLoader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.FileLoader" title="Link to this definition">¶</a></dt>
  694. <dd><p>An abstract base class which inherits from <a class="reference internal" href="#importlib.abc.ResourceLoader" title="importlib.abc.ResourceLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">ResourceLoader</span></code></a> and
  695. <a class="reference internal" href="#importlib.abc.ExecutionLoader" title="importlib.abc.ExecutionLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">ExecutionLoader</span></code></a>, providing concrete implementations of
  696. <a class="reference internal" href="#importlib.abc.ResourceLoader.get_data" title="importlib.abc.ResourceLoader.get_data"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ResourceLoader.get_data()</span></code></a> and <a class="reference internal" href="#importlib.abc.ExecutionLoader.get_filename" title="importlib.abc.ExecutionLoader.get_filename"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ExecutionLoader.get_filename()</span></code></a>.</p>
  697. <p>The <em>fullname</em> argument is a fully resolved name of the module the loader is
  698. to handle. The <em>path</em> argument is the path to the file for the module.</p>
  699. <div class="versionadded">
  700. <p><span class="versionmodified added">New in version 3.3.</span></p>
  701. </div>
  702. <dl class="py attribute">
  703. <dt class="sig sig-object py" id="importlib.abc.FileLoader.name">
  704. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#importlib.abc.FileLoader.name" title="Link to this definition">¶</a></dt>
  705. <dd><p>The name of the module the loader can handle.</p>
  706. </dd></dl>
  707. <dl class="py attribute">
  708. <dt class="sig sig-object py" id="importlib.abc.FileLoader.path">
  709. <span class="sig-name descname"><span class="pre">path</span></span><a class="headerlink" href="#importlib.abc.FileLoader.path" title="Link to this definition">¶</a></dt>
  710. <dd><p>Path to the file of the module.</p>
  711. </dd></dl>
  712. <dl class="py method">
  713. <dt class="sig sig-object py" id="importlib.abc.FileLoader.load_module">
  714. <span class="sig-name descname"><span class="pre">load_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.FileLoader.load_module" title="Link to this definition">¶</a></dt>
  715. <dd><p>Calls super’s <code class="docutils literal notranslate"><span class="pre">load_module()</span></code>.</p>
  716. <div class="deprecated">
  717. <p><span class="versionmodified deprecated">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Loader.exec_module()</span></code></a> instead.</p>
  718. </div>
  719. </dd></dl>
  720. <dl class="py method">
  721. <dt class="sig sig-object py" id="importlib.abc.FileLoader.get_filename">
  722. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">get_filename</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.FileLoader.get_filename" title="Link to this definition">¶</a></dt>
  723. <dd><p>Returns <a class="reference internal" href="#importlib.abc.FileLoader.path" title="importlib.abc.FileLoader.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a>.</p>
  724. </dd></dl>
  725. <dl class="py method">
  726. <dt class="sig sig-object py" id="importlib.abc.FileLoader.get_data">
  727. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">get_data</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.FileLoader.get_data" title="Link to this definition">¶</a></dt>
  728. <dd><p>Reads <em>path</em> as a binary file and returns the bytes from it.</p>
  729. </dd></dl>
  730. </dd></dl>
  731. <dl class="py class">
  732. <dt class="sig sig-object py" id="importlib.abc.SourceLoader">
  733. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.abc.</span></span><span class="sig-name descname"><span class="pre">SourceLoader</span></span><a class="headerlink" href="#importlib.abc.SourceLoader" title="Link to this definition">¶</a></dt>
  734. <dd><p>An abstract base class for implementing source (and optionally bytecode)
  735. file loading. The class inherits from both <a class="reference internal" href="#importlib.abc.ResourceLoader" title="importlib.abc.ResourceLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">ResourceLoader</span></code></a> and
  736. <a class="reference internal" href="#importlib.abc.ExecutionLoader" title="importlib.abc.ExecutionLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">ExecutionLoader</span></code></a>, requiring the implementation of:</p>
  737. <ul class="simple">
  738. <li><p><a class="reference internal" href="#importlib.abc.ResourceLoader.get_data" title="importlib.abc.ResourceLoader.get_data"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ResourceLoader.get_data()</span></code></a></p></li>
  739. <li><dl class="simple">
  740. <dt><a class="reference internal" href="#importlib.abc.ExecutionLoader.get_filename" title="importlib.abc.ExecutionLoader.get_filename"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ExecutionLoader.get_filename()</span></code></a></dt><dd><p>Should only return the path to the source file; sourceless
  741. loading is not supported.</p>
  742. </dd>
  743. </dl>
  744. </li>
  745. </ul>
  746. <p>The abstract methods defined by this class are to add optional bytecode
  747. file support. Not implementing these optional methods (or causing them to
  748. raise <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>) causes the loader to
  749. only work with source code. Implementing the methods allows the loader to
  750. work with source <em>and</em> bytecode files; it does not allow for <em>sourceless</em>
  751. loading where only bytecode is provided. Bytecode files are an
  752. optimization to speed up loading by removing the parsing step of Python’s
  753. compiler, and so no bytecode-specific API is exposed.</p>
  754. <dl class="py method">
  755. <dt class="sig sig-object py" id="importlib.abc.SourceLoader.path_stats">
  756. <span class="sig-name descname"><span class="pre">path_stats</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.path_stats" title="Link to this definition">¶</a></dt>
  757. <dd><p>Optional abstract method which returns a <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> containing
  758. metadata about the specified path. Supported dictionary keys are:</p>
  759. <ul class="simple">
  760. <li><p><code class="docutils literal notranslate"><span class="pre">'mtime'</span></code> (mandatory): an integer or floating-point number
  761. representing the modification time of the source code;</p></li>
  762. <li><p><code class="docutils literal notranslate"><span class="pre">'size'</span></code> (optional): the size in bytes of the source code.</p></li>
  763. </ul>
  764. <p>Any other keys in the dictionary are ignored, to allow for future
  765. extensions. If the path cannot be handled, <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised.</p>
  766. <div class="versionadded">
  767. <p><span class="versionmodified added">New in version 3.3.</span></p>
  768. </div>
  769. <div class="versionchanged">
  770. <p><span class="versionmodified changed">Changed in version 3.4: </span>Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
  771. </div>
  772. </dd></dl>
  773. <dl class="py method">
  774. <dt class="sig sig-object py" id="importlib.abc.SourceLoader.path_mtime">
  775. <span class="sig-name descname"><span class="pre">path_mtime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.path_mtime" title="Link to this definition">¶</a></dt>
  776. <dd><p>Optional abstract method which returns the modification time for the
  777. specified path.</p>
  778. <div class="deprecated">
  779. <p><span class="versionmodified deprecated">Deprecated since version 3.3: </span>This method is deprecated in favour of <a class="reference internal" href="#importlib.abc.SourceLoader.path_stats" title="importlib.abc.SourceLoader.path_stats"><code class="xref py py-meth docutils literal notranslate"><span class="pre">path_stats()</span></code></a>. You don’t
  780. have to implement it, but it is still available for compatibility
  781. purposes. Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> if the path cannot be handled.</p>
  782. </div>
  783. <div class="versionchanged">
  784. <p><span class="versionmodified changed">Changed in version 3.4: </span>Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
  785. </div>
  786. </dd></dl>
  787. <dl class="py method">
  788. <dt class="sig sig-object py" id="importlib.abc.SourceLoader.set_data">
  789. <span class="sig-name descname"><span class="pre">set_data</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.set_data" title="Link to this definition">¶</a></dt>
  790. <dd><p>Optional abstract method which writes the specified bytes to a file
  791. path. Any intermediate directories which do not exist are to be created
  792. automatically.</p>
  793. <p>When writing to the path fails because the path is read-only
  794. (<a class="reference internal" href="errno.html#errno.EACCES" title="errno.EACCES"><code class="xref py py-const docutils literal notranslate"><span class="pre">errno.EACCES</span></code></a>/<a class="reference internal" href="exceptions.html#PermissionError" title="PermissionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PermissionError</span></code></a>), do not propagate the
  795. exception.</p>
  796. <div class="versionchanged">
  797. <p><span class="versionmodified changed">Changed in version 3.4: </span>No longer raises <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> when called.</p>
  798. </div>
  799. </dd></dl>
  800. <dl class="py method">
  801. <dt class="sig sig-object py" id="importlib.abc.SourceLoader.get_code">
  802. <span class="sig-name descname"><span class="pre">get_code</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.get_code" title="Link to this definition">¶</a></dt>
  803. <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.InspectLoader.get_code" title="importlib.abc.InspectLoader.get_code"><code class="xref py py-meth docutils literal notranslate"><span class="pre">InspectLoader.get_code()</span></code></a>.</p>
  804. </dd></dl>
  805. <dl class="py method">
  806. <dt class="sig sig-object py" id="importlib.abc.SourceLoader.exec_module">
  807. <span class="sig-name descname"><span class="pre">exec_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.exec_module" title="Link to this definition">¶</a></dt>
  808. <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Loader.exec_module()</span></code></a>.</p>
  809. <div class="versionadded">
  810. <p><span class="versionmodified added">New in version 3.4.</span></p>
  811. </div>
  812. </dd></dl>
  813. <dl class="py method">
  814. <dt class="sig sig-object py" id="importlib.abc.SourceLoader.load_module">
  815. <span class="sig-name descname"><span class="pre">load_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.load_module" title="Link to this definition">¶</a></dt>
  816. <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Loader.load_module()</span></code></a>.</p>
  817. <div class="deprecated">
  818. <p><span class="versionmodified deprecated">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.abc.SourceLoader.exec_module" title="importlib.abc.SourceLoader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> instead.</p>
  819. </div>
  820. </dd></dl>
  821. <dl class="py method">
  822. <dt class="sig sig-object py" id="importlib.abc.SourceLoader.get_source">
  823. <span class="sig-name descname"><span class="pre">get_source</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.get_source" title="Link to this definition">¶</a></dt>
  824. <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.InspectLoader.get_source" title="importlib.abc.InspectLoader.get_source"><code class="xref py py-meth docutils literal notranslate"><span class="pre">InspectLoader.get_source()</span></code></a>.</p>
  825. </dd></dl>
  826. <dl class="py method">
  827. <dt class="sig sig-object py" id="importlib.abc.SourceLoader.is_package">
  828. <span class="sig-name descname"><span class="pre">is_package</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.is_package" title="Link to this definition">¶</a></dt>
  829. <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.InspectLoader.is_package" title="importlib.abc.InspectLoader.is_package"><code class="xref py py-meth docutils literal notranslate"><span class="pre">InspectLoader.is_package()</span></code></a>. A module
  830. is determined to be a package if its file path (as provided by
  831. <a class="reference internal" href="#importlib.abc.ExecutionLoader.get_filename" title="importlib.abc.ExecutionLoader.get_filename"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ExecutionLoader.get_filename()</span></code></a>) is a file named
  832. <code class="docutils literal notranslate"><span class="pre">__init__</span></code> when the file extension is removed <strong>and</strong> the module name
  833. itself does not end in <code class="docutils literal notranslate"><span class="pre">__init__</span></code>.</p>
  834. </dd></dl>
  835. </dd></dl>
  836. <dl class="py class">
  837. <dt class="sig sig-object py" id="importlib.abc.ResourceReader">
  838. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.abc.</span></span><span class="sig-name descname"><span class="pre">ResourceReader</span></span><a class="headerlink" href="#importlib.abc.ResourceReader" title="Link to this definition">¶</a></dt>
  839. <dd><p><em>Superseded by TraversableResources</em></p>
  840. <p>An <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base class</span></a> to provide the ability to read
  841. <em>resources</em>.</p>
  842. <p>From the perspective of this ABC, a <em>resource</em> is a binary
  843. artifact that is shipped within a package. Typically this is
  844. something like a data file that lives next to the <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code>
  845. file of the package. The purpose of this class is to help abstract
  846. out the accessing of such data files so that it does not matter if
  847. the package and its data file(s) are stored in a e.g. zip file
  848. versus on the file system.</p>
  849. <p>For any of methods of this class, a <em>resource</em> argument is
  850. expected to be a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> which represents
  851. conceptually just a file name. This means that no subdirectory
  852. paths should be included in the <em>resource</em> argument. This is
  853. because the location of the package the reader is for, acts as the
  854. “directory”. Hence the metaphor for directories and file
  855. names is packages and resources, respectively. This is also why
  856. instances of this class are expected to directly correlate to
  857. a specific package (instead of potentially representing multiple
  858. packages or a module).</p>
  859. <p>Loaders that wish to support resource reading are expected to
  860. provide a method called <code class="docutils literal notranslate"><span class="pre">get_resource_reader(fullname)</span></code> which
  861. returns an object implementing this ABC’s interface. If the module
  862. specified by fullname is not a package, this method should return
  863. <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>. An object compatible with this ABC should only be
  864. returned when the specified module is a package.</p>
  865. <div class="versionadded">
  866. <p><span class="versionmodified added">New in version 3.7.</span></p>
  867. </div>
  868. <div class="deprecated-removed">
  869. <p><span class="versionmodified">Deprecated since version 3.12, will be removed in version 3.14: </span>Use <a class="reference internal" href="importlib.resources.abc.html#importlib.resources.abc.TraversableResources" title="importlib.resources.abc.TraversableResources"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.resources.abc.TraversableResources</span></code></a> instead.</p>
  870. </div>
  871. <dl class="py method">
  872. <dt class="sig sig-object py" id="importlib.abc.ResourceReader.open_resource">
  873. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">open_resource</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">resource</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.ResourceReader.open_resource" title="Link to this definition">¶</a></dt>
  874. <dd><p>Returns an opened, <a class="reference internal" href="../glossary.html#term-file-like-object"><span class="xref std std-term">file-like object</span></a> for binary reading
  875. of the <em>resource</em>.</p>
  876. <p>If the resource cannot be found, <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a> is
  877. raised.</p>
  878. </dd></dl>
  879. <dl class="py method">
  880. <dt class="sig sig-object py" id="importlib.abc.ResourceReader.resource_path">
  881. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">resource_path</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">resource</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.ResourceReader.resource_path" title="Link to this definition">¶</a></dt>
  882. <dd><p>Returns the file system path to the <em>resource</em>.</p>
  883. <p>If the resource does not concretely exist on the file system,
  884. raise <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a>.</p>
  885. </dd></dl>
  886. <dl class="py method">
  887. <dt class="sig sig-object py" id="importlib.abc.ResourceReader.is_resource">
  888. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">is_resource</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.ResourceReader.is_resource" title="Link to this definition">¶</a></dt>
  889. <dd><p>Returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if the named <em>name</em> is considered a resource.
  890. <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a> is raised if <em>name</em> does not exist.</p>
  891. </dd></dl>
  892. <dl class="py method">
  893. <dt class="sig sig-object py" id="importlib.abc.ResourceReader.contents">
  894. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">contents</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.ResourceReader.contents" title="Link to this definition">¶</a></dt>
  895. <dd><p>Returns an <a class="reference internal" href="../glossary.html#term-iterable"><span class="xref std std-term">iterable</span></a> of strings over the contents of
  896. the package. Do note that it is not required that all names
  897. returned by the iterator be actual resources, e.g. it is
  898. acceptable to return names for which <a class="reference internal" href="#importlib.abc.ResourceReader.is_resource" title="importlib.abc.ResourceReader.is_resource"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_resource()</span></code></a> would
  899. be false.</p>
  900. <p>Allowing non-resource names to be returned is to allow for
  901. situations where how a package and its resources are stored
  902. are known a priori and the non-resource names would be useful.
  903. For instance, returning subdirectory names is allowed so that
  904. when it is known that the package and resources are stored on
  905. the file system then those subdirectory names can be used
  906. directly.</p>
  907. <p>The abstract method returns an iterable of no items.</p>
  908. </dd></dl>
  909. </dd></dl>
  910. <dl class="py class">
  911. <dt class="sig sig-object py" id="importlib.abc.Traversable">
  912. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.abc.</span></span><span class="sig-name descname"><span class="pre">Traversable</span></span><a class="headerlink" href="#importlib.abc.Traversable" title="Link to this definition">¶</a></dt>
  913. <dd><p>An object with a subset of <a class="reference internal" href="pathlib.html#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">pathlib.Path</span></code></a> methods suitable for
  914. traversing directories and opening files.</p>
  915. <p>For a representation of the object on the file-system, use
  916. <a class="reference internal" href="importlib.resources.html#importlib.resources.as_file" title="importlib.resources.as_file"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.resources.as_file()</span></code></a>.</p>
  917. <div class="versionadded">
  918. <p><span class="versionmodified added">New in version 3.9.</span></p>
  919. </div>
  920. <div class="deprecated-removed">
  921. <p><span class="versionmodified">Deprecated since version 3.12, will be removed in version 3.14: </span>Use <a class="reference internal" href="importlib.resources.abc.html#importlib.resources.abc.Traversable" title="importlib.resources.abc.Traversable"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.resources.abc.Traversable</span></code></a> instead.</p>
  922. </div>
  923. <dl class="py attribute">
  924. <dt class="sig sig-object py" id="importlib.abc.Traversable.name">
  925. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#importlib.abc.Traversable.name" title="Link to this definition">¶</a></dt>
  926. <dd><p>Abstract. The base name of this object without any parent references.</p>
  927. </dd></dl>
  928. <dl class="py method">
  929. <dt class="sig sig-object py" id="importlib.abc.Traversable.iterdir">
  930. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">iterdir</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Traversable.iterdir" title="Link to this definition">¶</a></dt>
  931. <dd><p>Yield <code class="docutils literal notranslate"><span class="pre">Traversable</span></code> objects in <code class="docutils literal notranslate"><span class="pre">self</span></code>.</p>
  932. </dd></dl>
  933. <dl class="py method">
  934. <dt class="sig sig-object py" id="importlib.abc.Traversable.is_dir">
  935. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">is_dir</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Traversable.is_dir" title="Link to this definition">¶</a></dt>
  936. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if <code class="docutils literal notranslate"><span class="pre">self</span></code> is a directory.</p>
  937. </dd></dl>
  938. <dl class="py method">
  939. <dt class="sig sig-object py" id="importlib.abc.Traversable.is_file">
  940. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">is_file</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Traversable.is_file" title="Link to this definition">¶</a></dt>
  941. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if <code class="docutils literal notranslate"><span class="pre">self</span></code> is a file.</p>
  942. </dd></dl>
  943. <dl class="py method">
  944. <dt class="sig sig-object py" id="importlib.abc.Traversable.joinpath">
  945. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">joinpath</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">child</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Traversable.joinpath" title="Link to this definition">¶</a></dt>
  946. <dd><p>Return Traversable child in <code class="docutils literal notranslate"><span class="pre">self</span></code>.</p>
  947. </dd></dl>
  948. <dl class="py method">
  949. <dt class="sig sig-object py" id="importlib.abc.Traversable.__truediv__">
  950. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">__truediv__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">child</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Traversable.__truediv__" title="Link to this definition">¶</a></dt>
  951. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">Traversable</span></code> child in <code class="docutils literal notranslate"><span class="pre">self</span></code>.</p>
  952. </dd></dl>
  953. <dl class="py method">
  954. <dt class="sig sig-object py" id="importlib.abc.Traversable.open">
  955. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Traversable.open" title="Link to this definition">¶</a></dt>
  956. <dd><p><em>mode</em> may be ‘r’ or ‘rb’ to open as text or binary. Return a handle
  957. suitable for reading (same as <a class="reference internal" href="pathlib.html#pathlib.Path.open" title="pathlib.Path.open"><code class="xref py py-attr docutils literal notranslate"><span class="pre">pathlib.Path.open</span></code></a>).</p>
  958. <p>When opening as text, accepts encoding parameters such as those
  959. accepted by <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-attr docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a>.</p>
  960. </dd></dl>
  961. <dl class="py method">
  962. <dt class="sig sig-object py" id="importlib.abc.Traversable.read_bytes">
  963. <span class="sig-name descname"><span class="pre">read_bytes</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Traversable.read_bytes" title="Link to this definition">¶</a></dt>
  964. <dd><p>Read contents of <code class="docutils literal notranslate"><span class="pre">self</span></code> as bytes.</p>
  965. </dd></dl>
  966. <dl class="py method">
  967. <dt class="sig sig-object py" id="importlib.abc.Traversable.read_text">
  968. <span class="sig-name descname"><span class="pre">read_text</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">encoding</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="#importlib.abc.Traversable.read_text" title="Link to this definition">¶</a></dt>
  969. <dd><p>Read contents of <code class="docutils literal notranslate"><span class="pre">self</span></code> as text.</p>
  970. </dd></dl>
  971. </dd></dl>
  972. <dl class="py class">
  973. <dt class="sig sig-object py" id="importlib.abc.TraversableResources">
  974. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.abc.</span></span><span class="sig-name descname"><span class="pre">TraversableResources</span></span><a class="headerlink" href="#importlib.abc.TraversableResources" title="Link to this definition">¶</a></dt>
  975. <dd><p>An abstract base class for resource readers capable of serving
  976. the <a class="reference internal" href="importlib.resources.html#importlib.resources.files" title="importlib.resources.files"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.resources.files()</span></code></a> interface. Subclasses
  977. <a class="reference internal" href="importlib.resources.abc.html#importlib.resources.abc.ResourceReader" title="importlib.resources.abc.ResourceReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.resources.abc.ResourceReader</span></code></a> and provides
  978. concrete implementations of the <a class="reference internal" href="importlib.resources.abc.html#importlib.resources.abc.ResourceReader" title="importlib.resources.abc.ResourceReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.resources.abc.ResourceReader</span></code></a>’s
  979. abstract methods. Therefore, any loader supplying
  980. <a class="reference internal" href="#importlib.abc.TraversableResources" title="importlib.abc.TraversableResources"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.TraversableResources</span></code></a> also supplies ResourceReader.</p>
  981. <p>Loaders that wish to support resource reading are expected to
  982. implement this interface.</p>
  983. <div class="versionadded">
  984. <p><span class="versionmodified added">New in version 3.9.</span></p>
  985. </div>
  986. <div class="deprecated-removed">
  987. <p><span class="versionmodified">Deprecated since version 3.12, will be removed in version 3.14: </span>Use <a class="reference internal" href="importlib.resources.abc.html#importlib.resources.abc.TraversableResources" title="importlib.resources.abc.TraversableResources"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.resources.abc.TraversableResources</span></code></a> instead.</p>
  988. </div>
  989. <dl class="py method">
  990. <dt class="sig sig-object py" id="importlib.abc.TraversableResources.files">
  991. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">files</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.TraversableResources.files" title="Link to this definition">¶</a></dt>
  992. <dd><p>Returns a <a class="reference internal" href="importlib.resources.abc.html#importlib.resources.abc.Traversable" title="importlib.resources.abc.Traversable"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.resources.abc.Traversable</span></code></a> object for the loaded
  993. package.</p>
  994. </dd></dl>
  995. </dd></dl>
  996. </section>
  997. <section id="module-importlib.machinery">
  998. <span id="importlib-machinery-importers-and-path-hooks"></span><h2><a class="reference internal" href="#module-importlib.machinery" title="importlib.machinery: Importers and path hooks"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.machinery</span></code></a> – Importers and path hooks<a class="headerlink" href="#module-importlib.machinery" title="Link to this heading">¶</a></h2>
  999. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/importlib/machinery.py">Lib/importlib/machinery.py</a></p>
  1000. <hr class="docutils" />
  1001. <p>This module contains the various objects that help <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a>
  1002. find and load modules.</p>
  1003. <dl class="py attribute">
  1004. <dt class="sig sig-object py" id="importlib.machinery.SOURCE_SUFFIXES">
  1005. <span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">SOURCE_SUFFIXES</span></span><a class="headerlink" href="#importlib.machinery.SOURCE_SUFFIXES" title="Link to this definition">¶</a></dt>
  1006. <dd><p>A list of strings representing the recognized file suffixes for source
  1007. modules.</p>
  1008. <div class="versionadded">
  1009. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1010. </div>
  1011. </dd></dl>
  1012. <dl class="py attribute">
  1013. <dt class="sig sig-object py" id="importlib.machinery.DEBUG_BYTECODE_SUFFIXES">
  1014. <span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">DEBUG_BYTECODE_SUFFIXES</span></span><a class="headerlink" href="#importlib.machinery.DEBUG_BYTECODE_SUFFIXES" title="Link to this definition">¶</a></dt>
  1015. <dd><p>A list of strings representing the file suffixes for non-optimized bytecode
  1016. modules.</p>
  1017. <div class="versionadded">
  1018. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1019. </div>
  1020. <div class="deprecated">
  1021. <p><span class="versionmodified deprecated">Deprecated since version 3.5: </span>Use <a class="reference internal" href="#importlib.machinery.BYTECODE_SUFFIXES" title="importlib.machinery.BYTECODE_SUFFIXES"><code class="xref py py-attr docutils literal notranslate"><span class="pre">BYTECODE_SUFFIXES</span></code></a> instead.</p>
  1022. </div>
  1023. </dd></dl>
  1024. <dl class="py attribute">
  1025. <dt class="sig sig-object py" id="importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES">
  1026. <span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">OPTIMIZED_BYTECODE_SUFFIXES</span></span><a class="headerlink" href="#importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES" title="Link to this definition">¶</a></dt>
  1027. <dd><p>A list of strings representing the file suffixes for optimized bytecode
  1028. modules.</p>
  1029. <div class="versionadded">
  1030. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1031. </div>
  1032. <div class="deprecated">
  1033. <p><span class="versionmodified deprecated">Deprecated since version 3.5: </span>Use <a class="reference internal" href="#importlib.machinery.BYTECODE_SUFFIXES" title="importlib.machinery.BYTECODE_SUFFIXES"><code class="xref py py-attr docutils literal notranslate"><span class="pre">BYTECODE_SUFFIXES</span></code></a> instead.</p>
  1034. </div>
  1035. </dd></dl>
  1036. <dl class="py attribute">
  1037. <dt class="sig sig-object py" id="importlib.machinery.BYTECODE_SUFFIXES">
  1038. <span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">BYTECODE_SUFFIXES</span></span><a class="headerlink" href="#importlib.machinery.BYTECODE_SUFFIXES" title="Link to this definition">¶</a></dt>
  1039. <dd><p>A list of strings representing the recognized file suffixes for bytecode
  1040. modules (including the leading dot).</p>
  1041. <div class="versionadded">
  1042. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1043. </div>
  1044. <div class="versionchanged">
  1045. <p><span class="versionmodified changed">Changed in version 3.5: </span>The value is no longer dependent on <code class="docutils literal notranslate"><span class="pre">__debug__</span></code>.</p>
  1046. </div>
  1047. </dd></dl>
  1048. <dl class="py attribute">
  1049. <dt class="sig sig-object py" id="importlib.machinery.EXTENSION_SUFFIXES">
  1050. <span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">EXTENSION_SUFFIXES</span></span><a class="headerlink" href="#importlib.machinery.EXTENSION_SUFFIXES" title="Link to this definition">¶</a></dt>
  1051. <dd><p>A list of strings representing the recognized file suffixes for
  1052. extension modules.</p>
  1053. <div class="versionadded">
  1054. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1055. </div>
  1056. </dd></dl>
  1057. <dl class="py function">
  1058. <dt class="sig sig-object py" id="importlib.machinery.all_suffixes">
  1059. <span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">all_suffixes</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.all_suffixes" title="Link to this definition">¶</a></dt>
  1060. <dd><p>Returns a combined list of strings representing all file suffixes for
  1061. modules recognized by the standard import machinery. This is a
  1062. helper for code which simply needs to know if a filesystem path
  1063. potentially refers to a module without needing any details on the kind
  1064. of module (for example, <a class="reference internal" href="inspect.html#inspect.getmodulename" title="inspect.getmodulename"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.getmodulename()</span></code></a>).</p>
  1065. <div class="versionadded">
  1066. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1067. </div>
  1068. </dd></dl>
  1069. <dl class="py class">
  1070. <dt class="sig sig-object py" id="importlib.machinery.BuiltinImporter">
  1071. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">BuiltinImporter</span></span><a class="headerlink" href="#importlib.machinery.BuiltinImporter" title="Link to this definition">¶</a></dt>
  1072. <dd><p>An <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a> for built-in modules. All known built-in modules are
  1073. listed in <a class="reference internal" href="sys.html#sys.builtin_module_names" title="sys.builtin_module_names"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.builtin_module_names</span></code></a>. This class implements the
  1074. <a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder</span></code></a> and
  1075. <a class="reference internal" href="#importlib.abc.InspectLoader" title="importlib.abc.InspectLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.InspectLoader</span></code></a> ABCs.</p>
  1076. <p>Only class methods are defined by this class to alleviate the need for
  1077. instantiation.</p>
  1078. <div class="versionchanged">
  1079. <p><span class="versionmodified changed">Changed in version 3.5: </span>As part of <span class="target" id="index-17"></span><a class="pep reference external" href="https://peps.python.org/pep-0489/"><strong>PEP 489</strong></a>, the builtin importer now implements
  1080. <code class="xref py py-meth docutils literal notranslate"><span class="pre">Loader.create_module()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">Loader.exec_module()</span></code></p>
  1081. </div>
  1082. </dd></dl>
  1083. <dl class="py class">
  1084. <dt class="sig sig-object py" id="importlib.machinery.FrozenImporter">
  1085. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">FrozenImporter</span></span><a class="headerlink" href="#importlib.machinery.FrozenImporter" title="Link to this definition">¶</a></dt>
  1086. <dd><p>An <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a> for frozen modules. This class implements the
  1087. <a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder</span></code></a> and
  1088. <a class="reference internal" href="#importlib.abc.InspectLoader" title="importlib.abc.InspectLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.InspectLoader</span></code></a> ABCs.</p>
  1089. <p>Only class methods are defined by this class to alleviate the need for
  1090. instantiation.</p>
  1091. <div class="versionchanged">
  1092. <p><span class="versionmodified changed">Changed in version 3.4: </span>Gained <code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code>
  1093. methods.</p>
  1094. </div>
  1095. </dd></dl>
  1096. <dl class="py class">
  1097. <dt class="sig sig-object py" id="importlib.machinery.WindowsRegistryFinder">
  1098. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">WindowsRegistryFinder</span></span><a class="headerlink" href="#importlib.machinery.WindowsRegistryFinder" title="Link to this definition">¶</a></dt>
  1099. <dd><p><a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">Finder</span></a> for modules declared in the Windows registry. This class
  1100. implements the <a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder</span></code></a> ABC.</p>
  1101. <p>Only class methods are defined by this class to alleviate the need for
  1102. instantiation.</p>
  1103. <div class="versionadded">
  1104. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1105. </div>
  1106. <div class="deprecated">
  1107. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span>Use <a class="reference internal" href="site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> configuration instead. Future versions of Python may
  1108. not enable this finder by default.</p>
  1109. </div>
  1110. </dd></dl>
  1111. <dl class="py class">
  1112. <dt class="sig sig-object py" id="importlib.machinery.PathFinder">
  1113. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">PathFinder</span></span><a class="headerlink" href="#importlib.machinery.PathFinder" title="Link to this definition">¶</a></dt>
  1114. <dd><p>A <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">Finder</span></a> for <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> and package <code class="docutils literal notranslate"><span class="pre">__path__</span></code> attributes.
  1115. This class implements the <a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder</span></code></a> ABC.</p>
  1116. <p>Only class methods are defined by this class to alleviate the need for
  1117. instantiation.</p>
  1118. <dl class="py method">
  1119. <dt class="sig sig-object py" id="importlib.machinery.PathFinder.find_spec">
  1120. <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">find_spec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</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">target</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="#importlib.machinery.PathFinder.find_spec" title="Link to this definition">¶</a></dt>
  1121. <dd><p>Class method that attempts to find a <a class="reference internal" href="../glossary.html#term-module-spec"><span class="xref std std-term">spec</span></a>
  1122. for the module specified by <em>fullname</em> on <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> or, if
  1123. defined, on <em>path</em>. For each path entry that is searched,
  1124. <a class="reference internal" href="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> is checked. If a non-false object
  1125. is found then it is used as the <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a> to look
  1126. for the module being searched for. If no entry is found in
  1127. <a class="reference internal" href="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>, then <a class="reference internal" href="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> is
  1128. searched for a finder for the path entry and, if found, is stored
  1129. in <a class="reference internal" href="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> along with being queried about
  1130. the module. If no finder is ever found then <code class="docutils literal notranslate"><span class="pre">None</span></code> is both
  1131. stored in the cache and returned.</p>
  1132. <div class="versionadded">
  1133. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1134. </div>
  1135. <div class="versionchanged">
  1136. <p><span class="versionmodified changed">Changed in version 3.5: </span>If the current working directory – represented by an empty string –
  1137. is no longer valid then <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned but no value is cached
  1138. in <a class="reference internal" href="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>.</p>
  1139. </div>
  1140. </dd></dl>
  1141. <dl class="py method">
  1142. <dt class="sig sig-object py" id="importlib.machinery.PathFinder.invalidate_caches">
  1143. <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">invalidate_caches</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.PathFinder.invalidate_caches" title="Link to this definition">¶</a></dt>
  1144. <dd><p>Calls <a class="reference internal" href="#importlib.abc.PathEntryFinder.invalidate_caches" title="importlib.abc.PathEntryFinder.invalidate_caches"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.PathEntryFinder.invalidate_caches()</span></code></a> on all
  1145. finders stored in <a class="reference internal" href="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> that define the method.
  1146. Otherwise entries in <a class="reference internal" href="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> set to <code class="docutils literal notranslate"><span class="pre">None</span></code> are
  1147. deleted.</p>
  1148. <div class="versionchanged">
  1149. <p><span class="versionmodified changed">Changed in version 3.7: </span>Entries of <code class="docutils literal notranslate"><span class="pre">None</span></code> in <a class="reference internal" href="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> are deleted.</p>
  1150. </div>
  1151. </dd></dl>
  1152. <div class="versionchanged">
  1153. <p><span class="versionmodified changed">Changed in version 3.4: </span>Calls objects in <a class="reference internal" href="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> with the current working
  1154. directory for <code class="docutils literal notranslate"><span class="pre">''</span></code> (i.e. the empty string).</p>
  1155. </div>
  1156. </dd></dl>
  1157. <dl class="py class">
  1158. <dt class="sig sig-object py" id="importlib.machinery.FileFinder">
  1159. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">FileFinder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">loader_details</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.FileFinder" title="Link to this definition">¶</a></dt>
  1160. <dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.PathEntryFinder" title="importlib.abc.PathEntryFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.PathEntryFinder</span></code></a> which
  1161. caches results from the file system.</p>
  1162. <p>The <em>path</em> argument is the directory for which the finder is in charge of
  1163. searching.</p>
  1164. <p>The <em>loader_details</em> argument is a variable number of 2-item tuples each
  1165. containing a loader and a sequence of file suffixes the loader recognizes.
  1166. The loaders are expected to be callables which accept two arguments of
  1167. the module’s name and the path to the file found.</p>
  1168. <p>The finder will cache the directory contents as necessary, making stat calls
  1169. for each module search to verify the cache is not outdated. Because cache
  1170. staleness relies upon the granularity of the operating system’s state
  1171. information of the file system, there is a potential race condition of
  1172. searching for a module, creating a new file, and then searching for the
  1173. module the new file represents. If the operations happen fast enough to fit
  1174. within the granularity of stat calls, then the module search will fail. To
  1175. prevent this from happening, when you create a module dynamically, make sure
  1176. to call <a class="reference internal" href="#importlib.invalidate_caches" title="importlib.invalidate_caches"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.invalidate_caches()</span></code></a>.</p>
  1177. <div class="versionadded">
  1178. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1179. </div>
  1180. <dl class="py attribute">
  1181. <dt class="sig sig-object py" id="importlib.machinery.FileFinder.path">
  1182. <span class="sig-name descname"><span class="pre">path</span></span><a class="headerlink" href="#importlib.machinery.FileFinder.path" title="Link to this definition">¶</a></dt>
  1183. <dd><p>The path the finder will search in.</p>
  1184. </dd></dl>
  1185. <dl class="py method">
  1186. <dt class="sig sig-object py" id="importlib.machinery.FileFinder.find_spec">
  1187. <span class="sig-name descname"><span class="pre">find_spec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">target</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="#importlib.machinery.FileFinder.find_spec" title="Link to this definition">¶</a></dt>
  1188. <dd><p>Attempt to find the spec to handle <em>fullname</em> within <a class="reference internal" href="#importlib.machinery.FileFinder.path" title="importlib.machinery.FileFinder.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a>.</p>
  1189. <div class="versionadded">
  1190. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1191. </div>
  1192. </dd></dl>
  1193. <dl class="py method">
  1194. <dt class="sig sig-object py" id="importlib.machinery.FileFinder.invalidate_caches">
  1195. <span class="sig-name descname"><span class="pre">invalidate_caches</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.FileFinder.invalidate_caches" title="Link to this definition">¶</a></dt>
  1196. <dd><p>Clear out the internal cache.</p>
  1197. </dd></dl>
  1198. <dl class="py method">
  1199. <dt class="sig sig-object py" id="importlib.machinery.FileFinder.path_hook">
  1200. <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">path_hook</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">loader_details</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.FileFinder.path_hook" title="Link to this definition">¶</a></dt>
  1201. <dd><p>A class method which returns a closure for use on <a class="reference internal" href="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>.
  1202. An instance of <a class="reference internal" href="#importlib.machinery.FileFinder" title="importlib.machinery.FileFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileFinder</span></code></a> is returned by the closure using the
  1203. path argument given to the closure directly and <em>loader_details</em>
  1204. indirectly.</p>
  1205. <p>If the argument to the closure is not an existing directory,
  1206. <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> is raised.</p>
  1207. </dd></dl>
  1208. </dd></dl>
  1209. <dl class="py class">
  1210. <dt class="sig sig-object py" id="importlib.machinery.SourceFileLoader">
  1211. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">SourceFileLoader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourceFileLoader" title="Link to this definition">¶</a></dt>
  1212. <dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.SourceLoader" title="importlib.abc.SourceLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.SourceLoader</span></code></a> by
  1213. subclassing <a class="reference internal" href="#importlib.abc.FileLoader" title="importlib.abc.FileLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.FileLoader</span></code></a> and providing some concrete
  1214. implementations of other methods.</p>
  1215. <div class="versionadded">
  1216. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1217. </div>
  1218. <dl class="py attribute">
  1219. <dt class="sig sig-object py" id="importlib.machinery.SourceFileLoader.name">
  1220. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#importlib.machinery.SourceFileLoader.name" title="Link to this definition">¶</a></dt>
  1221. <dd><p>The name of the module that this loader will handle.</p>
  1222. </dd></dl>
  1223. <dl class="py attribute">
  1224. <dt class="sig sig-object py" id="importlib.machinery.SourceFileLoader.path">
  1225. <span class="sig-name descname"><span class="pre">path</span></span><a class="headerlink" href="#importlib.machinery.SourceFileLoader.path" title="Link to this definition">¶</a></dt>
  1226. <dd><p>The path to the source file.</p>
  1227. </dd></dl>
  1228. <dl class="py method">
  1229. <dt class="sig sig-object py" id="importlib.machinery.SourceFileLoader.is_package">
  1230. <span class="sig-name descname"><span class="pre">is_package</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourceFileLoader.is_package" title="Link to this definition">¶</a></dt>
  1231. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if <a class="reference internal" href="#importlib.machinery.SourceFileLoader.path" title="importlib.machinery.SourceFileLoader.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a> appears to be for a package.</p>
  1232. </dd></dl>
  1233. <dl class="py method">
  1234. <dt class="sig sig-object py" id="importlib.machinery.SourceFileLoader.path_stats">
  1235. <span class="sig-name descname"><span class="pre">path_stats</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourceFileLoader.path_stats" title="Link to this definition">¶</a></dt>
  1236. <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.SourceLoader.path_stats" title="importlib.abc.SourceLoader.path_stats"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.SourceLoader.path_stats()</span></code></a>.</p>
  1237. </dd></dl>
  1238. <dl class="py method">
  1239. <dt class="sig sig-object py" id="importlib.machinery.SourceFileLoader.set_data">
  1240. <span class="sig-name descname"><span class="pre">set_data</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourceFileLoader.set_data" title="Link to this definition">¶</a></dt>
  1241. <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.SourceLoader.set_data" title="importlib.abc.SourceLoader.set_data"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.SourceLoader.set_data()</span></code></a>.</p>
  1242. </dd></dl>
  1243. <dl class="py method">
  1244. <dt class="sig sig-object py" id="importlib.machinery.SourceFileLoader.load_module">
  1245. <span class="sig-name descname"><span class="pre">load_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">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="#importlib.machinery.SourceFileLoader.load_module" title="Link to this definition">¶</a></dt>
  1246. <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.load_module()</span></code></a> where
  1247. specifying the name of the module to load is optional.</p>
  1248. <div class="deprecated">
  1249. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span>Use <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.exec_module()</span></code></a> instead.</p>
  1250. </div>
  1251. </dd></dl>
  1252. </dd></dl>
  1253. <dl class="py class">
  1254. <dt class="sig sig-object py" id="importlib.machinery.SourcelessFileLoader">
  1255. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">SourcelessFileLoader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader" title="Link to this definition">¶</a></dt>
  1256. <dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.FileLoader" title="importlib.abc.FileLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.FileLoader</span></code></a> which can
  1257. import bytecode files (i.e. no source code files exist).</p>
  1258. <p>Please note that direct use of bytecode files (and thus not source code
  1259. files) inhibits your modules from being usable by all Python
  1260. implementations or new versions of Python which change the bytecode
  1261. format.</p>
  1262. <div class="versionadded">
  1263. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1264. </div>
  1265. <dl class="py attribute">
  1266. <dt class="sig sig-object py" id="importlib.machinery.SourcelessFileLoader.name">
  1267. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.name" title="Link to this definition">¶</a></dt>
  1268. <dd><p>The name of the module the loader will handle.</p>
  1269. </dd></dl>
  1270. <dl class="py attribute">
  1271. <dt class="sig sig-object py" id="importlib.machinery.SourcelessFileLoader.path">
  1272. <span class="sig-name descname"><span class="pre">path</span></span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.path" title="Link to this definition">¶</a></dt>
  1273. <dd><p>The path to the bytecode file.</p>
  1274. </dd></dl>
  1275. <dl class="py method">
  1276. <dt class="sig sig-object py" id="importlib.machinery.SourcelessFileLoader.is_package">
  1277. <span class="sig-name descname"><span class="pre">is_package</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.is_package" title="Link to this definition">¶</a></dt>
  1278. <dd><p>Determines if the module is a package based on <a class="reference internal" href="#importlib.machinery.SourcelessFileLoader.path" title="importlib.machinery.SourcelessFileLoader.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a>.</p>
  1279. </dd></dl>
  1280. <dl class="py method">
  1281. <dt class="sig sig-object py" id="importlib.machinery.SourcelessFileLoader.get_code">
  1282. <span class="sig-name descname"><span class="pre">get_code</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.get_code" title="Link to this definition">¶</a></dt>
  1283. <dd><p>Returns the code object for <a class="reference internal" href="#importlib.machinery.SourcelessFileLoader.name" title="importlib.machinery.SourcelessFileLoader.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a> created from <a class="reference internal" href="#importlib.machinery.SourcelessFileLoader.path" title="importlib.machinery.SourcelessFileLoader.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a>.</p>
  1284. </dd></dl>
  1285. <dl class="py method">
  1286. <dt class="sig sig-object py" id="importlib.machinery.SourcelessFileLoader.get_source">
  1287. <span class="sig-name descname"><span class="pre">get_source</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.get_source" title="Link to this definition">¶</a></dt>
  1288. <dd><p>Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> as bytecode files have no source when this loader is
  1289. used.</p>
  1290. </dd></dl>
  1291. <dl class="py method">
  1292. <dt class="sig sig-object py" id="importlib.machinery.SourcelessFileLoader.load_module">
  1293. <span class="sig-name descname"><span class="pre">load_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">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="#importlib.machinery.SourcelessFileLoader.load_module" title="Link to this definition">¶</a></dt>
  1294. <dd></dd></dl>
  1295. <p>Concrete implementation of <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.load_module()</span></code></a> where
  1296. specifying the name of the module to load is optional.</p>
  1297. <div class="deprecated">
  1298. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span>Use <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.exec_module()</span></code></a> instead.</p>
  1299. </div>
  1300. </dd></dl>
  1301. <dl class="py class">
  1302. <dt class="sig sig-object py" id="importlib.machinery.ExtensionFileLoader">
  1303. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">ExtensionFileLoader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader" title="Link to this definition">¶</a></dt>
  1304. <dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.ExecutionLoader" title="importlib.abc.ExecutionLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.ExecutionLoader</span></code></a> for
  1305. extension modules.</p>
  1306. <p>The <em>fullname</em> argument specifies the name of the module the loader is to
  1307. support. The <em>path</em> argument is the path to the extension module’s file.</p>
  1308. <p>Note that, by default, importing an extension module will fail
  1309. in subinterpreters if it doesn’t implement multi-phase init
  1310. (see <span class="target" id="index-18"></span><a class="pep reference external" href="https://peps.python.org/pep-0489/"><strong>PEP 489</strong></a>), even if it would otherwise import successfully.</p>
  1311. <div class="versionadded">
  1312. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1313. </div>
  1314. <div class="versionchanged">
  1315. <p><span class="versionmodified changed">Changed in version 3.12: </span>Multi-phase init is now required for use in subinterpreters.</p>
  1316. </div>
  1317. <dl class="py attribute">
  1318. <dt class="sig sig-object py" id="importlib.machinery.ExtensionFileLoader.name">
  1319. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.name" title="Link to this definition">¶</a></dt>
  1320. <dd><p>Name of the module the loader supports.</p>
  1321. </dd></dl>
  1322. <dl class="py attribute">
  1323. <dt class="sig sig-object py" id="importlib.machinery.ExtensionFileLoader.path">
  1324. <span class="sig-name descname"><span class="pre">path</span></span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.path" title="Link to this definition">¶</a></dt>
  1325. <dd><p>Path to the extension module.</p>
  1326. </dd></dl>
  1327. <dl class="py method">
  1328. <dt class="sig sig-object py" id="importlib.machinery.ExtensionFileLoader.create_module">
  1329. <span class="sig-name descname"><span class="pre">create_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">spec</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.create_module" title="Link to this definition">¶</a></dt>
  1330. <dd><p>Creates the module object from the given specification in accordance
  1331. with <span class="target" id="index-19"></span><a class="pep reference external" href="https://peps.python.org/pep-0489/"><strong>PEP 489</strong></a>.</p>
  1332. <div class="versionadded">
  1333. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1334. </div>
  1335. </dd></dl>
  1336. <dl class="py method">
  1337. <dt class="sig sig-object py" id="importlib.machinery.ExtensionFileLoader.exec_module">
  1338. <span class="sig-name descname"><span class="pre">exec_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.exec_module" title="Link to this definition">¶</a></dt>
  1339. <dd><p>Initializes the given module object in accordance with <span class="target" id="index-20"></span><a class="pep reference external" href="https://peps.python.org/pep-0489/"><strong>PEP 489</strong></a>.</p>
  1340. <div class="versionadded">
  1341. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1342. </div>
  1343. </dd></dl>
  1344. <dl class="py method">
  1345. <dt class="sig sig-object py" id="importlib.machinery.ExtensionFileLoader.is_package">
  1346. <span class="sig-name descname"><span class="pre">is_package</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.is_package" title="Link to this definition">¶</a></dt>
  1347. <dd><p>Returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if the file path points to a package’s <code class="docutils literal notranslate"><span class="pre">__init__</span></code>
  1348. module based on <a class="reference internal" href="#importlib.machinery.EXTENSION_SUFFIXES" title="importlib.machinery.EXTENSION_SUFFIXES"><code class="xref py py-attr docutils literal notranslate"><span class="pre">EXTENSION_SUFFIXES</span></code></a>.</p>
  1349. </dd></dl>
  1350. <dl class="py method">
  1351. <dt class="sig sig-object py" id="importlib.machinery.ExtensionFileLoader.get_code">
  1352. <span class="sig-name descname"><span class="pre">get_code</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.get_code" title="Link to this definition">¶</a></dt>
  1353. <dd><p>Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> as extension modules lack a code object.</p>
  1354. </dd></dl>
  1355. <dl class="py method">
  1356. <dt class="sig sig-object py" id="importlib.machinery.ExtensionFileLoader.get_source">
  1357. <span class="sig-name descname"><span class="pre">get_source</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.get_source" title="Link to this definition">¶</a></dt>
  1358. <dd><p>Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> as extension modules do not have source code.</p>
  1359. </dd></dl>
  1360. <dl class="py method">
  1361. <dt class="sig sig-object py" id="importlib.machinery.ExtensionFileLoader.get_filename">
  1362. <span class="sig-name descname"><span class="pre">get_filename</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.get_filename" title="Link to this definition">¶</a></dt>
  1363. <dd><p>Returns <a class="reference internal" href="#importlib.machinery.ExtensionFileLoader.path" title="importlib.machinery.ExtensionFileLoader.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a>.</p>
  1364. <div class="versionadded">
  1365. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1366. </div>
  1367. </dd></dl>
  1368. </dd></dl>
  1369. <dl class="py class">
  1370. <dt class="sig sig-object py" id="importlib.machinery.NamespaceLoader">
  1371. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">NamespaceLoader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path_finder</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.NamespaceLoader" title="Link to this definition">¶</a></dt>
  1372. <dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.InspectLoader" title="importlib.abc.InspectLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.InspectLoader</span></code></a> for
  1373. namespace packages. This is an alias for a private class and is only made
  1374. public for introspecting the <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> attribute on namespace
  1375. packages:</p>
  1376. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">importlib.machinery</span> <span class="kn">import</span> <span class="n">NamespaceLoader</span>
  1377. <span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">my_namespace</span>
  1378. <span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">my_namespace</span><span class="o">.</span><span class="n">__loader__</span><span class="p">,</span> <span class="n">NamespaceLoader</span><span class="p">)</span>
  1379. <span class="go">True</span>
  1380. <span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">importlib.abc</span>
  1381. <span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">my_namespace</span><span class="o">.</span><span class="n">__loader__</span><span class="p">,</span> <span class="n">importlib</span><span class="o">.</span><span class="n">abc</span><span class="o">.</span><span class="n">Loader</span><span class="p">)</span>
  1382. <span class="go">True</span>
  1383. </pre></div>
  1384. </div>
  1385. <div class="versionadded">
  1386. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1387. </div>
  1388. </dd></dl>
  1389. <dl class="py class">
  1390. <dt class="sig sig-object py" id="importlib.machinery.ModuleSpec">
  1391. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.machinery.</span></span><span class="sig-name descname"><span class="pre">ModuleSpec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">loader</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">origin</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">loader_state</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">is_package</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="#importlib.machinery.ModuleSpec" title="Link to this definition">¶</a></dt>
  1392. <dd><p>A specification for a module’s import-system-related state. This is
  1393. typically exposed as 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> attribute. In the
  1394. descriptions below, the names in parentheses give the corresponding
  1395. attribute available directly on the module object,
  1396. e.g. <code class="docutils literal notranslate"><span class="pre">module.__spec__.origin</span> <span class="pre">==</span> <span class="pre">module.__file__</span></code>. Note, however, that
  1397. while the <em>values</em> are usually equivalent, they can differ since there is
  1398. no synchronization between the two objects. For example, it is possible to update
  1399. 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> at runtime and this will not be automatically
  1400. reflected in the module’s <code class="xref py py-attr docutils literal notranslate"><span class="pre">__spec__.origin</span></code>, and vice versa.</p>
  1401. <div class="versionadded">
  1402. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1403. </div>
  1404. <dl class="py attribute">
  1405. <dt class="sig sig-object py" id="importlib.machinery.ModuleSpec.name">
  1406. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#importlib.machinery.ModuleSpec.name" title="Link to this definition">¶</a></dt>
  1407. <dd></dd></dl>
  1408. <p>(<a class="reference internal" href="../reference/import.html#name__" title="__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a>)</p>
  1409. <p>The module’s fully qualified name.
  1410. The <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finder</span></a> should always set this attribute to a non-empty string.</p>
  1411. <dl class="py attribute">
  1412. <dt class="sig sig-object py" id="importlib.machinery.ModuleSpec.loader">
  1413. <span class="sig-name descname"><span class="pre">loader</span></span><a class="headerlink" href="#importlib.machinery.ModuleSpec.loader" title="Link to this definition">¶</a></dt>
  1414. <dd></dd></dl>
  1415. <p>(<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>)</p>
  1416. <p>The <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> used to load the module.
  1417. The <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finder</span></a> should always set this attribute.</p>
  1418. <dl class="py attribute">
  1419. <dt class="sig sig-object py" id="importlib.machinery.ModuleSpec.origin">
  1420. <span class="sig-name descname"><span class="pre">origin</span></span><a class="headerlink" href="#importlib.machinery.ModuleSpec.origin" title="Link to this definition">¶</a></dt>
  1421. <dd></dd></dl>
  1422. <p>(<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>)</p>
  1423. <p>The location the <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> should use to load the module.
  1424. For example, for modules loaded from a .py file this is the filename.
  1425. The <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finder</span></a> should always set this attribute to a meaningful value
  1426. for the <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> to use. In the uncommon case that there is not one
  1427. (like for namespace packages), it should be set to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1428. <dl class="py attribute">
  1429. <dt class="sig sig-object py" id="importlib.machinery.ModuleSpec.submodule_search_locations">
  1430. <span class="sig-name descname"><span class="pre">submodule_search_locations</span></span><a class="headerlink" href="#importlib.machinery.ModuleSpec.submodule_search_locations" title="Link to this definition">¶</a></dt>
  1431. <dd></dd></dl>
  1432. <p>(<a class="reference internal" href="../reference/import.html#path__" title="__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__path__</span></code></a>)</p>
  1433. <p>The list of locations where the package’s submodules will be found.
  1434. Most of the time this is a single directory.
  1435. The <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finder</span></a> should set this attribute to a list, even an empty one, to indicate
  1436. to the import system that the module is a package. It should be set to <code class="docutils literal notranslate"><span class="pre">None</span></code> for
  1437. non-package modules. It is set automatically later to a special object for
  1438. namespace packages.</p>
  1439. <dl class="py attribute">
  1440. <dt class="sig sig-object py" id="importlib.machinery.ModuleSpec.loader_state">
  1441. <span class="sig-name descname"><span class="pre">loader_state</span></span><a class="headerlink" href="#importlib.machinery.ModuleSpec.loader_state" title="Link to this definition">¶</a></dt>
  1442. <dd></dd></dl>
  1443. <p>The <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finder</span></a> may set this attribute to an object containing additional,
  1444. module-specific data to use when loading the module. Otherwise it should be
  1445. set to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1446. <dl class="py attribute">
  1447. <dt class="sig sig-object py" id="importlib.machinery.ModuleSpec.cached">
  1448. <span class="sig-name descname"><span class="pre">cached</span></span><a class="headerlink" href="#importlib.machinery.ModuleSpec.cached" title="Link to this definition">¶</a></dt>
  1449. <dd></dd></dl>
  1450. <p>(<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>)</p>
  1451. <p>The filename of a compiled version of the module’s code.
  1452. The <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finder</span></a> should always set this attribute but it may be <code class="docutils literal notranslate"><span class="pre">None</span></code>
  1453. for modules that do not need compiled code stored.</p>
  1454. <dl class="py attribute">
  1455. <dt class="sig sig-object py" id="importlib.machinery.ModuleSpec.parent">
  1456. <span class="sig-name descname"><span class="pre">parent</span></span><a class="headerlink" href="#importlib.machinery.ModuleSpec.parent" title="Link to this definition">¶</a></dt>
  1457. <dd></dd></dl>
  1458. <p>(<a class="reference internal" href="../reference/import.html#package__" title="__package__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__package__</span></code></a>)</p>
  1459. <p>(Read-only) The fully qualified name of the package the module is in (or the
  1460. empty string for a top-level module).
  1461. If the module is a package then this is the same as <a class="reference internal" href="#importlib.machinery.ModuleSpec.name" title="importlib.machinery.ModuleSpec.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a>.</p>
  1462. <dl class="py attribute">
  1463. <dt class="sig sig-object py" id="importlib.machinery.ModuleSpec.has_location">
  1464. <span class="sig-name descname"><span class="pre">has_location</span></span><a class="headerlink" href="#importlib.machinery.ModuleSpec.has_location" title="Link to this definition">¶</a></dt>
  1465. <dd></dd></dl>
  1466. <dl class="simple">
  1467. <dt><code class="docutils literal notranslate"><span class="pre">True</span></code> if the spec’s <a class="reference internal" href="#importlib.machinery.ModuleSpec.origin" title="importlib.machinery.ModuleSpec.origin"><code class="xref py py-attr docutils literal notranslate"><span class="pre">origin</span></code></a> refers to a loadable location,</dt><dd><p><code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise. This value impacts how <a class="reference internal" href="#importlib.machinery.ModuleSpec.origin" title="importlib.machinery.ModuleSpec.origin"><code class="xref py py-attr docutils literal notranslate"><span class="pre">origin</span></code></a> is interpreted
  1468. and how 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> is populated.</p>
  1469. </dd>
  1470. </dl>
  1471. </dd></dl>
  1472. </section>
  1473. <section id="module-importlib.util">
  1474. <span id="importlib-util-utility-code-for-importers"></span><h2><a class="reference internal" href="#module-importlib.util" title="importlib.util: Utility code for importers"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.util</span></code></a> – Utility code for importers<a class="headerlink" href="#module-importlib.util" title="Link to this heading">¶</a></h2>
  1475. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/importlib/util.py">Lib/importlib/util.py</a></p>
  1476. <hr class="docutils" />
  1477. <p>This module contains the various objects that help in the construction of
  1478. an <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a>.</p>
  1479. <dl class="py attribute">
  1480. <dt class="sig sig-object py" id="importlib.util.MAGIC_NUMBER">
  1481. <span class="sig-prename descclassname"><span class="pre">importlib.util.</span></span><span class="sig-name descname"><span class="pre">MAGIC_NUMBER</span></span><a class="headerlink" href="#importlib.util.MAGIC_NUMBER" title="Link to this definition">¶</a></dt>
  1482. <dd><p>The bytes which represent the bytecode version number. If you need help with
  1483. loading/writing bytecode then consider <a class="reference internal" href="#importlib.abc.SourceLoader" title="importlib.abc.SourceLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.SourceLoader</span></code></a>.</p>
  1484. <div class="versionadded">
  1485. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1486. </div>
  1487. </dd></dl>
  1488. <dl class="py function">
  1489. <dt class="sig sig-object py" id="importlib.util.cache_from_source">
  1490. <span class="sig-prename descclassname"><span class="pre">importlib.util.</span></span><span class="sig-name descname"><span class="pre">cache_from_source</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">debug_override</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="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optimization</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="#importlib.util.cache_from_source" title="Link to this definition">¶</a></dt>
  1491. <dd><p>Return the <span class="target" id="index-21"></span><a class="pep reference external" href="https://peps.python.org/pep-3147/"><strong>PEP 3147</strong></a>/<span class="target" id="index-22"></span><a class="pep reference external" href="https://peps.python.org/pep-0488/"><strong>PEP 488</strong></a> path to the byte-compiled file associated
  1492. with the source <em>path</em>. For example, if <em>path</em> is <code class="docutils literal notranslate"><span class="pre">/foo/bar/baz.py</span></code> the return
  1493. value would be <code class="docutils literal notranslate"><span class="pre">/foo/bar/__pycache__/baz.cpython-32.pyc</span></code> for Python 3.2.
  1494. The <code class="docutils literal notranslate"><span class="pre">cpython-32</span></code> string comes from the current magic tag (see
  1495. <code class="xref py py-func docutils literal notranslate"><span class="pre">get_tag()</span></code>; if <code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.implementation.cache_tag</span></code> is not defined then
  1496. <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> will be raised).</p>
  1497. <p>The <em>optimization</em> parameter is used to specify the optimization level of the
  1498. bytecode file. An empty string represents no optimization, so
  1499. <code class="docutils literal notranslate"><span class="pre">/foo/bar/baz.py</span></code> with an <em>optimization</em> of <code class="docutils literal notranslate"><span class="pre">''</span></code> will result in a
  1500. bytecode path of <code class="docutils literal notranslate"><span class="pre">/foo/bar/__pycache__/baz.cpython-32.pyc</span></code>. <code class="docutils literal notranslate"><span class="pre">None</span></code> causes
  1501. the interpreter’s optimization level to be used. Any other value’s string
  1502. representation is used, so <code class="docutils literal notranslate"><span class="pre">/foo/bar/baz.py</span></code> with an <em>optimization</em> of
  1503. <code class="docutils literal notranslate"><span class="pre">2</span></code> will lead to the bytecode path of
  1504. <code class="docutils literal notranslate"><span class="pre">/foo/bar/__pycache__/baz.cpython-32.opt-2.pyc</span></code>. The string representation
  1505. of <em>optimization</em> can only be alphanumeric, else <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>
  1506. <p>The <em>debug_override</em> parameter is deprecated and can be used to override
  1507. the system’s value for <code class="docutils literal notranslate"><span class="pre">__debug__</span></code>. A <code class="docutils literal notranslate"><span class="pre">True</span></code> value is the equivalent of
  1508. setting <em>optimization</em> to the empty string. A <code class="docutils literal notranslate"><span class="pre">False</span></code> value is the same as
  1509. setting <em>optimization</em> to <code class="docutils literal notranslate"><span class="pre">1</span></code>. If both <em>debug_override</em> an <em>optimization</em>
  1510. are not <code class="docutils literal notranslate"><span class="pre">None</span></code> then <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.</p>
  1511. <div class="versionadded">
  1512. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1513. </div>
  1514. <div class="versionchanged">
  1515. <p><span class="versionmodified changed">Changed in version 3.5: </span>The <em>optimization</em> parameter was added and the <em>debug_override</em> parameter
  1516. was deprecated.</p>
  1517. </div>
  1518. <div class="versionchanged">
  1519. <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  1520. </div>
  1521. </dd></dl>
  1522. <dl class="py function">
  1523. <dt class="sig sig-object py" id="importlib.util.source_from_cache">
  1524. <span class="sig-prename descclassname"><span class="pre">importlib.util.</span></span><span class="sig-name descname"><span class="pre">source_from_cache</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.source_from_cache" title="Link to this definition">¶</a></dt>
  1525. <dd><p>Given the <em>path</em> to a <span class="target" id="index-23"></span><a class="pep reference external" href="https://peps.python.org/pep-3147/"><strong>PEP 3147</strong></a> file name, return the associated source code
  1526. file path. For example, if <em>path</em> is
  1527. <code class="docutils literal notranslate"><span class="pre">/foo/bar/__pycache__/baz.cpython-32.pyc</span></code> the returned path would be
  1528. <code class="docutils literal notranslate"><span class="pre">/foo/bar/baz.py</span></code>. <em>path</em> need not exist, however if it does not conform
  1529. to <span class="target" id="index-24"></span><a class="pep reference external" href="https://peps.python.org/pep-3147/"><strong>PEP 3147</strong></a> or <span class="target" id="index-25"></span><a class="pep reference external" href="https://peps.python.org/pep-0488/"><strong>PEP 488</strong></a> format, a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised. If
  1530. <code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.implementation.cache_tag</span></code> is not defined,
  1531. <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> is raised.</p>
  1532. <div class="versionadded">
  1533. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1534. </div>
  1535. <div class="versionchanged">
  1536. <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  1537. </div>
  1538. </dd></dl>
  1539. <dl class="py function">
  1540. <dt class="sig sig-object py" id="importlib.util.decode_source">
  1541. <span class="sig-prename descclassname"><span class="pre">importlib.util.</span></span><span class="sig-name descname"><span class="pre">decode_source</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source_bytes</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.decode_source" title="Link to this definition">¶</a></dt>
  1542. <dd><p>Decode the given bytes representing source code and return it as a string
  1543. with universal newlines (as required by
  1544. <a class="reference internal" href="#importlib.abc.InspectLoader.get_source" title="importlib.abc.InspectLoader.get_source"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.InspectLoader.get_source()</span></code></a>).</p>
  1545. <div class="versionadded">
  1546. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1547. </div>
  1548. </dd></dl>
  1549. <dl class="py function">
  1550. <dt class="sig sig-object py" id="importlib.util.resolve_name">
  1551. <span class="sig-prename descclassname"><span class="pre">importlib.util.</span></span><span class="sig-name descname"><span class="pre">resolve_name</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">package</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.resolve_name" title="Link to this definition">¶</a></dt>
  1552. <dd><p>Resolve a relative module name to an absolute one.</p>
  1553. <p>If <strong>name</strong> has no leading dots, then <strong>name</strong> is simply returned. This
  1554. allows for usage such as
  1555. <code class="docutils literal notranslate"><span class="pre">importlib.util.resolve_name('sys',</span> <span class="pre">__spec__.parent)</span></code> without doing a
  1556. check to see if the <strong>package</strong> argument is needed.</p>
  1557. <p><a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> is raised if <strong>name</strong> is a relative module name but
  1558. <strong>package</strong> is a false value (e.g. <code class="docutils literal notranslate"><span class="pre">None</span></code> or the empty string).
  1559. <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> is also raised if a relative name would escape its
  1560. containing package (e.g. requesting <code class="docutils literal notranslate"><span class="pre">..bacon</span></code> from within the <code class="docutils literal notranslate"><span class="pre">spam</span></code>
  1561. package).</p>
  1562. <div class="versionadded">
  1563. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1564. </div>
  1565. <div class="versionchanged">
  1566. <p><span class="versionmodified changed">Changed in version 3.9: </span>To improve consistency with import statements, raise
  1567. <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> instead of <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> for invalid relative
  1568. import attempts.</p>
  1569. </div>
  1570. </dd></dl>
  1571. <dl class="py function">
  1572. <dt class="sig sig-object py" id="importlib.util.find_spec">
  1573. <span class="sig-prename descclassname"><span class="pre">importlib.util.</span></span><span class="sig-name descname"><span class="pre">find_spec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">package</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="#importlib.util.find_spec" title="Link to this definition">¶</a></dt>
  1574. <dd><p>Find the <a class="reference internal" href="../glossary.html#term-module-spec"><span class="xref std std-term">spec</span></a> for a module, optionally relative to
  1575. the specified <strong>package</strong> name. If the module is in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>,
  1576. then <code class="docutils literal notranslate"><span class="pre">sys.modules[name].__spec__</span></code> is returned (unless the spec would be
  1577. <code class="docutils literal notranslate"><span class="pre">None</span></code> or is not set, in which case <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised).
  1578. Otherwise a search using <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a> is done. <code class="docutils literal notranslate"><span class="pre">None</span></code> is
  1579. returned if no spec is found.</p>
  1580. <p>If <strong>name</strong> is for a submodule (contains a dot), the parent module is
  1581. automatically imported.</p>
  1582. <p><strong>name</strong> and <strong>package</strong> work the same as for <code class="xref py py-func docutils literal notranslate"><span class="pre">import_module()</span></code>.</p>
  1583. <div class="versionadded">
  1584. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1585. </div>
  1586. <div class="versionchanged">
  1587. <p><span class="versionmodified changed">Changed in version 3.7: </span>Raises <a class="reference internal" href="exceptions.html#ModuleNotFoundError" title="ModuleNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ModuleNotFoundError</span></code></a> instead of <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a> if
  1588. <strong>package</strong> is in fact not a package (i.e. lacks a <a class="reference internal" href="../reference/import.html#path__" title="__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__path__</span></code></a>
  1589. attribute).</p>
  1590. </div>
  1591. </dd></dl>
  1592. <dl class="py function">
  1593. <dt class="sig sig-object py" id="importlib.util.module_from_spec">
  1594. <span class="sig-prename descclassname"><span class="pre">importlib.util.</span></span><span class="sig-name descname"><span class="pre">module_from_spec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">spec</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.module_from_spec" title="Link to this definition">¶</a></dt>
  1595. <dd><p>Create a new module based on <strong>spec</strong> and
  1596. <a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">spec.loader.create_module</span></code></a>.</p>
  1597. <p>If <a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">spec.loader.create_module</span></code></a>
  1598. does not return <code class="docutils literal notranslate"><span class="pre">None</span></code>, then any pre-existing attributes will not be reset.
  1599. Also, no <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a> will be raised if triggered while accessing
  1600. <strong>spec</strong> or setting an attribute on the module.</p>
  1601. <p>This function is preferred over using <a class="reference internal" href="types.html#types.ModuleType" title="types.ModuleType"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.ModuleType</span></code></a> to create a
  1602. new module as <strong>spec</strong> is used to set as many import-controlled attributes on
  1603. the module as possible.</p>
  1604. <div class="versionadded">
  1605. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1606. </div>
  1607. </dd></dl>
  1608. <dl class="py function">
  1609. <dt class="sig sig-object py" id="importlib.util.spec_from_loader">
  1610. <span class="sig-prename descclassname"><span class="pre">importlib.util.</span></span><span class="sig-name descname"><span class="pre">spec_from_loader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">loader</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">origin</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">is_package</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="#importlib.util.spec_from_loader" title="Link to this definition">¶</a></dt>
  1611. <dd><p>A factory function for creating a <a class="reference internal" href="#importlib.machinery.ModuleSpec" title="importlib.machinery.ModuleSpec"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModuleSpec</span></code></a>
  1612. instance based on a loader. The parameters have the same meaning as they do
  1613. for ModuleSpec. The function uses available <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> APIs, such as
  1614. <code class="xref py py-meth docutils literal notranslate"><span class="pre">InspectLoader.is_package()</span></code>, to fill in any missing
  1615. information on the spec.</p>
  1616. <div class="versionadded">
  1617. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1618. </div>
  1619. </dd></dl>
  1620. <dl class="py function">
  1621. <dt class="sig sig-object py" id="importlib.util.spec_from_file_location">
  1622. <span class="sig-prename descclassname"><span class="pre">importlib.util.</span></span><span class="sig-name descname"><span class="pre">spec_from_file_location</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">location</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">loader</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">submodule_search_locations</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="#importlib.util.spec_from_file_location" title="Link to this definition">¶</a></dt>
  1623. <dd><p>A factory function for creating a <a class="reference internal" href="#importlib.machinery.ModuleSpec" title="importlib.machinery.ModuleSpec"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModuleSpec</span></code></a>
  1624. instance based on the path to a file. Missing information will be filled in
  1625. on the spec by making use of loader APIs and by the implication that the
  1626. module will be file-based.</p>
  1627. <div class="versionadded">
  1628. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1629. </div>
  1630. <div class="versionchanged">
  1631. <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  1632. </div>
  1633. </dd></dl>
  1634. <dl class="py function">
  1635. <dt class="sig sig-object py" id="importlib.util.source_hash">
  1636. <span class="sig-prename descclassname"><span class="pre">importlib.util.</span></span><span class="sig-name descname"><span class="pre">source_hash</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source_bytes</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.source_hash" title="Link to this definition">¶</a></dt>
  1637. <dd><p>Return the hash of <em>source_bytes</em> as bytes. A hash-based <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> file embeds
  1638. the <a class="reference internal" href="#importlib.util.source_hash" title="importlib.util.source_hash"><code class="xref py py-func docutils literal notranslate"><span class="pre">source_hash()</span></code></a> of the corresponding source file’s contents in its
  1639. header.</p>
  1640. <div class="versionadded">
  1641. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1642. </div>
  1643. </dd></dl>
  1644. <dl class="py function">
  1645. <dt class="sig sig-object py" id="importlib.util._incompatible_extension_module_restrictions">
  1646. <span class="sig-prename descclassname"><span class="pre">importlib.util.</span></span><span class="sig-name descname"><span class="pre">_incompatible_extension_module_restrictions</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">disable_check</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util._incompatible_extension_module_restrictions" title="Link to this definition">¶</a></dt>
  1647. <dd><p>A context manager that can temporarily skip the compatibility check
  1648. for extension modules. By default the check is enabled and will fail
  1649. when a single-phase init module is imported in a subinterpreter.
  1650. It will also fail for a multi-phase init module that doesn’t
  1651. explicitly support a per-interpreter GIL, when imported
  1652. in an interpreter with its own GIL.</p>
  1653. <p>Note that this function is meant to accommodate an unusual case;
  1654. one which is likely to eventually go away. There’s is a pretty good
  1655. chance this is not what you were looking for.</p>
  1656. <p>You can get the same effect as this function by implementing the
  1657. basic interface of multi-phase init (<span class="target" id="index-26"></span><a class="pep reference external" href="https://peps.python.org/pep-0489/"><strong>PEP 489</strong></a>) and lying about
  1658. support for multiple interpreters (or per-interpreter GIL).</p>
  1659. <div class="admonition warning">
  1660. <p class="admonition-title">Warning</p>
  1661. <p>Using this function to disable the check can lead to
  1662. unexpected behavior and even crashes. It should only be used during
  1663. extension module development.</p>
  1664. </div>
  1665. <div class="versionadded">
  1666. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1667. </div>
  1668. </dd></dl>
  1669. <dl class="py class">
  1670. <dt class="sig sig-object py" id="importlib.util.LazyLoader">
  1671. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.util.</span></span><span class="sig-name descname"><span class="pre">LazyLoader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">loader</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.LazyLoader" title="Link to this definition">¶</a></dt>
  1672. <dd><p>A class which postpones the execution of the loader of a module until the
  1673. module has an attribute accessed.</p>
  1674. <p>This class <strong>only</strong> works with loaders that define
  1675. <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> as control over what module type
  1676. is used for the module is required. For those same reasons, the loader’s
  1677. <a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</span></code></a> method must return <code class="docutils literal notranslate"><span class="pre">None</span></code> or a
  1678. type for which its <code class="docutils literal notranslate"><span class="pre">__class__</span></code> attribute can be mutated along with not
  1679. using <a class="reference internal" href="../glossary.html#term-__slots__"><span class="xref std std-term">slots</span></a>. Finally, modules which substitute the object
  1680. placed into <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> will not work as there is no way to properly
  1681. replace the module references throughout the interpreter safely;
  1682. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if such a substitution is detected.</p>
  1683. <div class="admonition note">
  1684. <p class="admonition-title">Note</p>
  1685. <p>For projects where startup time is critical, this class allows for
  1686. potentially minimizing the cost of loading a module if it is never used.
  1687. For projects where startup time is not essential then use of this class is
  1688. <strong>heavily</strong> discouraged due to error messages created during loading being
  1689. postponed and thus occurring out of context.</p>
  1690. </div>
  1691. <div class="versionadded">
  1692. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1693. </div>
  1694. <div class="versionchanged">
  1695. <p><span class="versionmodified changed">Changed in version 3.6: </span>Began calling <a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</span></code></a>, removing the
  1696. compatibility warning for <a class="reference internal" href="#importlib.machinery.BuiltinImporter" title="importlib.machinery.BuiltinImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.BuiltinImporter</span></code></a> and
  1697. <a class="reference internal" href="#importlib.machinery.ExtensionFileLoader" title="importlib.machinery.ExtensionFileLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.ExtensionFileLoader</span></code></a>.</p>
  1698. </div>
  1699. <dl class="py method">
  1700. <dt class="sig sig-object py" id="importlib.util.LazyLoader.factory">
  1701. <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">factory</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">loader</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.LazyLoader.factory" title="Link to this definition">¶</a></dt>
  1702. <dd><p>A class method which returns a callable that creates a lazy loader. This
  1703. is meant to be used in situations where the loader is passed by class
  1704. instead of by instance.</p>
  1705. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">suffixes</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">SOURCE_SUFFIXES</span>
  1706. <span class="n">loader</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">SourceFileLoader</span>
  1707. <span class="n">lazy_loader</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">LazyLoader</span><span class="o">.</span><span class="n">factory</span><span class="p">(</span><span class="n">loader</span><span class="p">)</span>
  1708. <span class="n">finder</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">FileFinder</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="p">(</span><span class="n">lazy_loader</span><span class="p">,</span> <span class="n">suffixes</span><span class="p">))</span>
  1709. </pre></div>
  1710. </div>
  1711. </dd></dl>
  1712. </dd></dl>
  1713. </section>
  1714. <section id="examples">
  1715. <span id="importlib-examples"></span><h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
  1716. <section id="importing-programmatically">
  1717. <h3>Importing programmatically<a class="headerlink" href="#importing-programmatically" title="Link to this heading">¶</a></h3>
  1718. <p>To programmatically import a module, use <a class="reference internal" href="#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>.</p>
  1719. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">importlib</span>
  1720. <span class="n">itertools</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">import_module</span><span class="p">(</span><span class="s1">&#39;itertools&#39;</span><span class="p">)</span>
  1721. </pre></div>
  1722. </div>
  1723. </section>
  1724. <section id="checking-if-a-module-can-be-imported">
  1725. <h3>Checking if a module can be imported<a class="headerlink" href="#checking-if-a-module-can-be-imported" title="Link to this heading">¶</a></h3>
  1726. <p>If you need to find out if a module can be imported without actually doing the
  1727. import, then you should use <a class="reference internal" href="#importlib.util.find_spec" title="importlib.util.find_spec"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.find_spec()</span></code></a>.</p>
  1728. <p>Note that if <code class="docutils literal notranslate"><span class="pre">name</span></code> is a submodule (contains a dot),
  1729. <a class="reference internal" href="#importlib.util.find_spec" title="importlib.util.find_spec"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.find_spec()</span></code></a> will import the parent module.</p>
  1730. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">importlib.util</span>
  1731. <span class="kn">import</span> <span class="nn">sys</span>
  1732. <span class="c1"># For illustrative purposes.</span>
  1733. <span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;itertools&#39;</span>
  1734. <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">:</span>
  1735. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">name</span><span class="si">!r}</span><span class="s2"> already in sys.modules&quot;</span><span class="p">)</span>
  1736. <span class="k">elif</span> <span class="p">(</span><span class="n">spec</span> <span class="o">:=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">find_spec</span><span class="p">(</span><span class="n">name</span><span class="p">))</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
  1737. <span class="c1"># If you chose to perform the actual import ...</span>
  1738. <span class="n">module</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">module_from_spec</span><span class="p">(</span><span class="n">spec</span><span class="p">)</span>
  1739. <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">module</span>
  1740. <span class="n">spec</span><span class="o">.</span><span class="n">loader</span><span class="o">.</span><span class="n">exec_module</span><span class="p">(</span><span class="n">module</span><span class="p">)</span>
  1741. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">name</span><span class="si">!r}</span><span class="s2"> has been imported&quot;</span><span class="p">)</span>
  1742. <span class="k">else</span><span class="p">:</span>
  1743. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;can&#39;t find the </span><span class="si">{</span><span class="n">name</span><span class="si">!r}</span><span class="s2"> module&quot;</span><span class="p">)</span>
  1744. </pre></div>
  1745. </div>
  1746. </section>
  1747. <section id="importing-a-source-file-directly">
  1748. <h3>Importing a source file directly<a class="headerlink" href="#importing-a-source-file-directly" title="Link to this heading">¶</a></h3>
  1749. <p>To import a Python source file directly, use the following recipe:</p>
  1750. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">importlib.util</span>
  1751. <span class="kn">import</span> <span class="nn">sys</span>
  1752. <span class="c1"># For illustrative purposes.</span>
  1753. <span class="kn">import</span> <span class="nn">tokenize</span>
  1754. <span class="n">file_path</span> <span class="o">=</span> <span class="n">tokenize</span><span class="o">.</span><span class="vm">__file__</span>
  1755. <span class="n">module_name</span> <span class="o">=</span> <span class="n">tokenize</span><span class="o">.</span><span class="vm">__name__</span>
  1756. <span class="n">spec</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">spec_from_file_location</span><span class="p">(</span><span class="n">module_name</span><span class="p">,</span> <span class="n">file_path</span><span class="p">)</span>
  1757. <span class="n">module</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">module_from_spec</span><span class="p">(</span><span class="n">spec</span><span class="p">)</span>
  1758. <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">module_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">module</span>
  1759. <span class="n">spec</span><span class="o">.</span><span class="n">loader</span><span class="o">.</span><span class="n">exec_module</span><span class="p">(</span><span class="n">module</span><span class="p">)</span>
  1760. </pre></div>
  1761. </div>
  1762. </section>
  1763. <section id="implementing-lazy-imports">
  1764. <h3>Implementing lazy imports<a class="headerlink" href="#implementing-lazy-imports" title="Link to this heading">¶</a></h3>
  1765. <p>The example below shows how to implement lazy imports:</p>
  1766. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">importlib.util</span>
  1767. <span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">sys</span>
  1768. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">lazy_import</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
  1769. <span class="gp">... </span> <span class="n">spec</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">find_spec</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
  1770. <span class="gp">... </span> <span class="n">loader</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">LazyLoader</span><span class="p">(</span><span class="n">spec</span><span class="o">.</span><span class="n">loader</span><span class="p">)</span>
  1771. <span class="gp">... </span> <span class="n">spec</span><span class="o">.</span><span class="n">loader</span> <span class="o">=</span> <span class="n">loader</span>
  1772. <span class="gp">... </span> <span class="n">module</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">module_from_spec</span><span class="p">(</span><span class="n">spec</span><span class="p">)</span>
  1773. <span class="gp">... </span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">module</span>
  1774. <span class="gp">... </span> <span class="n">loader</span><span class="o">.</span><span class="n">exec_module</span><span class="p">(</span><span class="n">module</span><span class="p">)</span>
  1775. <span class="gp">... </span> <span class="k">return</span> <span class="n">module</span>
  1776. <span class="gp">...</span>
  1777. <span class="gp">&gt;&gt;&gt; </span><span class="n">lazy_typing</span> <span class="o">=</span> <span class="n">lazy_import</span><span class="p">(</span><span class="s2">&quot;typing&quot;</span><span class="p">)</span>
  1778. <span class="gp">&gt;&gt;&gt; </span><span class="c1">#lazy_typing is a real module object,</span>
  1779. <span class="gp">&gt;&gt;&gt; </span><span class="c1">#but it is not loaded in memory yet.</span>
  1780. <span class="gp">&gt;&gt;&gt; </span><span class="n">lazy_typing</span><span class="o">.</span><span class="n">TYPE_CHECKING</span>
  1781. <span class="go">False</span>
  1782. </pre></div>
  1783. </div>
  1784. </section>
  1785. <section id="setting-up-an-importer">
  1786. <h3>Setting up an importer<a class="headerlink" href="#setting-up-an-importer" title="Link to this heading">¶</a></h3>
  1787. <p>For deep customizations of import, you typically want to implement an
  1788. <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a>. This means managing both the <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finder</span></a> and <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a>
  1789. side of things. For finders there are two flavours to choose from depending on
  1790. your needs: a <a class="reference internal" href="../glossary.html#term-meta-path-finder"><span class="xref std std-term">meta path finder</span></a> or a <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a>. The
  1791. former is what you would put on <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a> while the latter is what
  1792. you create using a <a class="reference internal" href="../glossary.html#term-path-entry-hook"><span class="xref std std-term">path entry hook</span></a> on <a class="reference internal" href="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> which works
  1793. with <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 to potentially create a finder. This example will
  1794. show you how to register your own importers so that import will use them (for
  1795. creating an importer for yourself, read the documentation for the appropriate
  1796. classes defined within this package):</p>
  1797. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">importlib.machinery</span>
  1798. <span class="kn">import</span> <span class="nn">sys</span>
  1799. <span class="c1"># For illustrative purposes only.</span>
  1800. <span class="n">SpamMetaPathFinder</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">PathFinder</span>
  1801. <span class="n">SpamPathEntryFinder</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">FileFinder</span>
  1802. <span class="n">loader_details</span> <span class="o">=</span> <span class="p">(</span><span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">SourceFileLoader</span><span class="p">,</span>
  1803. <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">SOURCE_SUFFIXES</span><span class="p">)</span>
  1804. <span class="c1"># Setting up a meta path finder.</span>
  1805. <span class="c1"># Make sure to put the finder in the proper location in the list in terms of</span>
  1806. <span class="c1"># priority.</span>
  1807. <span class="n">sys</span><span class="o">.</span><span class="n">meta_path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">SpamMetaPathFinder</span><span class="p">)</span>
  1808. <span class="c1"># Setting up a path entry finder.</span>
  1809. <span class="c1"># Make sure to put the path hook in the proper location in the list in terms</span>
  1810. <span class="c1"># of priority.</span>
  1811. <span class="n">sys</span><span class="o">.</span><span class="n">path_hooks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">SpamPathEntryFinder</span><span class="o">.</span><span class="n">path_hook</span><span class="p">(</span><span class="n">loader_details</span><span class="p">))</span>
  1812. </pre></div>
  1813. </div>
  1814. </section>
  1815. <section id="approximating-importlib-import-module">
  1816. <h3>Approximating <a class="reference internal" href="#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><a class="headerlink" href="#approximating-importlib-import-module" title="Link to this heading">¶</a></h3>
  1817. <p>Import itself is implemented in Python code, making it possible to
  1818. expose most of the import machinery through importlib. The following
  1819. helps illustrate the various APIs that importlib exposes by providing an
  1820. approximate implementation of
  1821. <a class="reference internal" href="#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>:</p>
  1822. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">importlib.util</span>
  1823. <span class="kn">import</span> <span class="nn">sys</span>
  1824. <span class="k">def</span> <span class="nf">import_module</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">package</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
  1825. <span class="w"> </span><span class="sd">&quot;&quot;&quot;An approximate implementation of import.&quot;&quot;&quot;</span>
  1826. <span class="n">absolute_name</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">resolve_name</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">package</span><span class="p">)</span>
  1827. <span class="k">try</span><span class="p">:</span>
  1828. <span class="k">return</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">absolute_name</span><span class="p">]</span>
  1829. <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
  1830. <span class="k">pass</span>
  1831. <span class="n">path</span> <span class="o">=</span> <span class="kc">None</span>
  1832. <span class="k">if</span> <span class="s1">&#39;.&#39;</span> <span class="ow">in</span> <span class="n">absolute_name</span><span class="p">:</span>
  1833. <span class="n">parent_name</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">child_name</span> <span class="o">=</span> <span class="n">absolute_name</span><span class="o">.</span><span class="n">rpartition</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
  1834. <span class="n">parent_module</span> <span class="o">=</span> <span class="n">import_module</span><span class="p">(</span><span class="n">parent_name</span><span class="p">)</span>
  1835. <span class="n">path</span> <span class="o">=</span> <span class="n">parent_module</span><span class="o">.</span><span class="n">__spec__</span><span class="o">.</span><span class="n">submodule_search_locations</span>
  1836. <span class="k">for</span> <span class="n">finder</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">meta_path</span><span class="p">:</span>
  1837. <span class="n">spec</span> <span class="o">=</span> <span class="n">finder</span><span class="o">.</span><span class="n">find_spec</span><span class="p">(</span><span class="n">absolute_name</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span>
  1838. <span class="k">if</span> <span class="n">spec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
  1839. <span class="k">break</span>
  1840. <span class="k">else</span><span class="p">:</span>
  1841. <span class="n">msg</span> <span class="o">=</span> <span class="sa">f</span><span class="s1">&#39;No module named </span><span class="si">{</span><span class="n">absolute_name</span><span class="si">!r}</span><span class="s1">&#39;</span>
  1842. <span class="k">raise</span> <span class="ne">ModuleNotFoundError</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">absolute_name</span><span class="p">)</span>
  1843. <span class="n">module</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">module_from_spec</span><span class="p">(</span><span class="n">spec</span><span class="p">)</span>
  1844. <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">absolute_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">module</span>
  1845. <span class="n">spec</span><span class="o">.</span><span class="n">loader</span><span class="o">.</span><span class="n">exec_module</span><span class="p">(</span><span class="n">module</span><span class="p">)</span>
  1846. <span class="k">if</span> <span class="n">path</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
  1847. <span class="nb">setattr</span><span class="p">(</span><span class="n">parent_module</span><span class="p">,</span> <span class="n">child_name</span><span class="p">,</span> <span class="n">module</span><span class="p">)</span>
  1848. <span class="k">return</span> <span class="n">module</span>
  1849. </pre></div>
  1850. </div>
  1851. </section>
  1852. </section>
  1853. </section>
  1854. <div class="clearer"></div>
  1855. </div>
  1856. </div>
  1857. </div>
  1858. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1859. <div class="sphinxsidebarwrapper">
  1860. <div>
  1861. <h3><a href="../contents.html">Table of Contents</a></h3>
  1862. <ul>
  1863. <li><a class="reference internal" href="#"><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><ul>
  1864. <li><a class="reference internal" href="#introduction">Introduction</a></li>
  1865. <li><a class="reference internal" href="#functions">Functions</a></li>
  1866. <li><a class="reference internal" href="#module-importlib.abc"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.abc</span></code> – Abstract base classes related to import</a></li>
  1867. <li><a class="reference internal" href="#module-importlib.machinery"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.machinery</span></code> – Importers and path hooks</a></li>
  1868. <li><a class="reference internal" href="#module-importlib.util"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.util</span></code> – Utility code for importers</a></li>
  1869. <li><a class="reference internal" href="#examples">Examples</a><ul>
  1870. <li><a class="reference internal" href="#importing-programmatically">Importing programmatically</a></li>
  1871. <li><a class="reference internal" href="#checking-if-a-module-can-be-imported">Checking if a module can be imported</a></li>
  1872. <li><a class="reference internal" href="#importing-a-source-file-directly">Importing a source file directly</a></li>
  1873. <li><a class="reference internal" href="#implementing-lazy-imports">Implementing lazy imports</a></li>
  1874. <li><a class="reference internal" href="#setting-up-an-importer">Setting up an importer</a></li>
  1875. <li><a class="reference internal" href="#approximating-importlib-import-module">Approximating <code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a></li>
  1876. </ul>
  1877. </li>
  1878. </ul>
  1879. </li>
  1880. </ul>
  1881. </div>
  1882. <div>
  1883. <h4>Previous topic</h4>
  1884. <p class="topless"><a href="runpy.html"
  1885. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">runpy</span></code> — Locating and executing Python modules</a></p>
  1886. </div>
  1887. <div>
  1888. <h4>Next topic</h4>
  1889. <p class="topless"><a href="importlib.resources.html"
  1890. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.resources</span></code> – Package resource reading, opening and access</a></p>
  1891. </div>
  1892. <div role="note" aria-label="source link">
  1893. <h3>This Page</h3>
  1894. <ul class="this-page-menu">
  1895. <li><a href="../bugs.html">Report a Bug</a></li>
  1896. <li>
  1897. <a href="https://github.com/python/cpython/blob/main/Doc/library/importlib.rst"
  1898. rel="nofollow">Show Source
  1899. </a>
  1900. </li>
  1901. </ul>
  1902. </div>
  1903. </div>
  1904. <div id="sidebarbutton" title="Collapse sidebar">
  1905. <span>«</span>
  1906. </div>
  1907. </div>
  1908. <div class="clearer"></div>
  1909. </div>
  1910. <div class="related" role="navigation" aria-label="related navigation">
  1911. <h3>Navigation</h3>
  1912. <ul>
  1913. <li class="right" style="margin-right: 10px">
  1914. <a href="../genindex.html" title="General Index"
  1915. >index</a></li>
  1916. <li class="right" >
  1917. <a href="../py-modindex.html" title="Python Module Index"
  1918. >modules</a> |</li>
  1919. <li class="right" >
  1920. <a href="importlib.resources.html" title="importlib.resources – Package resource reading, opening and access"
  1921. >next</a> |</li>
  1922. <li class="right" >
  1923. <a href="runpy.html" title="runpy — Locating and executing Python modules"
  1924. >previous</a> |</li>
  1925. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1926. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1927. <li class="switchers">
  1928. <div class="language_switcher_placeholder"></div>
  1929. <div class="version_switcher_placeholder"></div>
  1930. </li>
  1931. <li>
  1932. </li>
  1933. <li id="cpython-language-and-version">
  1934. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1935. </li>
  1936. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1937. <li class="nav-item nav-item-2"><a href="modules.html" >Importing Modules</a> &#187;</li>
  1938. <li class="nav-item nav-item-this"><a href=""><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></li>
  1939. <li class="right">
  1940. <div class="inline-search" role="search">
  1941. <form class="inline-search" action="../search.html" method="get">
  1942. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1943. <input type="submit" value="Go" />
  1944. </form>
  1945. </div>
  1946. |
  1947. </li>
  1948. <li class="right">
  1949. <label class="theme-selector-label">
  1950. Theme
  1951. <select class="theme-selector" oninput="activateTheme(this.value)">
  1952. <option value="auto" selected>Auto</option>
  1953. <option value="light">Light</option>
  1954. <option value="dark">Dark</option>
  1955. </select>
  1956. </label> |</li>
  1957. </ul>
  1958. </div>
  1959. <div class="footer">
  1960. &copy;
  1961. <a href="../copyright.html">
  1962. Copyright
  1963. </a>
  1964. 2001-2024, Python Software Foundation.
  1965. <br />
  1966. This page is licensed under the Python Software Foundation License Version 2.
  1967. <br />
  1968. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1969. <br />
  1970. See <a href="/license.html">History and License</a> for more information.<br />
  1971. <br />
  1972. The Python Software Foundation is a non-profit corporation.
  1973. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1974. <br />
  1975. <br />
  1976. Last updated on Apr 09, 2024 (13:47 UTC).
  1977. <a href="/bugs.html">Found a bug</a>?
  1978. <br />
  1979. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1980. </div>
  1981. </body>
  1982. </html>
上海开阖软件有限公司 沪ICP备12045867号-1