parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="importlib — The implementation of import" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/importlib.html" />10<meta property="og:site_name" content="Python documentation" />11<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..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_importlib_ec3fea04.png" />15<meta property="og:image:alt" 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..." />16<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..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>importlib — The implementation of import — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="importlib.resources – Package resource reading, opening and access" href="importlib.resources.html" />43 <link rel="prev" title="runpy — Locating and executing Python modules" href="runpy.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/importlib.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <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>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<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>108<li><a class="reference internal" href="#introduction">Introduction</a></li>109<li><a class="reference internal" href="#functions">Functions</a></li>110<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>111<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>112<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>113<li><a class="reference internal" href="#examples">Examples</a><ul>114<li><a class="reference internal" href="#importing-programmatically">Importing programmatically</a></li>115<li><a class="reference internal" href="#checking-if-a-module-can-be-imported">Checking if a module can be imported</a></li>116<li><a class="reference internal" href="#importing-a-source-file-directly">Importing a source file directly</a></li>117<li><a class="reference internal" href="#implementing-lazy-imports">Implementing lazy imports</a></li>118<li><a class="reference internal" href="#setting-up-an-importer">Setting up an importer</a></li>119<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>120</ul>121</li>122</ul>123</li>124</ul>125 126 </div>127 <div>128 <h4>Previous topic</h4>129 <p class="topless"><a href="runpy.html"130 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>131 </div>132 <div>133 <h4>Next topic</h4>134 <p class="topless"><a href="importlib.resources.html"135 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>136 </div>137 <script>138 document.addEventListener('DOMContentLoaded', () => {139 const title = document.querySelector('meta[property="og:title"]').content;140 const elements = document.querySelectorAll('.improvepage');141 const pageurl = window.location.href.split('?')[0];142 elements.forEach(element => {143 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));144 url.searchParams.set('pagetitle', title);145 url.searchParams.set('pageurl', pageurl);146 url.searchParams.set('pagesource', "library/importlib.rst");147 element.href = url.toString();148 });149 });150 </script>151 <div role="note" aria-label="source link">152 <h3>This page</h3>153 <ul class="this-page-menu">154 <li><a href="../bugs.html">Report a bug</a></li>155 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>156 <li>157 <a href="https://github.com/python/cpython/blob/main/Doc/library/importlib.rst?plain=1"158 rel="nofollow">Show source159 </a>160 </li>161 162 </ul>163 </div>164 </nav>165 </div>166</div>167 168 169 <div class="related" role="navigation" aria-label="Related">170 <h3>Navigation</h3>171 <ul>172 <li class="right" style="margin-right: 10px">173 <a href="../genindex.html" title="General Index"174 accesskey="I">index</a></li>175 <li class="right" >176 <a href="../py-modindex.html" title="Python Module Index"177 >modules</a> |</li>178 <li class="right" >179 <a href="importlib.resources.html" title="importlib.resources – Package resource reading, opening and access"180 accesskey="N">next</a> |</li>181 <li class="right" >182 <a href="runpy.html" title="runpy — Locating and executing Python modules"183 accesskey="P">previous</a> |</li>184 185 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>186 <li><a href="https://www.python.org/">Python</a> »</li>187 <li class="switchers">188 <div class="language_switcher_placeholder"></div>189 <div class="version_switcher_placeholder"></div>190 </li>191 <li>192 193 </li>194 <li id="cpython-language-and-version">195 <a href="../index.html">3.15.0a6 Documentation</a> »196 </li>197 198 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>199 <li class="nav-item nav-item-2"><a href="modules.html" accesskey="U">Importing Modules</a> »</li>200 <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>201 <li class="right">202 203 204 <div class="inline-search" role="search">205 <form class="inline-search" action="../search.html" method="get">206 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">207 <input type="submit" value="Go">208 </form>209 </div>210 |211 </li>212 <li class="right">213<label class="theme-selector-label">214 Theme215 <select class="theme-selector" oninput="activateTheme(this.value)">216 <option value="auto" selected>Auto</option>217 <option value="light">Light</option>218 <option value="dark">Dark</option>219 </select>220</label> |</li>221 222 </ul>223 </div> 224 225 <div class="document">226 <div class="documentwrapper">227 <div class="bodywrapper">228 <div class="body" role="main">229 230 <section id="module-importlib">231<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>232<div class="versionadded">233<p><span class="versionmodified added">Added in version 3.1.</span></p>234</div>235<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/importlib/__init__.py">Lib/importlib/__init__.py</a></p>236<hr class="docutils" />237<section id="introduction">238<h2>Introduction<a class="headerlink" href="#introduction" title="Link to this heading">¶</a></h2>239<p>The purpose of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code> package is three-fold.</p>240<p>One is to provide the241implementation 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, the242<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 an243implementation of <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code> which is portable to any Python244interpreter. This also provides an implementation which is easier to245comprehend than one implemented in a programming language other than Python.</p>246<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 this247package, making it easier for users to create their own custom objects (known248generically 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>249<p>Three, the package contains modules exposing additional functionality for250managing aspects of Python packages:</p>251<ul class="simple">252<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-party253distributions.</p></li>254<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-code255“resources” from Python packages.</p></li>256</ul>257<div class="admonition seealso">258<p class="admonition-title">See also</p>259<dl class="simple">260<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>261</dd>262<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 since263the writing of this document (e.g. redirecting based on <code class="docutils literal notranslate"><span class="pre">None</span></code>264in <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>265</dd>266<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>267</dd>268<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>269</dd>270<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>271</dd>272<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>273</dd>274<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>275</dd>276<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>277</dd>278<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>279</dd>280<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>281</dd>282<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>283</dd>284<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>285</dd>286<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>287</dd>288<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>289</dd>290<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>291</dd>292<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>293</dd>294</dl>295</div>296</section>297<section id="functions">298<h2>Functions<a class="headerlink" href="#functions" title="Link to this heading">¶</a></h2>299<dl class="py function">300<dt class="sig sig-object py" id="importlib.__import__">301<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>302<dd><p>An implementation of the built-in <code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code> function.</p>303<div class="admonition note">304<p class="admonition-title">Note</p>305<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>306instead of this function.</p>307</div>308</dd></dl>309 310<dl class="py function">311<dt class="sig sig-object py" id="importlib.import_module">312<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>313<dd><p>Import a module. The <em>name</em> argument specifies what module to314import in absolute or relative terms315(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 is316specified in relative terms, then the <em>package</em> argument must be set to317the name of the package which is to act as the anchor for resolving the318package name (e.g. <code class="docutils literal notranslate"><span class="pre">import_module('..mod',</span> <span class="pre">'pkg.subpkg')</span></code> will import319<code class="docutils literal notranslate"><span class="pre">pkg.mod</span></code>).</p>320<p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">import_module()</span></code> function acts as a simplifying wrapper around321<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 are322derived from <code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.__import__()</span></code>. The most important difference323between these two functions is that <code class="xref py py-func docutils literal notranslate"><span class="pre">import_module()</span></code> returns the324specified 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>325returns the top-level package or module (e.g. <code class="docutils literal notranslate"><span class="pre">pkg</span></code>).</p>326<p>If you are dynamically importing a module that was created since the327interpreter began execution (e.g., created a Python source file), you may328need 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 be329noticed by the import system.</p>330<p>If the module cannot be imported, <code class="xref py py-func docutils literal notranslate"><span class="pre">import_module()</span></code> will raise331<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> or an appropriate subclass like332<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>.</p>333<div class="versionchanged">334<p><span class="versionmodified changed">Changed in version 3.3: </span>Parent packages are automatically imported.</p>335</div>336</dd></dl>337 338<dl class="py function">339<dt class="sig sig-object py" id="importlib.invalidate_caches">340<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>341<dd><p>Invalidate the internal caches of finders stored at342<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 it343will be called to perform the invalidation. This function should be called344if any modules are created/installed while your program is running to345guarantee all finders will notice the new module’s existence.</p>346<div class="versionadded">347<p><span class="versionmodified added">Added in version 3.3.</span></p>348</div>349<div class="versionchanged">350<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>351location after the same namespace was already imported are noticed.</p>352</div>353</dd></dl>354 355<dl class="py function">356<dt class="sig sig-object py" id="importlib.reload">357<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>358<dd><p>Reload a previously imported <em>module</em>. The argument must be a module object,359so it must have been successfully imported before. This is useful if you360have edited the module source file using an external editor and want to try361out the new version without leaving the Python interpreter. The return value362is the module object (which can be different if re-importing causes a363different 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>364<p>When <code class="xref py py-func docutils literal notranslate"><span class="pre">reload()</span></code> is executed:</p>365<ul class="simple">366<li><p>Python module’s code is recompiled and the module-level code re-executed,367defining a new set of objects which are bound to names in the module’s368dictionary by reusing the <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> which originally loaded the369module. The <code class="docutils literal notranslate"><span class="pre">init</span></code> function of extension modules is not called a second370time.</p></li>371<li><p>As with all other objects in Python the old objects are only reclaimed372after their reference counts drop to zero.</p></li>373<li><p>The names in the module namespace are updated to point to any new or374changed objects.</p></li>375<li><p>Other references to the old objects (such as names external to the module) are376not rebound to refer to the new objects and must be updated in each namespace377where they occur if that is desired.</p></li>378</ul>379<p>There are a number of other caveats:</p>380<p>When a module is reloaded, its dictionary (containing the module’s global381variables) is retained. Redefinitions of names will override the old382definitions, so this is generally not a problem. If the new version of a383module does not define a name that was defined by the old version, the old384definition remains. This feature can be used to the module’s advantage if it385maintains 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>386statement it can test for the table’s presence and skip its initialization if387desired:</p>388<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>389 <span class="n">cache</span>390<span class="k">except</span> <span class="ne">NameError</span><span class="p">:</span>391 <span class="n">cache</span> <span class="o">=</span> <span class="p">{}</span>392</pre></div>393</div>394<p>It is generally not very useful to reload built-in or dynamically loaded395modules. 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 other396key modules is not recommended. In many cases extension modules are not397designed to be initialized more than once, and may fail in arbitrary ways398when reloaded.</p>399<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> …400<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 <code class="xref py py-func docutils literal notranslate"><span class="pre">reload()</span></code> for the other module does not401redefine the objects imported from it — one way around this is to402re-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>403and qualified names (<em>module.name</em>) instead.</p>404<p>If a module instantiates instances of a class, reloading the module that405defines the class does not affect the method definitions of the instances —406they continue to use the old class definition. The same is true for derived407classes.</p>408<div class="versionadded">409<p><span class="versionmodified added">Added in version 3.4.</span></p>410</div>411<div class="versionchanged">412<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 lacks413a <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>414</div>415<div class="admonition warning">416<p class="admonition-title">Warning</p>417<p>This function is not thread-safe. Calling it from multiple threads can result418in unexpected behavior. It’s recommended to use the <a class="reference internal" href="threading.html#threading.Lock" title="threading.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Lock</span></code></a>419or other synchronization primitives for thread-safe module reloading.</p>420</div>421</dd></dl>422 423</section>424<section id="module-importlib.abc">425<span id="importlib-abc-abstract-base-classes-related-to-import"></span><h2><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.abc</span></code> – Abstract base classes related to import<a class="headerlink" href="#module-importlib.abc" title="Link to this heading">¶</a></h2>426<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/importlib/abc.py">Lib/importlib/abc.py</a></p>427<hr class="docutils" />428<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.abc</span></code> module contains all of the core abstract base classes429used 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 classes430are also provided to help in implementing the core ABCs.</p>431<p>ABC hierarchy:</p>432<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">object</span>433 <span class="o">+--</span> <span class="n">MetaPathFinder</span>434 <span class="o">+--</span> <span class="n">PathEntryFinder</span>435 <span class="o">+--</span> <span class="n">Loader</span>436 <span class="o">+--</span> <span class="n">ResourceLoader</span> <span class="o">--------+</span>437 <span class="o">+--</span> <span class="n">InspectLoader</span> <span class="o">|</span>438 <span class="o">+--</span> <span class="n">ExecutionLoader</span> <span class="o">--+</span>439 <span class="o">+--</span> <span class="n">FileLoader</span>440 <span class="o">+--</span> <span class="n">SourceLoader</span>441</pre></div>442</div>443<dl class="py class">444<dt class="sig sig-object py" id="importlib.abc.MetaPathFinder">445<em class="property"><span class="k"><span class="pre">class</span></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>446<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>447<div class="versionadded">448<p><span class="versionmodified added">Added in version 3.3.</span></p>449</div>450<div class="versionchanged">451<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>452</div>453<dl class="py method">454<dt class="sig sig-object py" id="importlib.abc.MetaPathFinder.find_spec">455<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>456<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> for457the specified module. If this is a top-level import, <em>path</em> will458be <code class="docutils literal notranslate"><span class="pre">None</span></code>. Otherwise, this is a search for a subpackage or459module and <em>path</em> will be the value of <a class="reference internal" href="../reference/datamodel.html#module.__path__" title="module.__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__path__</span></code></a> from the460parent package. If a spec cannot be found, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned.461When passed in, <code class="docutils literal notranslate"><span class="pre">target</span></code> is a module object that the finder may462use to make a more educated guess about what spec to return.463<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 implementing464concrete <code class="docutils literal notranslate"><span class="pre">MetaPathFinders</span></code>.</p>465<div class="versionadded">466<p><span class="versionmodified added">Added in version 3.4.</span></p>467</div>468</dd></dl>469 470<dl class="py method">471<dt class="sig sig-object py" id="importlib.abc.MetaPathFinder.invalidate_caches">472<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>473<dd><p>An optional method which, when called, should invalidate any internal474cache 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>475when 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>476<div class="versionchanged">477<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>478</div>479</dd></dl>480 481<dl class="py method">482<dt class="sig sig-object py" id="importlib.abc.MetaPathFinder.discover">483<span class="sig-name descname"><span class="pre">discover</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">parent</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.discover" title="Link to this definition">¶</a></dt>484<dd><p>An optional method which searches for possible specs with given <em>parent</em>485module spec. If <em>parent</em> is <em>None</em>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">MetaPathFinder.discover()</span></code> will486search for top-level modules.</p>487<p>Returns an iterable of possible specs.</p>488<p>Raises <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> if <em>parent</em> is not a package module.</p>489<div class="admonition warning">490<p class="admonition-title">Warning</p>491<p>This method can potentially yield a very large number of objects, and492it may carry out IO operations when computing these values.</p>493<p>Because of this, it will generaly be desirable to compute the result494values on-the-fly, as they are needed. As such, the returned object is495only guaranteed to be an <a class="reference internal" href="collections.abc.html#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal notranslate"><span class="pre">iterable</span></code></a>,496instead of a <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> or other497<a class="reference internal" href="collections.abc.html#collections.abc.Collection" title="collections.abc.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">collection</span></code></a> type.</p>498</div>499<div class="versionadded">500<p><span class="versionmodified added">Added in version 3.15.0a6 (unreleased).</span></p>501</div>502</dd></dl>503 504</dd></dl>505 506<dl class="py class">507<dt class="sig sig-object py" id="importlib.abc.PathEntryFinder">508<em class="property"><span class="k"><span class="pre">class</span></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>509<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>. Though510it 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>511is meant for use only within the path-based import subsystem provided512by <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>513<div class="versionadded">514<p><span class="versionmodified added">Added in version 3.3.</span></p>515</div>516<div class="versionchanged">517<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>518</div>519<dl class="py method">520<dt class="sig sig-object py" id="importlib.abc.PathEntryFinder.find_spec">521<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>522<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> for523the specified module. The finder will search for the module only524within 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 spec525cannot 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>526is a module object that the finder may use to make a more educated527guess 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>528may be useful for implementing concrete <code class="docutils literal notranslate"><span class="pre">PathEntryFinders</span></code>.</p>529<div class="versionadded">530<p><span class="versionmodified added">Added in version 3.4.</span></p>531</div>532</dd></dl>533 534<dl class="py method">535<dt class="sig sig-object py" id="importlib.abc.PathEntryFinder.invalidate_caches">536<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>537<dd><p>An optional method which, when called, should invalidate any internal538cache used by the finder. Used by539<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>540when invalidating the caches of all cached finders.</p>541</dd></dl>542 543<dl class="py method">544<dt class="sig sig-object py" id="importlib.abc.PathEntryFinder.discover">545<span class="sig-name descname"><span class="pre">discover</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">parent</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.discover" title="Link to this definition">¶</a></dt>546<dd><p>An optional method which searches for possible specs with given <em>parent</em>547module spec. If <em>parent</em> is <em>None</em>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">PathEntryFinder.discover()</span></code> will548search for top-level modules.</p>549<p>Returns an iterable of possible specs.</p>550<p>Raises <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> if <em>parent</em> is not a package module.</p>551<div class="admonition warning">552<p class="admonition-title">Warning</p>553<p>This method can potentially yield a very large number of objects, and554it may carry out IO operations when computing these values.</p>555<p>Because of this, it will generaly be desirable to compute the result556values on-the-fly, as they are needed. As such, the returned object is557only guaranteed to be an <a class="reference internal" href="collections.abc.html#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal notranslate"><span class="pre">iterable</span></code></a>,558instead of a <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> or other559<a class="reference internal" href="collections.abc.html#collections.abc.Collection" title="collections.abc.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">collection</span></code></a> type.</p>560</div>561<div class="versionadded">562<p><span class="versionmodified added">Added in version 3.15.0a6 (unreleased).</span></p>563</div>564</dd></dl>565 566</dd></dl>567 568<dl class="py class">569<dt class="sig sig-object py" id="importlib.abc.Loader">570<em class="property"><span class="k"><span class="pre">class</span></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>571<dd><blockquote>572<div><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>.573See <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>574<p>Loaders that wish to support resource reading should implement a575<code class="xref py py-meth docutils literal notranslate"><span class="pre">get_resource_reader()</span></code> method as specified by576<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>577<div class="versionchanged">578<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>579</div>580</div></blockquote>581<div class="versionchanged">582<p><span class="versionmodified changed">Changed in version 3.15: </span>Removed the <code class="docutils literal notranslate"><span class="pre">load_module()</span></code> method.</p>583<dl class="py method">584<dt class="sig sig-object py" id="importlib.abc.Loader.create_module">585<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>586<dd><p>A method that returns the module object to use when587importing a module. This method may return <code class="docutils literal notranslate"><span class="pre">None</span></code>,588indicating that default module creation semantics should take place.</p>589<div class="versionadded">590<p><span class="versionmodified added">Added in version 3.4.</span></p>591</div>592<div class="versionchanged">593<p><span class="versionmodified changed">Changed in version 3.6: </span>This method is no longer optional when594<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>595</div>596</dd></dl>597 598<dl class="py method">599<dt class="sig sig-object py" id="importlib.abc.Loader.exec_module">600<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>601<dd><p>An abstract method that executes the module in its own namespace602when a module is imported or reloaded. The module should already603be initialized when <code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code> is called. When this method exists,604<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>605<div class="versionadded">606<p><span class="versionmodified added">Added in version 3.4.</span></p>607</div>608<div class="versionchanged">609<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>610</div>611</dd></dl>612 613</div>614</dd></dl>615 616<dl class="py class">617<dt class="sig sig-object py" id="importlib.abc.ResourceLoader">618<em class="property"><span class="k"><span class="pre">class</span></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>619<dd><p><em>Superseded by TraversableResources</em></p>620<blockquote>621<div><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 optional622<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 storage623back-end.</p>624<div class="deprecated">625<p><span class="versionmodified deprecated">Deprecated since version 3.7: </span>This ABC is deprecated in favour of supporting resource loading626through <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>.627This class exists for backwards compatibility only with other ABCs in628this module.</p>629</div>630<dl class="py method">631<dt class="sig sig-object py" id="importlib.abc.ResourceLoader.get_data">632<em class="property"><span class="k"><span class="pre">abstractmethod</span></span><span class="w"> </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>633<dd><blockquote>634<div><p>An abstract method to return the bytes for the data located at <em>path</em>.635Loaders that have a file-like storage back-end636that allows storing arbitrary data637can implement this abstract method to give direct access638to 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> cannot639be found. The <em>path</em> is expected to be constructed using a module’s640<a class="reference internal" href="../reference/datamodel.html#module.__file__" title="module.__file__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__file__</span></code></a> attribute or an item from a package’s641<a class="reference internal" href="../reference/datamodel.html#module.__path__" title="module.__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__path__</span></code></a>.</p>642<div class="versionchanged">643<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>644</div>645</div></blockquote>646</dd></dl>647 648</div></blockquote>649</dd></dl>650 651<dl class="py class">652<dt class="sig sig-object py" id="importlib.abc.InspectLoader">653<em class="property"><span class="k"><span class="pre">class</span></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>654<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 optional655<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>656<dl class="py method">657<dt class="sig sig-object py" id="importlib.abc.InspectLoader.get_code">658<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>659<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 not660have a code object (as would be the case, for example, for a built-in661module). 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 the662requested module.</p>663<div class="admonition note">664<p class="admonition-title">Note</p>665<p>While the method has a default implementation, it is suggested that666it be overridden if possible for performance.</p>667</div>668<div class="versionchanged" id="index-15">669<p><span class="versionmodified changed">Changed in version 3.4: </span>No longer abstract and a concrete implementation is provided.</p>670</div>671</dd></dl>672 673<dl class="py method">674<dt class="sig sig-object py" id="importlib.abc.InspectLoader.get_source">675<em class="property"><span class="k"><span class="pre">abstractmethod</span></span><span class="w"> </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>676<dd><blockquote>677<div><p>An abstract method to return the source of a module. It is returned as678a text string using <a class="reference internal" href="../glossary.html#term-universal-newlines"><span class="xref std std-term">universal newlines</span></a>, translating all679recognized 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>680if no source is available (e.g. a built-in module). Raises681<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>682<div class="versionchanged">683<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>684</div>685</div></blockquote>686</dd></dl>687 688<dl class="py method">689<dt class="sig sig-object py" id="importlib.abc.InspectLoader.is_package">690<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>691<dd><p>An optional method to return a true value if the module is a package, a692false 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 the693<a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> cannot find the module.</p>694<div class="versionchanged">695<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>696</div>697</dd></dl>698 699<dl class="py method">700<dt class="sig sig-object py" id="importlib.abc.InspectLoader.source_to_code">701<em class="property"><span class="k"><span class="pre">static</span></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">'<string>'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fullname</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.InspectLoader.source_to_code" title="Link to this definition">¶</a></dt>702<dd><p>Create a code object from Python source.</p>703<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> function704supports (i.e. string or bytes). The <em>path</em> argument should be705the “path” to where the source code originated from, which can be an706abstract concept (e.g. location in a zip file).</p>707<p>With the subsequent code object one can execute it in a module by708running <code class="docutils literal notranslate"><span class="pre">exec(code,</span> <span class="pre">module.__dict__)</span></code>.</p>709<p>The optional argument <em>fullname</em> specifies the module name.710It is needed to unambiguous <a class="reference internal" href="warnings.html#warning-filter"><span class="std std-ref">filter</span></a> syntax711warnings by module name.</p>712<div class="versionadded">713<p><span class="versionmodified added">Added in version 3.4.</span></p>714</div>715<div class="versionchanged">716<p><span class="versionmodified changed">Changed in version 3.5: </span>Made the method static.</p>717</div>718<div class="versionadded">719<p><span class="versionmodified added">Added in version 3.15: </span>Added the <em>fullname</em> parameter.</p>720</div>721</dd></dl>722 723<dl class="py method">724<dt class="sig sig-object py" id="importlib.abc.InspectLoader.exec_module">725<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>726<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>727<div class="versionadded">728<p><span class="versionmodified added">Added in version 3.4.</span></p>729</div>730</dd></dl>731 732</dd></dl>733 734<dl class="py class">735<dt class="sig sig-object py" id="importlib.abc.ExecutionLoader">736<em class="property"><span class="k"><span class="pre">class</span></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>737<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,738when implemented, helps a module to be executed as a script. The ABC739represents 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>740<dl class="py method">741<dt class="sig sig-object py" id="importlib.abc.ExecutionLoader.get_filename">742<em class="property"><span class="k"><span class="pre">abstractmethod</span></span><span class="w"> </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>743<dd><blockquote>744<div><p>An abstract method that is to return the value of745<a class="reference internal" href="../reference/datamodel.html#module.__file__" title="module.__file__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__file__</span></code></a> for the specified module. If no path is746available, <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>747<p>If source code is available, then the method should return the path to748the source file, regardless of whether a bytecode was used to load the749module.</p>750<div class="versionchanged">751<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>752</div>753</div></blockquote>754</dd></dl>755 756</dd></dl>757 758<dl class="py class">759<dt class="sig sig-object py" id="importlib.abc.FileLoader">760<em class="property"><span class="k"><span class="pre">class</span></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>761<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> and762<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 of763<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>764<p>The <em>fullname</em> argument is a fully resolved name of the module the loader is765to handle. The <em>path</em> argument is the path to the file for the module.</p>766<div class="versionadded">767<p><span class="versionmodified added">Added in version 3.3.</span></p>768</div>769<div class="versionchanged">770<p><span class="versionmodified changed">Changed in version 3.15: </span>Removed the <code class="docutils literal notranslate"><span class="pre">load_module()</span></code> method.</p>771</div>772<dl class="py attribute">773<dt class="sig sig-object py" id="importlib.abc.FileLoader.name">774<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>775<dd><p>The name of the module the loader can handle.</p>776</dd></dl>777 778<dl class="py attribute">779<dt class="sig sig-object py" id="importlib.abc.FileLoader.path">780<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>781<dd><p>Path to the file of the module.</p>782</dd></dl>783 784<dl class="py method">785<dt class="sig sig-object py" id="importlib.abc.FileLoader.get_filename">786<em class="property"><span class="k"><span class="pre">abstractmethod</span></span><span class="w"> </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>787<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>788</dd></dl>789 790<dl class="py method">791<dt class="sig sig-object py" id="importlib.abc.FileLoader.get_data">792<em class="property"><span class="k"><span class="pre">abstractmethod</span></span><span class="w"> </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>793<dd><p>Reads <em>path</em> as a binary file and returns the bytes from it.</p>794</dd></dl>795 796</dd></dl>797 798<dl class="py class">799<dt class="sig sig-object py" id="importlib.abc.SourceLoader">800<em class="property"><span class="k"><span class="pre">class</span></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>801<dd><p>An abstract base class for implementing source (and optionally bytecode)802file 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> and803<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>804<ul class="simple">805<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>806<li><dl class="simple">807<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; sourceless808loading is not supported.</p>809</dd>810</dl>811</li>812</ul>813<p>The abstract methods defined by this class are to add optional bytecode814file support. Not implementing these optional methods (or causing them to815raise <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 to816only work with source code. Implementing the methods allows the loader to817work with source <em>and</em> bytecode files; it does not allow for <em>sourceless</em>818loading where only bytecode is provided. Bytecode files are an819optimization to speed up loading by removing the parsing step of Python’s820compiler, and so no bytecode-specific API is exposed.</p>821<div class="versionchanged">822<p><span class="versionmodified changed">Changed in version 3.15: </span>Removed the <code class="docutils literal notranslate"><span class="pre">load_module()</span></code> method.</p>823</div>824<dl class="py method">825<dt class="sig sig-object py" id="importlib.abc.SourceLoader.path_stats">826<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>827<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> containing828metadata about the specified path. Supported dictionary keys are:</p>829<ul class="simple">830<li><p><code class="docutils literal notranslate"><span class="pre">'mtime'</span></code> (mandatory): an integer or floating-point number831representing the modification time of the source code;</p></li>832<li><p><code class="docutils literal notranslate"><span class="pre">'size'</span></code> (optional): the size in bytes of the source code.</p></li>833</ul>834<p>Any other keys in the dictionary are ignored, to allow for future835extensions. 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>836<div class="versionadded">837<p><span class="versionmodified added">Added in version 3.3.</span></p>838</div>839<div class="versionchanged">840<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>841</div>842</dd></dl>843 844<dl class="py method">845<dt class="sig sig-object py" id="importlib.abc.SourceLoader.path_mtime">846<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>847<dd><p>Optional abstract method which returns the modification time for the848specified path.</p>849<div class="deprecated">850<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’t851have to implement it, but it is still available for compatibility852purposes. 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>853</div>854<div class="versionchanged">855<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>856</div>857</dd></dl>858 859<dl class="py method">860<dt class="sig sig-object py" id="importlib.abc.SourceLoader.set_data">861<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>862<dd><p>Optional abstract method which writes the specified bytes to a file863path. Any intermediate directories which do not exist are to be created864automatically.</p>865<p>When writing to the path fails because the path is read-only866(<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 the867exception.</p>868<div class="versionchanged">869<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>870</div>871</dd></dl>872 873<dl class="py method">874<dt class="sig sig-object py" id="importlib.abc.SourceLoader.get_code">875<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>876<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>877</dd></dl>878 879<dl class="py method">880<dt class="sig sig-object py" id="importlib.abc.SourceLoader.exec_module">881<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>882<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>883<div class="versionadded">884<p><span class="versionmodified added">Added in version 3.4.</span></p>885</div>886</dd></dl>887 888<dl class="py method">889<dt class="sig sig-object py" id="importlib.abc.SourceLoader.get_source">890<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>891<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>892</dd></dl>893 894<dl class="py method">895<dt class="sig sig-object py" id="importlib.abc.SourceLoader.is_package">896<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>897<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 module898is determined to be a package if its file path (as provided by899<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 named900<code class="docutils literal notranslate"><span class="pre">__init__</span></code> when the file extension is removed <strong>and</strong> the module name901itself does not end in <code class="docutils literal notranslate"><span class="pre">__init__</span></code>.</p>902</dd></dl>903 904</dd></dl>905 906</section>907<section id="module-importlib.machinery">908<span id="importlib-machinery-importers-and-path-hooks"></span><h2><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.machinery</span></code> – Importers and path hooks<a class="headerlink" href="#module-importlib.machinery" title="Link to this heading">¶</a></h2>909<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/importlib/machinery.py">Lib/importlib/machinery.py</a></p>910<hr class="docutils" />911<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>912find and load modules.</p>913<dl class="py data">914<dt class="sig sig-object py" id="importlib.machinery.SOURCE_SUFFIXES">915<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>916<dd><p>A list of strings representing the recognized file suffixes for source917modules.</p>918<div class="versionadded">919<p><span class="versionmodified added">Added in version 3.3.</span></p>920</div>921</dd></dl>922 923<dl class="py data">924<dt class="sig sig-object py" id="importlib.machinery.DEBUG_BYTECODE_SUFFIXES">925<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>926<dd><p>A list of strings representing the file suffixes for non-optimized bytecode927modules.</p>928<div class="versionadded">929<p><span class="versionmodified added">Added in version 3.3.</span></p>930</div>931<div class="deprecated">932<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-const docutils literal notranslate"><span class="pre">BYTECODE_SUFFIXES</span></code></a> instead.</p>933</div>934</dd></dl>935 936<dl class="py data">937<dt class="sig sig-object py" id="importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES">938<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>939<dd><p>A list of strings representing the file suffixes for optimized bytecode940modules.</p>941<div class="versionadded">942<p><span class="versionmodified added">Added in version 3.3.</span></p>943</div>944<div class="deprecated">945<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-const docutils literal notranslate"><span class="pre">BYTECODE_SUFFIXES</span></code></a> instead.</p>946</div>947</dd></dl>948 949<dl class="py data">950<dt class="sig sig-object py" id="importlib.machinery.BYTECODE_SUFFIXES">951<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>952<dd><p>A list of strings representing the recognized file suffixes for bytecode953modules (including the leading dot).</p>954<div class="versionadded">955<p><span class="versionmodified added">Added in version 3.3.</span></p>956</div>957<div class="versionchanged">958<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>959</div>960</dd></dl>961 962<dl class="py data">963<dt class="sig sig-object py" id="importlib.machinery.EXTENSION_SUFFIXES">964<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>965<dd><p>A list of strings representing the recognized file suffixes for966extension modules.</p>967<div class="versionadded">968<p><span class="versionmodified added">Added in version 3.3.</span></p>969</div>970</dd></dl>971 972<dl class="py function">973<dt class="sig sig-object py" id="importlib.machinery.all_suffixes">974<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>975<dd><p>Returns a combined list of strings representing all file suffixes for976modules recognized by the standard import machinery. This is a977helper for code which simply needs to know if a filesystem path978potentially refers to a module without needing any details on the kind979of 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>980<div class="versionadded">981<p><span class="versionmodified added">Added in version 3.3.</span></p>982</div>983</dd></dl>984 985<dl class="py class">986<dt class="sig sig-object py" id="importlib.machinery.BuiltinImporter">987<em class="property"><span class="k"><span class="pre">class</span></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>988<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 are989listed 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 the990<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> and991<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>992<p>Only class methods are defined by this class to alleviate the need for993instantiation.</p>994<div class="versionchanged">995<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 implements996<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>997</div>998</dd></dl>999 1000<dl class="py class">1001<dt class="sig sig-object py" id="importlib.machinery.FrozenImporter">1002<em class="property"><span class="k"><span class="pre">class</span></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>1003<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 the1004<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> and1005<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>1006<p>Only class methods are defined by this class to alleviate the need for1007instantiation.</p>1008<div class="versionchanged">1009<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>1010methods.</p>1011</div>1012</dd></dl>1013 1014<dl class="py class">1015<dt class="sig sig-object py" id="importlib.machinery.WindowsRegistryFinder">1016<em class="property"><span class="k"><span class="pre">class</span></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>1017<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 class1018implements 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>1019<p>Only class methods are defined by this class to alleviate the need for1020instantiation.</p>1021<div class="versionadded">1022<p><span class="versionmodified added">Added in version 3.3.</span></p>1023</div>1024<div class="deprecated">1025<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 may1026not enable this finder by default.</p>1027</div>1028</dd></dl>1029 1030<dl class="py class">1031<dt class="sig sig-object py" id="importlib.machinery.PathFinder">1032<em class="property"><span class="k"><span class="pre">class</span></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>1033<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.1034This 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>1035<p>Only class methods are defined by this class to alleviate the need for1036instantiation.</p>1037<dl class="py method">1038<dt class="sig sig-object py" id="importlib.machinery.PathFinder.find_spec">1039<em class="property"><span class="k"><span class="pre">classmethod</span></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>1040<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>1041for 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, if1042defined, on <em>path</em>. For each path entry that is searched,1043<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 object1044is 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 look1045for the module being searched for. If no entry is found in1046<code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code>, 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> is1047searched for a finder for the path entry and, if found, is stored1048in <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code> along with being queried about1049the module. If no finder is ever found then <code class="docutils literal notranslate"><span class="pre">None</span></code> is both1050stored in the cache and returned.</p>1051<div class="versionadded">1052<p><span class="versionmodified added">Added in version 3.4.</span></p>1053</div>1054<div class="versionchanged">1055<p><span class="versionmodified changed">Changed in version 3.5: </span>If the current working directory – represented by an empty string –1056is no longer valid then <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned but no value is cached1057in <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>1058</div>1059</dd></dl>1060 1061<dl class="py method">1062<dt class="sig sig-object py" id="importlib.machinery.PathFinder.invalidate_caches">1063<em class="property"><span class="k"><span class="pre">classmethod</span></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>1064<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 all1065finders 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.1066Otherwise entries in <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code> set to <code class="docutils literal notranslate"><span class="pre">None</span></code> are1067deleted.</p>1068<div class="versionchanged">1069<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>1070</div>1071</dd></dl>1072 1073<div class="versionchanged">1074<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 working1075directory for <code class="docutils literal notranslate"><span class="pre">''</span></code> (i.e. the empty string).</p>1076</div>1077</dd></dl>1078 1079<dl class="py class">1080<dt class="sig sig-object py" id="importlib.machinery.FileFinder">1081<em class="property"><span class="k"><span class="pre">class</span></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>1082<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> which1083caches results from the file system.</p>1084<p>The <em>path</em> argument is the directory for which the finder is in charge of1085searching.</p>1086<p>The <em>loader_details</em> argument is a variable number of 2-item tuples each1087containing a loader and a sequence of file suffixes the loader recognizes.1088The loaders are expected to be callables which accept two arguments of1089the module’s name and the path to the file found.</p>1090<p>The finder will cache the directory contents as necessary, making stat calls1091for each module search to verify the cache is not outdated. Because cache1092staleness relies upon the granularity of the operating system’s state1093information of the file system, there is a potential race condition of1094searching for a module, creating a new file, and then searching for the1095module the new file represents. If the operations happen fast enough to fit1096within the granularity of stat calls, then the module search will fail. To1097prevent this from happening, when you create a module dynamically, make sure1098to 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>1099<div class="versionadded">1100<p><span class="versionmodified added">Added in version 3.3.</span></p>1101</div>1102<dl class="py attribute">1103<dt class="sig sig-object py" id="importlib.machinery.FileFinder.path">1104<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>1105<dd><p>The path the finder will search in.</p>1106</dd></dl>1107 1108<dl class="py method">1109<dt class="sig sig-object py" id="importlib.machinery.FileFinder.find_spec">1110<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>1111<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>1112<div class="versionadded">1113<p><span class="versionmodified added">Added in version 3.4.</span></p>1114</div>1115</dd></dl>1116 1117<dl class="py method">1118<dt class="sig sig-object py" id="importlib.machinery.FileFinder.invalidate_caches">1119<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>1120<dd><p>Clear out the internal cache.</p>1121</dd></dl>1122 1123<dl class="py method">1124<dt class="sig sig-object py" id="importlib.machinery.FileFinder.path_hook">1125<em class="property"><span class="k"><span class="pre">classmethod</span></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>1126<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>.1127An instance of <code class="xref py py-class docutils literal notranslate"><span class="pre">FileFinder</span></code> is returned by the closure using the1128path argument given to the closure directly and <em>loader_details</em>1129indirectly.</p>1130<p>If the argument to the closure is not an existing directory,1131<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>1132</dd></dl>1133 1134</dd></dl>1135 1136<dl class="py class">1137<dt class="sig sig-object py" id="importlib.machinery.NamespacePath">1138<em class="property"><span class="k"><span class="pre">class</span></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">NamespacePath</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.NamespacePath" title="Link to this definition">¶</a></dt>1139<dd><p>Represents a <a class="reference internal" href="../glossary.html#term-namespace-package"><span class="xref std std-term">namespace package</span></a>’s path (<a class="reference internal" href="../reference/datamodel.html#module.__path__" title="module.__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">module.__path__</span></code></a>).</p>1140<p>When its <code class="docutils literal notranslate"><span class="pre">__path__</span></code> value is accessed it will be recomputed if necessary.1141This keeps it in-sync with the global state (<a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.modules</span></code></a>).</p>1142<p>The <em>name</em> argument is the name of the namespace module.</p>1143<p>The <em>path</em> argument is the initial path value.</p>1144<p>The <em>path_finder</em> argument is the callable used to recompute the path value.1145The callable has the same signature as <a class="reference internal" href="#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder.find_spec()</span></code></a>.</p>1146<p>When the parent’s <a class="reference internal" href="../reference/datamodel.html#module.__path__" title="module.__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">module.__path__</span></code></a> attribute is updated, the path1147value is recomputed.</p>1148<p>If the parent module is missing 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>, then1149<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> will be raised.</p>1150<p>For top-level modules, the parent module’s path is <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>1151<div class="admonition note">1152<p class="admonition-title">Note</p>1153<p><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">PathFinder.invalidate_caches()</span></code></a> invalidates <code class="xref py py-class docutils literal notranslate"><span class="pre">NamespacePath</span></code>,1154forcing the path value to be recomputed next time it is accessed.</p>1155</div>1156<div class="versionadded">1157<p><span class="versionmodified added">Added in version 3.15.</span></p>1158</div>1159</dd></dl>1160 1161<dl class="py class">1162<dt class="sig sig-object py" id="importlib.machinery.SourceFileLoader">1163<em class="property"><span class="k"><span class="pre">class</span></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>1164<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> by1165subclassing <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 concrete1166implementations of other methods.</p>1167<div class="versionadded">1168<p><span class="versionmodified added">Added in version 3.3.</span></p>1169</div>1170<div class="versionchanged">1171<p><span class="versionmodified changed">Changed in version 3.15: </span>Removed the <code class="docutils literal notranslate"><span class="pre">load_module()</span></code> method.</p>1172</div>1173<dl class="py attribute">1174<dt class="sig sig-object py" id="importlib.machinery.SourceFileLoader.name">1175<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>1176<dd><p>The name of the module that this loader will handle.</p>1177</dd></dl>1178 1179<dl class="py attribute">1180<dt class="sig sig-object py" id="importlib.machinery.SourceFileLoader.path">1181<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>1182<dd><p>The path to the source file.</p>1183</dd></dl>1184 1185<dl class="py method">1186<dt class="sig sig-object py" id="importlib.machinery.SourceFileLoader.is_package">1187<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>1188<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>1189</dd></dl>1190 1191<dl class="py method">1192<dt class="sig sig-object py" id="importlib.machinery.SourceFileLoader.path_stats">1193<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>1194<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>1195</dd></dl>1196 1197<dl class="py method">1198<dt class="sig sig-object py" id="importlib.machinery.SourceFileLoader.set_data">1199<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>1200<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>