gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1309 lines
128KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="5. The import system" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/reference/import.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the ..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the ..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>5. The import system &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="6. Expressions" href="expressions.html" />
  33. <link rel="prev" title="4. Execution model" href="executionmodel.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/reference/import.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">5. The import system</a><ul>
  86. <li><a class="reference internal" href="#importlib">5.1. <code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a></li>
  87. <li><a class="reference internal" href="#packages">5.2. Packages</a><ul>
  88. <li><a class="reference internal" href="#regular-packages">5.2.1. Regular packages</a></li>
  89. <li><a class="reference internal" href="#namespace-packages">5.2.2. Namespace packages</a></li>
  90. </ul>
  91. </li>
  92. <li><a class="reference internal" href="#searching">5.3. Searching</a><ul>
  93. <li><a class="reference internal" href="#the-module-cache">5.3.1. The module cache</a></li>
  94. <li><a class="reference internal" href="#finders-and-loaders">5.3.2. Finders and loaders</a></li>
  95. <li><a class="reference internal" href="#import-hooks">5.3.3. Import hooks</a></li>
  96. <li><a class="reference internal" href="#the-meta-path">5.3.4. The meta path</a></li>
  97. </ul>
  98. </li>
  99. <li><a class="reference internal" href="#loading">5.4. Loading</a><ul>
  100. <li><a class="reference internal" href="#loaders">5.4.1. Loaders</a></li>
  101. <li><a class="reference internal" href="#submodules">5.4.2. Submodules</a></li>
  102. <li><a class="reference internal" href="#module-spec">5.4.3. Module spec</a></li>
  103. <li><a class="reference internal" href="#import-related-module-attributes">5.4.4. Import-related module attributes</a></li>
  104. <li><a class="reference internal" href="#module-path">5.4.5. module.__path__</a></li>
  105. <li><a class="reference internal" href="#module-reprs">5.4.6. Module reprs</a></li>
  106. <li><a class="reference internal" href="#cached-bytecode-invalidation">5.4.7. Cached bytecode invalidation</a></li>
  107. </ul>
  108. </li>
  109. <li><a class="reference internal" href="#the-path-based-finder">5.5. The Path Based Finder</a><ul>
  110. <li><a class="reference internal" href="#path-entry-finders">5.5.1. Path entry finders</a></li>
  111. <li><a class="reference internal" href="#path-entry-finder-protocol">5.5.2. Path entry finder protocol</a></li>
  112. </ul>
  113. </li>
  114. <li><a class="reference internal" href="#replacing-the-standard-import-system">5.6. Replacing the standard import system</a></li>
  115. <li><a class="reference internal" href="#package-relative-imports">5.7. Package Relative Imports</a></li>
  116. <li><a class="reference internal" href="#special-considerations-for-main">5.8. Special considerations for __main__</a><ul>
  117. <li><a class="reference internal" href="#main-spec">5.8.1. __main__.__spec__</a></li>
  118. </ul>
  119. </li>
  120. <li><a class="reference internal" href="#references">5.9. References</a></li>
  121. </ul>
  122. </li>
  123. </ul>
  124. </div>
  125. <div>
  126. <h4>Previous topic</h4>
  127. <p class="topless"><a href="executionmodel.html"
  128. title="previous chapter"><span class="section-number">4. </span>Execution model</a></p>
  129. </div>
  130. <div>
  131. <h4>Next topic</h4>
  132. <p class="topless"><a href="expressions.html"
  133. title="next chapter"><span class="section-number">6. </span>Expressions</a></p>
  134. </div>
  135. <div role="note" aria-label="source link">
  136. <h3>This Page</h3>
  137. <ul class="this-page-menu">
  138. <li><a href="../bugs.html">Report a Bug</a></li>
  139. <li>
  140. <a href="https://github.com/python/cpython/blob/main/Doc/reference/import.rst"
  141. rel="nofollow">Show Source
  142. </a>
  143. </li>
  144. </ul>
  145. </div>
  146. </nav>
  147. </div>
  148. </div>
  149. <div class="related" role="navigation" aria-label="related navigation">
  150. <h3>Navigation</h3>
  151. <ul>
  152. <li class="right" style="margin-right: 10px">
  153. <a href="../genindex.html" title="General Index"
  154. accesskey="I">index</a></li>
  155. <li class="right" >
  156. <a href="../py-modindex.html" title="Python Module Index"
  157. >modules</a> |</li>
  158. <li class="right" >
  159. <a href="expressions.html" title="6. Expressions"
  160. accesskey="N">next</a> |</li>
  161. <li class="right" >
  162. <a href="executionmodel.html" title="4. Execution model"
  163. accesskey="P">previous</a> |</li>
  164. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  165. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  166. <li class="switchers">
  167. <div class="language_switcher_placeholder"></div>
  168. <div class="version_switcher_placeholder"></div>
  169. </li>
  170. <li>
  171. </li>
  172. <li id="cpython-language-and-version">
  173. <a href="../index.html">3.12.3 Documentation</a> &#187;
  174. </li>
  175. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Language Reference</a> &#187;</li>
  176. <li class="nav-item nav-item-this"><a href=""><span class="section-number">5. </span>The import system</a></li>
  177. <li class="right">
  178. <div class="inline-search" role="search">
  179. <form class="inline-search" action="../search.html" method="get">
  180. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  181. <input type="submit" value="Go" />
  182. </form>
  183. </div>
  184. |
  185. </li>
  186. <li class="right">
  187. <label class="theme-selector-label">
  188. Theme
  189. <select class="theme-selector" oninput="activateTheme(this.value)">
  190. <option value="auto" selected>Auto</option>
  191. <option value="light">Light</option>
  192. <option value="dark">Dark</option>
  193. </select>
  194. </label> |</li>
  195. </ul>
  196. </div>
  197. <div class="document">
  198. <div class="documentwrapper">
  199. <div class="bodywrapper">
  200. <div class="body" role="main">
  201. <section id="the-import-system">
  202. <span id="importsystem"></span><h1><span class="section-number">5. </span>The import system<a class="headerlink" href="#the-import-system" title="Link to this heading">¶</a></h1>
  203. <p id="index-0">Python code in one <a class="reference internal" href="../glossary.html#term-module"><span class="xref std std-term">module</span></a> gains access to the code in another module
  204. by the process of <a class="reference internal" href="../glossary.html#term-importing"><span class="xref std std-term">importing</span></a> it. The <a class="reference internal" href="simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement is
  205. the most common way of invoking the import machinery, but it is not the only
  206. way. Functions such as <a class="reference internal" href="../library/importlib.html#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a> and built-in
  207. <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> can also be used to invoke the import machinery.</p>
  208. <p>The <a class="reference internal" href="simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement combines two operations; it searches for the
  209. named module, then it binds the results of that search to a name in the local
  210. scope. The search operation of the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code> statement is defined as
  211. a call to the <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> function, with the appropriate arguments.
  212. The return value of <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> is used to perform the name
  213. binding operation of the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code> statement. See the
  214. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code> statement for the exact details of that name binding
  215. operation.</p>
  216. <p>A direct call to <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> performs only the module search and, if
  217. found, the module creation operation. While certain side-effects may occur,
  218. such as the importing of parent packages, and the updating of various caches
  219. (including <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>), only the <a class="reference internal" href="simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement performs
  220. a name binding operation.</p>
  221. <p>When an <a class="reference internal" href="simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement is executed, the standard builtin
  222. <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> function is called. Other mechanisms for invoking the
  223. import system (such as <a class="reference internal" href="../library/importlib.html#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a>) may choose to bypass
  224. <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> and use their own solutions to implement import semantics.</p>
  225. <p>When a module is first imported, Python searches for the module and if found,
  226. it creates a module object <a class="footnote-reference brackets" href="#fnmo" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a>, initializing it. If the named module
  227. cannot be found, a <a class="reference internal" href="../library/exceptions.html#ModuleNotFoundError" title="ModuleNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ModuleNotFoundError</span></code></a> is raised. Python implements various
  228. strategies to search for the named module when the import machinery is
  229. invoked. These strategies can be modified and extended by using various hooks
  230. described in the sections below.</p>
  231. <div class="versionchanged">
  232. <p><span class="versionmodified changed">Changed in version 3.3: </span>The import system has been updated to fully implement the second phase
  233. of <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a>. There is no longer any implicit import machinery - the full
  234. import system is exposed through <a class="reference internal" href="../library/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>. In addition,
  235. native namespace package support has been implemented (see <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0420/"><strong>PEP 420</strong></a>).</p>
  236. </div>
  237. <section id="importlib">
  238. <h2><span class="section-number">5.1. </span><a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a><a class="headerlink" href="#importlib" title="Link to this heading">¶</a></h2>
  239. <p>The <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> module provides a rich API for interacting with the
  240. import system. For example <a class="reference internal" href="../library/importlib.html#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a> provides a
  241. recommended, simpler API than built-in <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> for invoking the
  242. import machinery. Refer to the <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> library documentation for
  243. additional detail.</p>
  244. </section>
  245. <section id="packages">
  246. <h2><span class="section-number">5.2. </span>Packages<a class="headerlink" href="#packages" title="Link to this heading">¶</a></h2>
  247. <p id="index-3">Python has only one type of module object, and all modules are of this type,
  248. regardless of whether the module is implemented in Python, C, or something
  249. else. To help organize modules and provide a naming hierarchy, Python has a
  250. concept of <a class="reference internal" href="../glossary.html#term-package"><span class="xref std std-term">packages</span></a>.</p>
  251. <p>You can think of packages as the directories on a file system and modules as
  252. files within directories, but don’t take this analogy too literally since
  253. packages and modules need not originate from the file system. For the
  254. purposes of this documentation, we’ll use this convenient analogy of
  255. directories and files. Like file system directories, packages are organized
  256. hierarchically, and packages may themselves contain subpackages, as well as
  257. regular modules.</p>
  258. <p>It’s important to keep in mind that all packages are modules, but not all
  259. modules are packages. Or put another way, packages are just a special kind of
  260. module. Specifically, any module that contains a <code class="docutils literal notranslate"><span class="pre">__path__</span></code> attribute is
  261. considered a package.</p>
  262. <p>All modules have a name. Subpackage names are separated from their parent
  263. package name by a dot, akin to Python’s standard attribute access syntax. Thus
  264. you might have a package called <a class="reference internal" href="../library/email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a>, which in turn has a subpackage
  265. called <a class="reference internal" href="../library/email.mime.html#module-email.mime" title="email.mime: Build MIME messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.mime</span></code></a> and a module within that subpackage called
  266. <a class="reference internal" href="../library/email.mime.html#module-email.mime.text" title="email.mime.text"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.mime.text</span></code></a>.</p>
  267. <section id="regular-packages">
  268. <h3><span class="section-number">5.2.1. </span>Regular packages<a class="headerlink" href="#regular-packages" title="Link to this heading">¶</a></h3>
  269. <p id="index-4">Python defines two types of packages, <a class="reference internal" href="../glossary.html#term-regular-package"><span class="xref std std-term">regular packages</span></a> and <a class="reference internal" href="../glossary.html#term-namespace-package"><span class="xref std std-term">namespace packages</span></a>. Regular
  270. packages are traditional packages as they existed in Python 3.2 and earlier.
  271. A regular package is typically implemented as a directory containing an
  272. <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> file. When a regular package is imported, this
  273. <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> file is implicitly executed, and the objects it defines are
  274. bound to names in the package’s namespace. The <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> file can
  275. contain the same Python code that any other module can contain, and Python
  276. will add some additional attributes to the module when it is imported.</p>
  277. <p>For example, the following file system layout defines a top level <code class="docutils literal notranslate"><span class="pre">parent</span></code>
  278. package with three subpackages:</p>
  279. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parent</span><span class="o">/</span>
  280. <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
  281. <span class="n">one</span><span class="o">/</span>
  282. <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
  283. <span class="n">two</span><span class="o">/</span>
  284. <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
  285. <span class="n">three</span><span class="o">/</span>
  286. <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
  287. </pre></div>
  288. </div>
  289. <p>Importing <code class="docutils literal notranslate"><span class="pre">parent.one</span></code> will implicitly execute <code class="docutils literal notranslate"><span class="pre">parent/__init__.py</span></code> and
  290. <code class="docutils literal notranslate"><span class="pre">parent/one/__init__.py</span></code>. Subsequent imports of <code class="docutils literal notranslate"><span class="pre">parent.two</span></code> or
  291. <code class="docutils literal notranslate"><span class="pre">parent.three</span></code> will execute <code class="docutils literal notranslate"><span class="pre">parent/two/__init__.py</span></code> and
  292. <code class="docutils literal notranslate"><span class="pre">parent/three/__init__.py</span></code> respectively.</p>
  293. </section>
  294. <section id="namespace-packages">
  295. <h3><span class="section-number">5.2.2. </span>Namespace packages<a class="headerlink" href="#namespace-packages" title="Link to this heading">¶</a></h3>
  296. <p id="index-5">A namespace package is a composite of various <a class="reference internal" href="../glossary.html#term-portion"><span class="xref std std-term">portions</span></a>,
  297. where each portion contributes a subpackage to the parent package. Portions
  298. may reside in different locations on the file system. Portions may also be
  299. found in zip files, on the network, or anywhere else that Python searches
  300. during import. Namespace packages may or may not correspond directly to
  301. objects on the file system; they may be virtual modules that have no concrete
  302. representation.</p>
  303. <p>Namespace packages do not use an ordinary list for their <code class="docutils literal notranslate"><span class="pre">__path__</span></code>
  304. attribute. They instead use a custom iterable type which will automatically
  305. perform a new search for package portions on the next import attempt within
  306. that package if the path of their parent package (or <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> for a
  307. top level package) changes.</p>
  308. <p>With namespace packages, there is no <code class="docutils literal notranslate"><span class="pre">parent/__init__.py</span></code> file. In fact,
  309. there may be multiple <code class="docutils literal notranslate"><span class="pre">parent</span></code> directories found during import search, where
  310. each one is provided by a different portion. Thus <code class="docutils literal notranslate"><span class="pre">parent/one</span></code> may not be
  311. physically located next to <code class="docutils literal notranslate"><span class="pre">parent/two</span></code>. In this case, Python will create a
  312. namespace package for the top-level <code class="docutils literal notranslate"><span class="pre">parent</span></code> package whenever it or one of
  313. its subpackages is imported.</p>
  314. <p>See also <span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0420/"><strong>PEP 420</strong></a> for the namespace package specification.</p>
  315. </section>
  316. </section>
  317. <section id="searching">
  318. <h2><span class="section-number">5.3. </span>Searching<a class="headerlink" href="#searching" title="Link to this heading">¶</a></h2>
  319. <p>To begin the search, Python needs the <a class="reference internal" href="../glossary.html#term-qualified-name"><span class="xref std std-term">fully qualified</span></a>
  320. name of the module (or package, but for the purposes of this discussion, the
  321. difference is immaterial) being imported. This name may come from various
  322. arguments to the <a class="reference internal" href="simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement, or from the parameters to the
  323. <a class="reference internal" href="../library/importlib.html#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a> or <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> functions.</p>
  324. <p>This name will be used in various phases of the import search, and it may be
  325. the dotted path to a submodule, e.g. <code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code>. In this case, Python
  326. first tries to import <code class="docutils literal notranslate"><span class="pre">foo</span></code>, then <code class="docutils literal notranslate"><span class="pre">foo.bar</span></code>, and finally <code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code>.
  327. If any of the intermediate imports fail, a <a class="reference internal" href="../library/exceptions.html#ModuleNotFoundError" title="ModuleNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ModuleNotFoundError</span></code></a> is raised.</p>
  328. <section id="the-module-cache">
  329. <h3><span class="section-number">5.3.1. </span>The module cache<a class="headerlink" href="#the-module-cache" title="Link to this heading">¶</a></h3>
  330. <p id="index-7">The first place checked during import search is <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>. This
  331. mapping serves as a cache of all modules that have been previously imported,
  332. including the intermediate paths. So if <code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code> was previously
  333. imported, <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> will contain entries for <code class="docutils literal notranslate"><span class="pre">foo</span></code>, <code class="docutils literal notranslate"><span class="pre">foo.bar</span></code>,
  334. and <code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code>. Each key will have as its value the corresponding module
  335. object.</p>
  336. <p>During import, the module name is looked up in <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> and if
  337. present, the associated value is the module satisfying the import, and the
  338. process completes. However, if the value is <code class="docutils literal notranslate"><span class="pre">None</span></code>, then a
  339. <a class="reference internal" href="../library/exceptions.html#ModuleNotFoundError" title="ModuleNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ModuleNotFoundError</span></code></a> is raised. If the module name is missing, Python will
  340. continue searching for the module.</p>
  341. <p><a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> is writable. Deleting a key may not destroy the
  342. associated module (as other modules may hold references to it),
  343. but it will invalidate the cache entry for the named module, causing
  344. Python to search anew for the named module upon its next
  345. import. The key can also be assigned to <code class="docutils literal notranslate"><span class="pre">None</span></code>, forcing the next import
  346. of the module to result in a <a class="reference internal" href="../library/exceptions.html#ModuleNotFoundError" title="ModuleNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ModuleNotFoundError</span></code></a>.</p>
  347. <p>Beware though, as if you keep a reference to the module object,
  348. invalidate its cache entry in <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>, and then re-import the
  349. named module, the two module objects will <em>not</em> be the same. By contrast,
  350. <a class="reference internal" href="../library/importlib.html#importlib.reload" title="importlib.reload"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.reload()</span></code></a> will reuse the <em>same</em> module object, and simply
  351. reinitialise the module contents by rerunning the module’s code.</p>
  352. </section>
  353. <section id="finders-and-loaders">
  354. <span id="id2"></span><h3><span class="section-number">5.3.2. </span>Finders and loaders<a class="headerlink" href="#finders-and-loaders" title="Link to this heading">¶</a></h3>
  355. <p id="index-8">If the named module is not found in <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>, then Python’s import
  356. protocol is invoked to find and load the module. This protocol consists of
  357. two conceptual objects, <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finders</span></a> and <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loaders</span></a>.
  358. A finder’s job is to determine whether it can find the named module using
  359. whatever strategy it knows about. Objects that implement both of these
  360. interfaces are referred to as <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importers</span></a> - they return
  361. themselves when they find that they can load the requested module.</p>
  362. <p>Python includes a number of default finders and importers. The first one
  363. knows how to locate built-in modules, and the second knows how to locate
  364. frozen modules. A third default finder searches an <a class="reference internal" href="../glossary.html#term-import-path"><span class="xref std std-term">import path</span></a>
  365. for modules. The <a class="reference internal" href="../glossary.html#term-import-path"><span class="xref std std-term">import path</span></a> is a list of locations that may
  366. name file system paths or zip files. It can also be extended to search
  367. for any locatable resource, such as those identified by URLs.</p>
  368. <p>The import machinery is extensible, so new finders can be added to extend the
  369. range and scope of module searching.</p>
  370. <p>Finders do not actually load modules. If they can find the named module, they
  371. return a <em class="dfn">module spec</em>, an encapsulation of the module’s import-related
  372. information, which the import machinery then uses when loading the module.</p>
  373. <p>The following sections describe the protocol for finders and loaders in more
  374. detail, including how you can create and register new ones to extend the
  375. import machinery.</p>
  376. <div class="versionchanged">
  377. <p><span class="versionmodified changed">Changed in version 3.4: </span>In previous versions of Python, finders returned <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loaders</span></a>
  378. directly, whereas now they return module specs which <em>contain</em> loaders.
  379. Loaders are still used during import but have fewer responsibilities.</p>
  380. </div>
  381. </section>
  382. <section id="import-hooks">
  383. <h3><span class="section-number">5.3.3. </span>Import hooks<a class="headerlink" href="#import-hooks" title="Link to this heading">¶</a></h3>
  384. <p id="index-9">The import machinery is designed to be extensible; the primary mechanism for
  385. this are the <em>import hooks</em>. There are two types of import hooks: <em>meta
  386. hooks</em> and <em>import path hooks</em>.</p>
  387. <p>Meta hooks are called at the start of import processing, before any other
  388. import processing has occurred, other than <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> cache look up.
  389. This allows meta hooks to override <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> processing, frozen
  390. modules, or even built-in modules. Meta hooks are registered by adding new
  391. finder objects to <a class="reference internal" href="../library/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>, as described below.</p>
  392. <p>Import path hooks are called as part of <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> (or
  393. <code class="docutils literal notranslate"><span class="pre">package.__path__</span></code>) processing, at the point where their associated path
  394. item is encountered. Import path hooks are registered by adding new callables
  395. to <a class="reference internal" href="../library/sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a> as described below.</p>
  396. </section>
  397. <section id="the-meta-path">
  398. <h3><span class="section-number">5.3.4. </span>The meta path<a class="headerlink" href="#the-meta-path" title="Link to this heading">¶</a></h3>
  399. <p id="index-10">When the named module is not found in <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>, Python next
  400. searches <a class="reference internal" href="../library/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>, which contains a list of meta path finder
  401. objects. These finders are queried in order to see if they know how to handle
  402. the named module. Meta path finders must implement a method called
  403. <a class="reference internal" href="../library/importlib.html#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> which takes three arguments:
  404. a name, an import path, and (optionally) a target module. The meta path
  405. finder can use any strategy it wants to determine whether it can handle
  406. the named module or not.</p>
  407. <p>If the meta path finder knows how to handle the named module, it returns a
  408. spec object. If it cannot handle the named module, it returns <code class="docutils literal notranslate"><span class="pre">None</span></code>. If
  409. <a class="reference internal" href="../library/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> processing reaches the end of its list without returning
  410. a spec, then a <a class="reference internal" href="../library/exceptions.html#ModuleNotFoundError" title="ModuleNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ModuleNotFoundError</span></code></a> is raised. Any other exceptions
  411. raised are simply propagated up, aborting the import process.</p>
  412. <p>The <a class="reference internal" href="../library/importlib.html#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> method of meta path
  413. finders is called with two or three arguments. The first is the fully
  414. qualified name of the module being imported, for example <code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code>.
  415. The second argument is the path entries to use for the module search. For
  416. top-level modules, the second argument is <code class="docutils literal notranslate"><span class="pre">None</span></code>, but for submodules or
  417. subpackages, the second argument is the value of the parent package’s
  418. <code class="docutils literal notranslate"><span class="pre">__path__</span></code> attribute. If the appropriate <code class="docutils literal notranslate"><span class="pre">__path__</span></code> attribute cannot
  419. be accessed, a <a class="reference internal" href="../library/exceptions.html#ModuleNotFoundError" title="ModuleNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ModuleNotFoundError</span></code></a> is raised. The third argument
  420. is an existing module object that will be the target of loading later.
  421. The import system passes in a target module only during reload.</p>
  422. <p>The meta path may be traversed multiple times for a single import request.
  423. For example, assuming none of the modules involved has already been cached,
  424. importing <code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code> will first perform a top level import, calling
  425. <code class="docutils literal notranslate"><span class="pre">mpf.find_spec(&quot;foo&quot;,</span> <span class="pre">None,</span> <span class="pre">None)</span></code> on each meta path finder (<code class="docutils literal notranslate"><span class="pre">mpf</span></code>). After
  426. <code class="docutils literal notranslate"><span class="pre">foo</span></code> has been imported, <code class="docutils literal notranslate"><span class="pre">foo.bar</span></code> will be imported by traversing the
  427. meta path a second time, calling
  428. <code class="docutils literal notranslate"><span class="pre">mpf.find_spec(&quot;foo.bar&quot;,</span> <span class="pre">foo.__path__,</span> <span class="pre">None)</span></code>. Once <code class="docutils literal notranslate"><span class="pre">foo.bar</span></code> has been
  429. imported, the final traversal will call
  430. <code class="docutils literal notranslate"><span class="pre">mpf.find_spec(&quot;foo.bar.baz&quot;,</span> <span class="pre">foo.bar.__path__,</span> <span class="pre">None)</span></code>.</p>
  431. <p>Some meta path finders only support top level imports. These importers will
  432. always return <code class="docutils literal notranslate"><span class="pre">None</span></code> when anything other than <code class="docutils literal notranslate"><span class="pre">None</span></code> is passed as the
  433. second argument.</p>
  434. <p>Python’s default <a class="reference internal" href="../library/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> has three meta path finders, one that
  435. knows how to import built-in modules, one that knows how to import frozen
  436. modules, and one that knows how to import modules from an <a class="reference internal" href="../glossary.html#term-import-path"><span class="xref std std-term">import path</span></a>
  437. (i.e. the <a class="reference internal" href="../glossary.html#term-path-based-finder"><span class="xref std std-term">path based finder</span></a>).</p>
  438. <div class="versionchanged">
  439. <p><span class="versionmodified changed">Changed in version 3.4: </span>The <a class="reference internal" href="../library/importlib.html#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> method of meta path
  440. finders replaced <code class="xref py py-meth docutils literal notranslate"><span class="pre">find_module()</span></code>, which
  441. is now deprecated. While it will continue to work without change, the
  442. import machinery will try it only if the finder does not implement
  443. <a class="reference internal" href="../library/importlib.html#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a>.</p>
  444. </div>
  445. <div class="versionchanged">
  446. <p><span class="versionmodified changed">Changed in version 3.10: </span>Use of <code class="xref py py-meth docutils literal notranslate"><span class="pre">find_module()</span></code> by the import system
  447. now raises <a class="reference internal" href="../library/exceptions.html#ImportWarning" title="ImportWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportWarning</span></code></a>.</p>
  448. </div>
  449. <div class="versionchanged">
  450. <p><span class="versionmodified changed">Changed in version 3.12: </span><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_module()</span></code> has been removed.
  451. Use <a class="reference internal" href="../library/importlib.html#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> instead.</p>
  452. </div>
  453. </section>
  454. </section>
  455. <section id="loading">
  456. <h2><span class="section-number">5.4. </span>Loading<a class="headerlink" href="#loading" title="Link to this heading">¶</a></h2>
  457. <p>If and when a module spec is found, the import machinery will use it (and
  458. the loader it contains) when loading the module. Here is an approximation
  459. of what happens during the loading portion of import:</p>
  460. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">module</span> <span class="o">=</span> <span class="kc">None</span>
  461. <span class="k">if</span> <span class="n">spec</span><span class="o">.</span><span class="n">loader</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">spec</span><span class="o">.</span><span class="n">loader</span><span class="p">,</span> <span class="s1">&#39;create_module&#39;</span><span class="p">):</span>
  462. <span class="c1"># It is assumed &#39;exec_module&#39; will also be defined on the loader.</span>
  463. <span class="n">module</span> <span class="o">=</span> <span class="n">spec</span><span class="o">.</span><span class="n">loader</span><span class="o">.</span><span class="n">create_module</span><span class="p">(</span><span class="n">spec</span><span class="p">)</span>
  464. <span class="k">if</span> <span class="n">module</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
  465. <span class="n">module</span> <span class="o">=</span> <span class="n">ModuleType</span><span class="p">(</span><span class="n">spec</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
  466. <span class="c1"># The import-related module attributes get set here:</span>
  467. <span class="n">_init_module_attrs</span><span class="p">(</span><span class="n">spec</span><span class="p">,</span> <span class="n">module</span><span class="p">)</span>
  468. <span class="k">if</span> <span class="n">spec</span><span class="o">.</span><span class="n">loader</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
  469. <span class="c1"># unsupported</span>
  470. <span class="k">raise</span> <span class="ne">ImportError</span>
  471. <span class="k">if</span> <span class="n">spec</span><span class="o">.</span><span class="n">origin</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">spec</span><span class="o">.</span><span class="n">submodule_search_locations</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
  472. <span class="c1"># namespace package</span>
  473. <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">spec</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">module</span>
  474. <span class="k">elif</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">spec</span><span class="o">.</span><span class="n">loader</span><span class="p">,</span> <span class="s1">&#39;exec_module&#39;</span><span class="p">):</span>
  475. <span class="n">module</span> <span class="o">=</span> <span class="n">spec</span><span class="o">.</span><span class="n">loader</span><span class="o">.</span><span class="n">load_module</span><span class="p">(</span><span class="n">spec</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
  476. <span class="k">else</span><span class="p">:</span>
  477. <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">spec</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">module</span>
  478. <span class="k">try</span><span class="p">:</span>
  479. <span class="n">spec</span><span class="o">.</span><span class="n">loader</span><span class="o">.</span><span class="n">exec_module</span><span class="p">(</span><span class="n">module</span><span class="p">)</span>
  480. <span class="k">except</span> <span class="ne">BaseException</span><span class="p">:</span>
  481. <span class="k">try</span><span class="p">:</span>
  482. <span class="k">del</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">spec</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
  483. <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
  484. <span class="k">pass</span>
  485. <span class="k">raise</span>
  486. <span class="k">return</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">spec</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
  487. </pre></div>
  488. </div>
  489. <p>Note the following details:</p>
  490. <ul class="simple">
  491. <li><p>If there is an existing module object with the given name in
  492. <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>, import will have already returned it.</p></li>
  493. <li><p>The module will exist in <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> before the loader
  494. executes the module code. This is crucial because the module code may
  495. (directly or indirectly) import itself; adding it to <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>
  496. beforehand prevents unbounded recursion in the worst case and multiple
  497. loading in the best.</p></li>
  498. <li><p>If loading fails, the failing module – and only the failing module –
  499. gets removed from <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>. Any module already in the
  500. <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> cache, and any module that was successfully loaded
  501. as a side-effect, must remain in the cache. This contrasts with
  502. reloading where even the failing module is left in <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>.</p></li>
  503. <li><p>After the module is created but before execution, the import machinery
  504. sets the import-related module attributes (“_init_module_attrs” in
  505. the pseudo-code example above), as summarized in a
  506. <a class="reference internal" href="#import-mod-attrs"><span class="std std-ref">later section</span></a>.</p></li>
  507. <li><p>Module execution is the key moment of loading in which the module’s
  508. namespace gets populated. Execution is entirely delegated to the
  509. loader, which gets to decide what gets populated and how.</p></li>
  510. <li><p>The module created during loading and passed to exec_module() may
  511. not be the one returned at the end of import <a class="footnote-reference brackets" href="#fnlo" id="id3" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a>.</p></li>
  512. </ul>
  513. <div class="versionchanged">
  514. <p><span class="versionmodified changed">Changed in version 3.4: </span>The import system has taken over the boilerplate responsibilities of
  515. loaders. These were previously performed by the
  516. <a class="reference internal" href="../library/importlib.html#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.load_module()</span></code></a> method.</p>
  517. </div>
  518. <section id="loaders">
  519. <h3><span class="section-number">5.4.1. </span>Loaders<a class="headerlink" href="#loaders" title="Link to this heading">¶</a></h3>
  520. <p>Module loaders provide the critical function of loading: module execution.
  521. The import machinery calls the <a class="reference internal" href="../library/importlib.html#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.exec_module()</span></code></a>
  522. method with a single argument, the module object to execute. Any value
  523. returned from <a class="reference internal" href="../library/importlib.html#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 ignored.</p>
  524. <p>Loaders must satisfy the following requirements:</p>
  525. <ul class="simple">
  526. <li><p>If the module is a Python module (as opposed to a built-in module or a
  527. dynamically loaded extension), the loader should execute the module’s code
  528. in the module’s global name space (<code class="docutils literal notranslate"><span class="pre">module.__dict__</span></code>).</p></li>
  529. <li><p>If the loader cannot execute the module, it should raise an
  530. <a class="reference internal" href="../library/exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a>, although any other exception raised during
  531. <a class="reference internal" href="../library/importlib.html#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> will be propagated.</p></li>
  532. </ul>
  533. <p>In many cases, the finder and loader can be the same object; in such cases the
  534. <a class="reference internal" href="../library/importlib.html#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> method would just return a
  535. spec with the loader set to <code class="docutils literal notranslate"><span class="pre">self</span></code>.</p>
  536. <p>Module loaders may opt in to creating the module object during loading
  537. by implementing a <a class="reference internal" href="../library/importlib.html#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</span></code></a> method.
  538. It takes one argument, the module spec, and returns the new module object
  539. to use during loading. <code class="docutils literal notranslate"><span class="pre">create_module()</span></code> does not need to set any attributes
  540. on the module object. If the method returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, the
  541. import machinery will create the new module itself.</p>
  542. <div class="versionadded">
  543. <p><span class="versionmodified added">New in version 3.4: </span>The <a class="reference internal" href="../library/importlib.html#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</span></code></a> method of loaders.</p>
  544. </div>
  545. <div class="versionchanged">
  546. <p><span class="versionmodified changed">Changed in version 3.4: </span>The <a class="reference internal" href="../library/importlib.html#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">load_module()</span></code></a> method was replaced by
  547. <a class="reference internal" href="../library/importlib.html#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> and the import
  548. machinery assumed all the boilerplate responsibilities of loading.</p>
  549. <p>For compatibility with existing loaders, the import machinery will use
  550. the <code class="docutils literal notranslate"><span class="pre">load_module()</span></code> method of loaders if it exists and the loader does
  551. not also implement <code class="docutils literal notranslate"><span class="pre">exec_module()</span></code>. However, <code class="docutils literal notranslate"><span class="pre">load_module()</span></code> has been
  552. deprecated and loaders should implement <code class="docutils literal notranslate"><span class="pre">exec_module()</span></code> instead.</p>
  553. <p>The <code class="docutils literal notranslate"><span class="pre">load_module()</span></code> method must implement all the boilerplate loading
  554. functionality described above in addition to executing the module. All
  555. the same constraints apply, with some additional clarification:</p>
  556. <ul class="simple">
  557. <li><p>If there is an existing module object with the given name in
  558. <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>, the loader must use that existing module.
  559. (Otherwise, <a class="reference internal" href="../library/importlib.html#importlib.reload" title="importlib.reload"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.reload()</span></code></a> will not work correctly.) If the
  560. named module does not exist in <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>, the loader
  561. must create a new module object and add it to <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>.</p></li>
  562. <li><p>The module <em>must</em> exist in <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> before the loader
  563. executes the module code, to prevent unbounded recursion or multiple
  564. loading.</p></li>
  565. <li><p>If loading fails, the loader must remove any modules it has inserted
  566. into <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>, but it must remove <strong>only</strong> the failing
  567. module(s), and only if the loader itself has loaded the module(s)
  568. explicitly.</p></li>
  569. </ul>
  570. </div>
  571. <div class="versionchanged">
  572. <p><span class="versionmodified changed">Changed in version 3.5: </span>A <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> is raised when <code class="docutils literal notranslate"><span class="pre">exec_module()</span></code> is defined but
  573. <code class="docutils literal notranslate"><span class="pre">create_module()</span></code> is not.</p>
  574. </div>
  575. <div class="versionchanged">
  576. <p><span class="versionmodified changed">Changed in version 3.6: </span>An <a class="reference internal" href="../library/exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> is raised when <code class="docutils literal notranslate"><span class="pre">exec_module()</span></code> is defined but
  577. <code class="docutils literal notranslate"><span class="pre">create_module()</span></code> is not.</p>
  578. </div>
  579. <div class="versionchanged">
  580. <p><span class="versionmodified changed">Changed in version 3.10: </span>Use of <code class="docutils literal notranslate"><span class="pre">load_module()</span></code> will raise <a class="reference internal" href="../library/exceptions.html#ImportWarning" title="ImportWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportWarning</span></code></a>.</p>
  581. </div>
  582. </section>
  583. <section id="submodules">
  584. <h3><span class="section-number">5.4.2. </span>Submodules<a class="headerlink" href="#submodules" title="Link to this heading">¶</a></h3>
  585. <p>When a submodule is loaded using any mechanism (e.g. <code class="docutils literal notranslate"><span class="pre">importlib</span></code> APIs, the
  586. <code class="docutils literal notranslate"><span class="pre">import</span></code> or <code class="docutils literal notranslate"><span class="pre">import-from</span></code> statements, or built-in <code class="docutils literal notranslate"><span class="pre">__import__()</span></code>) a
  587. binding is placed in the parent module’s namespace to the submodule object.
  588. For example, if package <code class="docutils literal notranslate"><span class="pre">spam</span></code> has a submodule <code class="docutils literal notranslate"><span class="pre">foo</span></code>, after importing
  589. <code class="docutils literal notranslate"><span class="pre">spam.foo</span></code>, <code class="docutils literal notranslate"><span class="pre">spam</span></code> will have an attribute <code class="docutils literal notranslate"><span class="pre">foo</span></code> which is bound to the
  590. submodule. Let’s say you have the following directory structure:</p>
  591. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">spam</span><span class="o">/</span>
  592. <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
  593. <span class="n">foo</span><span class="o">.</span><span class="n">py</span>
  594. </pre></div>
  595. </div>
  596. <p>and <code class="docutils literal notranslate"><span class="pre">spam/__init__.py</span></code> has the following line in it:</p>
  597. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">.foo</span> <span class="kn">import</span> <span class="n">Foo</span>
  598. </pre></div>
  599. </div>
  600. <p>then executing the following puts name bindings for <code class="docutils literal notranslate"><span class="pre">foo</span></code> and <code class="docutils literal notranslate"><span class="pre">Foo</span></code> in the
  601. <code class="docutils literal notranslate"><span class="pre">spam</span></code> module:</p>
  602. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">spam</span>
  603. <span class="gp">&gt;&gt;&gt; </span><span class="n">spam</span><span class="o">.</span><span class="n">foo</span>
  604. <span class="go">&lt;module &#39;spam.foo&#39; from &#39;/tmp/imports/spam/foo.py&#39;&gt;</span>
  605. <span class="gp">&gt;&gt;&gt; </span><span class="n">spam</span><span class="o">.</span><span class="n">Foo</span>
  606. <span class="go">&lt;class &#39;spam.foo.Foo&#39;&gt;</span>
  607. </pre></div>
  608. </div>
  609. <p>Given Python’s familiar name binding rules this might seem surprising, but
  610. it’s actually a fundamental feature of the import system. The invariant
  611. holding is that if you have <code class="docutils literal notranslate"><span class="pre">sys.modules['spam']</span></code> and
  612. <code class="docutils literal notranslate"><span class="pre">sys.modules['spam.foo']</span></code> (as you would after the above import), the latter
  613. must appear as the <code class="docutils literal notranslate"><span class="pre">foo</span></code> attribute of the former.</p>
  614. </section>
  615. <section id="module-spec">
  616. <h3><span class="section-number">5.4.3. </span>Module spec<a class="headerlink" href="#module-spec" title="Link to this heading">¶</a></h3>
  617. <p>The import machinery uses a variety of information about each module
  618. during import, especially before loading. Most of the information is
  619. common to all modules. The purpose of a module’s spec is to encapsulate
  620. this import-related information on a per-module basis.</p>
  621. <p>Using a spec during import allows state to be transferred between import
  622. system components, e.g. between the finder that creates the module spec
  623. and the loader that executes it. Most importantly, it allows the
  624. import machinery to perform the boilerplate operations of loading,
  625. whereas without a module spec the loader had that responsibility.</p>
  626. <p>The module’s spec is exposed as the <code class="docutils literal notranslate"><span class="pre">__spec__</span></code> attribute on a module object.
  627. See <a class="reference internal" href="../library/importlib.html#importlib.machinery.ModuleSpec" title="importlib.machinery.ModuleSpec"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModuleSpec</span></code></a> for details on the contents of
  628. the module spec.</p>
  629. <div class="versionadded">
  630. <p><span class="versionmodified added">New in version 3.4.</span></p>
  631. </div>
  632. </section>
  633. <section id="import-related-module-attributes">
  634. <span id="import-mod-attrs"></span><h3><span class="section-number">5.4.4. </span>Import-related module attributes<a class="headerlink" href="#import-related-module-attributes" title="Link to this heading">¶</a></h3>
  635. <p>The import machinery fills in these attributes on each module object
  636. during loading, based on the module’s spec, before the loader executes
  637. the module.</p>
  638. <p>It is <strong>strongly</strong> recommended that you rely on <a class="reference internal" href="#spec__" title="__spec__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__spec__</span></code></a> and
  639. its attributes instead of any of the other individual attributes
  640. listed below.</p>
  641. <dl class="py attribute">
  642. <dt class="sig sig-object py" id="name__">
  643. <span class="sig-name descname"><span class="pre">__name__</span></span><a class="headerlink" href="#name__" title="Link to this definition">¶</a></dt>
  644. <dd><p>The <code class="docutils literal notranslate"><span class="pre">__name__</span></code> attribute must be set to the fully qualified name of
  645. the module. This name is used to uniquely identify the module in
  646. the import system.</p>
  647. </dd></dl>
  648. <dl class="py attribute">
  649. <dt class="sig sig-object py" id="loader__">
  650. <span class="sig-name descname"><span class="pre">__loader__</span></span><a class="headerlink" href="#loader__" title="Link to this definition">¶</a></dt>
  651. <dd><p>The <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> attribute must be set to the loader object that
  652. the import machinery used when loading the module. This is mostly
  653. for introspection, but can be used for additional loader-specific
  654. functionality, for example getting data associated with a loader.</p>
  655. <p>It is <strong>strongly</strong> recommended that you rely on <a class="reference internal" href="#spec__" title="__spec__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__spec__</span></code></a>
  656. instead of this attribute.</p>
  657. <div class="versionchanged">
  658. <p><span class="versionmodified changed">Changed in version 3.12: </span>The value of <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> is expected to be the same as
  659. <code class="docutils literal notranslate"><span class="pre">__spec__.loader</span></code>. The use of <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> is deprecated and slated
  660. for removal in Python 3.14.</p>
  661. </div>
  662. </dd></dl>
  663. <dl class="py attribute">
  664. <dt class="sig sig-object py" id="package__">
  665. <span class="sig-name descname"><span class="pre">__package__</span></span><a class="headerlink" href="#package__" title="Link to this definition">¶</a></dt>
  666. <dd><p>The module’s <code class="docutils literal notranslate"><span class="pre">__package__</span></code> attribute may be set. Its value must
  667. be a string, but it can be the same value as its <code class="docutils literal notranslate"><span class="pre">__name__</span></code>. When
  668. the module is a package, its <code class="docutils literal notranslate"><span class="pre">__package__</span></code> value should be set to
  669. its <code class="docutils literal notranslate"><span class="pre">__name__</span></code>. When the module is not a package, <code class="docutils literal notranslate"><span class="pre">__package__</span></code>
  670. should be set to the empty string for top-level modules, or for
  671. submodules, to the parent package’s name. See <span class="target" id="index-11"></span><a class="pep reference external" href="https://peps.python.org/pep-0366/"><strong>PEP 366</strong></a> for further
  672. details.</p>
  673. <p>This attribute is used instead of <code class="docutils literal notranslate"><span class="pre">__name__</span></code> to calculate explicit
  674. relative imports for main modules, as defined in <span class="target" id="index-12"></span><a class="pep reference external" href="https://peps.python.org/pep-0366/"><strong>PEP 366</strong></a>.</p>
  675. <p>It is <strong>strongly</strong> recommended that you rely on <a class="reference internal" href="#spec__" title="__spec__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__spec__</span></code></a>
  676. instead of this attribute.</p>
  677. <div class="versionchanged">
  678. <p><span class="versionmodified changed">Changed in version 3.6: </span>The value of <code class="docutils literal notranslate"><span class="pre">__package__</span></code> is expected to be the same as
  679. <code class="docutils literal notranslate"><span class="pre">__spec__.parent</span></code>.</p>
  680. </div>
  681. <div class="versionchanged">
  682. <p><span class="versionmodified changed">Changed in version 3.10: </span><a class="reference internal" href="../library/exceptions.html#ImportWarning" title="ImportWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportWarning</span></code></a> is raised if import falls back to
  683. <code class="docutils literal notranslate"><span class="pre">__package__</span></code> instead of
  684. <a class="reference internal" href="../library/importlib.html#importlib.machinery.ModuleSpec.parent" title="importlib.machinery.ModuleSpec.parent"><code class="xref py py-attr docutils literal notranslate"><span class="pre">parent</span></code></a>.</p>
  685. </div>
  686. <div class="versionchanged">
  687. <p><span class="versionmodified changed">Changed in version 3.12: </span>Raise <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> instead of <a class="reference internal" href="../library/exceptions.html#ImportWarning" title="ImportWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportWarning</span></code></a>
  688. when falling back to <code class="docutils literal notranslate"><span class="pre">__package__</span></code>.</p>
  689. </div>
  690. </dd></dl>
  691. <dl class="py attribute">
  692. <dt class="sig sig-object py" id="spec__">
  693. <span class="sig-name descname"><span class="pre">__spec__</span></span><a class="headerlink" href="#spec__" title="Link to this definition">¶</a></dt>
  694. <dd><p>The <code class="docutils literal notranslate"><span class="pre">__spec__</span></code> attribute must be set to the module spec that was
  695. used when importing the module. Setting <code class="docutils literal notranslate"><span class="pre">__spec__</span></code>
  696. appropriately applies equally to <a class="reference internal" href="toplevel_components.html#programs"><span class="std std-ref">modules initialized during
  697. interpreter startup</span></a>. The one exception is <code class="docutils literal notranslate"><span class="pre">__main__</span></code>,
  698. where <code class="docutils literal notranslate"><span class="pre">__spec__</span></code> is <a class="reference internal" href="#main-spec"><span class="std std-ref">set to None in some cases</span></a>.</p>
  699. <p>When <code class="docutils literal notranslate"><span class="pre">__spec__.parent</span></code> is not set, <code class="docutils literal notranslate"><span class="pre">__package__</span></code> is used as
  700. a fallback.</p>
  701. <div class="versionadded">
  702. <p><span class="versionmodified added">New in version 3.4.</span></p>
  703. </div>
  704. <div class="versionchanged">
  705. <p><span class="versionmodified changed">Changed in version 3.6: </span><code class="docutils literal notranslate"><span class="pre">__spec__.parent</span></code> is used as a fallback when <code class="docutils literal notranslate"><span class="pre">__package__</span></code> is
  706. not defined.</p>
  707. </div>
  708. </dd></dl>
  709. <dl class="py attribute">
  710. <dt class="sig sig-object py" id="path__">
  711. <span class="sig-name descname"><span class="pre">__path__</span></span><a class="headerlink" href="#path__" title="Link to this definition">¶</a></dt>
  712. <dd><p>If the module is a package (either regular or namespace), the module
  713. object’s <code class="docutils literal notranslate"><span class="pre">__path__</span></code> attribute must be set. The value must be
  714. iterable, but may be empty if <code class="docutils literal notranslate"><span class="pre">__path__</span></code> has no further significance.
  715. If <code class="docutils literal notranslate"><span class="pre">__path__</span></code> is not empty, it must produce strings when iterated
  716. over. More details on the semantics of <code class="docutils literal notranslate"><span class="pre">__path__</span></code> are given
  717. <a class="reference internal" href="#package-path-rules"><span class="std std-ref">below</span></a>.</p>
  718. <p>Non-package modules should not have a <code class="docutils literal notranslate"><span class="pre">__path__</span></code> attribute.</p>
  719. </dd></dl>
  720. <dl class="py attribute">
  721. <dt class="sig sig-object py" id="file__">
  722. <span class="sig-name descname"><span class="pre">__file__</span></span><a class="headerlink" href="#file__" title="Link to this definition">¶</a></dt>
  723. <dd></dd></dl>
  724. <dl class="py attribute">
  725. <dt class="sig sig-object py" id="cached__">
  726. <span class="sig-name descname"><span class="pre">__cached__</span></span><a class="headerlink" href="#cached__" title="Link to this definition">¶</a></dt>
  727. <dd><p><code class="docutils literal notranslate"><span class="pre">__file__</span></code> is optional (if set, value must be a string). It indicates
  728. the pathname of the file from which the module was loaded (if
  729. loaded from a file), or the pathname of the shared library file
  730. for extension modules loaded dynamically from a shared library.
  731. It might be missing for certain types of modules, such as C
  732. modules that are statically linked into the interpreter, and the
  733. import system may opt to leave it unset if it has no semantic
  734. meaning (e.g. a module loaded from a database).</p>
  735. <p>If <code class="docutils literal notranslate"><span class="pre">__file__</span></code> is set then the <code class="docutils literal notranslate"><span class="pre">__cached__</span></code> attribute might also
  736. be set, which is the path to any compiled version of
  737. the code (e.g. byte-compiled file). The file does not need to exist
  738. to set this attribute; the path can simply point to where the
  739. compiled file would exist (see <span class="target" id="index-13"></span><a class="pep reference external" href="https://peps.python.org/pep-3147/"><strong>PEP 3147</strong></a>).</p>
  740. <p>Note that <code class="docutils literal notranslate"><span class="pre">__cached__</span></code> may be set even if <code class="docutils literal notranslate"><span class="pre">__file__</span></code> is not
  741. set. However, that scenario is quite atypical. Ultimately, the
  742. loader is what makes use of the module spec provided by the finder
  743. (from which <code class="docutils literal notranslate"><span class="pre">__file__</span></code> and <code class="docutils literal notranslate"><span class="pre">__cached__</span></code> are derived). So
  744. if a loader can load from a cached module but otherwise does not load
  745. from a file, that atypical scenario may be appropriate.</p>
  746. <p>It is <strong>strongly</strong> recommended that you rely on <a class="reference internal" href="#spec__" title="__spec__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__spec__</span></code></a>
  747. instead of <code class="docutils literal notranslate"><span class="pre">__cached__</span></code>.</p>
  748. </dd></dl>
  749. </section>
  750. <section id="module-path">
  751. <span id="package-path-rules"></span><h3><span class="section-number">5.4.5. </span>module.__path__<a class="headerlink" href="#module-path" title="Link to this heading">¶</a></h3>
  752. <p>By definition, if a module has a <code class="docutils literal notranslate"><span class="pre">__path__</span></code> attribute, it is a package.</p>
  753. <p>A package’s <code class="docutils literal notranslate"><span class="pre">__path__</span></code> attribute is used during imports of its subpackages.
  754. Within the import machinery, it functions much the same as <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>,
  755. i.e. providing a list of locations to search for modules during import.
  756. However, <code class="docutils literal notranslate"><span class="pre">__path__</span></code> is typically much more constrained than
  757. <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>.</p>
  758. <p><code class="docutils literal notranslate"><span class="pre">__path__</span></code> must be an iterable of strings, but it may be empty.
  759. The same rules used for <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> also apply to a package’s
  760. <code class="docutils literal notranslate"><span class="pre">__path__</span></code>, and <a class="reference internal" href="../library/sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a> (described below) are
  761. consulted when traversing a package’s <code class="docutils literal notranslate"><span class="pre">__path__</span></code>.</p>
  762. <p>A package’s <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> file may set or alter the package’s <code class="docutils literal notranslate"><span class="pre">__path__</span></code>
  763. attribute, and this was typically the way namespace packages were implemented
  764. prior to <span class="target" id="index-14"></span><a class="pep reference external" href="https://peps.python.org/pep-0420/"><strong>PEP 420</strong></a>. With the adoption of <span class="target" id="index-15"></span><a class="pep reference external" href="https://peps.python.org/pep-0420/"><strong>PEP 420</strong></a>, namespace packages no
  765. longer need to supply <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> files containing only <code class="docutils literal notranslate"><span class="pre">__path__</span></code>
  766. manipulation code; the import machinery automatically sets <code class="docutils literal notranslate"><span class="pre">__path__</span></code>
  767. correctly for the namespace package.</p>
  768. </section>
  769. <section id="module-reprs">
  770. <h3><span class="section-number">5.4.6. </span>Module reprs<a class="headerlink" href="#module-reprs" title="Link to this heading">¶</a></h3>
  771. <p>By default, all modules have a usable repr, however depending on the
  772. attributes set above, and in the module’s spec, you can more explicitly
  773. control the repr of module objects.</p>
  774. <p>If the module has a spec (<code class="docutils literal notranslate"><span class="pre">__spec__</span></code>), the import machinery will try
  775. to generate a repr from it. If that fails or there is no spec, the import
  776. system will craft a default repr using whatever information is available
  777. on the module. It will try to use the <code class="docutils literal notranslate"><span class="pre">module.__name__</span></code>,
  778. <code class="docutils literal notranslate"><span class="pre">module.__file__</span></code>, and <code class="docutils literal notranslate"><span class="pre">module.__loader__</span></code> as input into the repr,
  779. with defaults for whatever information is missing.</p>
  780. <p>Here are the exact rules used:</p>
  781. <ul class="simple">
  782. <li><p>If the module has a <code class="docutils literal notranslate"><span class="pre">__spec__</span></code> attribute, the information in the spec
  783. is used to generate the repr. The “name”, “loader”, “origin”, and
  784. “has_location” attributes are consulted.</p></li>
  785. <li><p>If the module has a <code class="docutils literal notranslate"><span class="pre">__file__</span></code> attribute, this is used as part of the
  786. module’s repr.</p></li>
  787. <li><p>If the module has no <code class="docutils literal notranslate"><span class="pre">__file__</span></code> but does have a <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> that is not
  788. <code class="docutils literal notranslate"><span class="pre">None</span></code>, then the loader’s repr is used as part of the module’s repr.</p></li>
  789. <li><p>Otherwise, just use the module’s <code class="docutils literal notranslate"><span class="pre">__name__</span></code> in the repr.</p></li>
  790. </ul>
  791. <div class="versionchanged">
  792. <p><span class="versionmodified changed">Changed in version 3.12: </span>Use of <code class="xref py py-meth docutils literal notranslate"><span class="pre">module_repr()</span></code>, having been deprecated since Python 3.4, was
  793. removed in Python 3.12 and is no longer called during the resolution of a
  794. module’s repr.</p>
  795. </div>
  796. </section>
  797. <section id="cached-bytecode-invalidation">
  798. <span id="pyc-invalidation"></span><h3><span class="section-number">5.4.7. </span>Cached bytecode invalidation<a class="headerlink" href="#cached-bytecode-invalidation" title="Link to this heading">¶</a></h3>
  799. <p>Before Python loads cached bytecode from a <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> file, it checks whether the
  800. cache is up-to-date with the source <code class="docutils literal notranslate"><span class="pre">.py</span></code> file. By default, Python does this
  801. by storing the source’s last-modified timestamp and size in the cache file when
  802. writing it. At runtime, the import system then validates the cache file by
  803. checking the stored metadata in the cache file against the source’s
  804. metadata.</p>
  805. <p>Python also supports “hash-based” cache files, which store a hash of the source
  806. file’s contents rather than its metadata. There are two variants of hash-based
  807. <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files: checked and unchecked. For checked hash-based <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files,
  808. Python validates the cache file by hashing the source file and comparing the
  809. resulting hash with the hash in the cache file. If a checked hash-based cache
  810. file is found to be invalid, Python regenerates it and writes a new checked
  811. hash-based cache file. For unchecked hash-based <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files, Python simply
  812. assumes the cache file is valid if it exists. Hash-based <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files
  813. validation behavior may be overridden with the <a class="reference internal" href="../using/cmdline.html#cmdoption-check-hash-based-pycs"><code class="xref std std-option docutils literal notranslate"><span class="pre">--check-hash-based-pycs</span></code></a>
  814. flag.</p>
  815. <div class="versionchanged">
  816. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added hash-based <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files. Previously, Python only supported
  817. timestamp-based invalidation of bytecode caches.</p>
  818. </div>
  819. </section>
  820. </section>
  821. <section id="the-path-based-finder">
  822. <h2><span class="section-number">5.5. </span>The Path Based Finder<a class="headerlink" href="#the-path-based-finder" title="Link to this heading">¶</a></h2>
  823. <p id="index-16">As mentioned previously, Python comes with several default meta path finders.
  824. One of these, called the <a class="reference internal" href="../glossary.html#term-path-based-finder"><span class="xref std std-term">path based finder</span></a>
  825. (<a class="reference internal" href="../library/importlib.html#importlib.machinery.PathFinder" title="importlib.machinery.PathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">PathFinder</span></code></a>), searches an <a class="reference internal" href="../glossary.html#term-import-path"><span class="xref std std-term">import path</span></a>,
  826. which contains a list of <a class="reference internal" href="../glossary.html#term-path-entry"><span class="xref std std-term">path entries</span></a>. Each path
  827. entry names a location to search for modules.</p>
  828. <p>The path based finder itself doesn’t know how to import anything. Instead, it
  829. traverses the individual path entries, associating each of them with a
  830. path entry finder that knows how to handle that particular kind of path.</p>
  831. <p>The default set of path entry finders implement all the semantics for finding
  832. modules on the file system, handling special file types such as Python source
  833. code (<code class="docutils literal notranslate"><span class="pre">.py</span></code> files), Python byte code (<code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files) and
  834. shared libraries (e.g. <code class="docutils literal notranslate"><span class="pre">.so</span></code> files). When supported by the <a class="reference internal" href="../library/zipimport.html#module-zipimport" title="zipimport: Support for importing Python modules from ZIP archives."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code></a>
  835. module in the standard library, the default path entry finders also handle
  836. loading all of these file types (other than shared libraries) from zipfiles.</p>
  837. <p>Path entries need not be limited to file system locations. They can refer to
  838. URLs, database queries, or any other location that can be specified as a
  839. string.</p>
  840. <p>The path based finder provides additional hooks and protocols so that you
  841. can extend and customize the types of searchable path entries. For example,
  842. if you wanted to support path entries as network URLs, you could write a hook
  843. that implements HTTP semantics to find modules on the web. This hook (a
  844. callable) would return a <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a> supporting the protocol
  845. described below, which was then used to get a loader for the module from the
  846. web.</p>
  847. <p>A word of warning: this section and the previous both use the term <em>finder</em>,
  848. distinguishing between them by using the terms <a class="reference internal" href="../glossary.html#term-meta-path-finder"><span class="xref std std-term">meta path finder</span></a> and
  849. <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a>. These two types of finders are very similar,
  850. support similar protocols, and function in similar ways during the import
  851. process, but it’s important to keep in mind that they are subtly different.
  852. In particular, meta path finders operate at the beginning of the import
  853. process, as keyed off the <a class="reference internal" href="../library/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> traversal.</p>
  854. <p>By contrast, path entry finders are in a sense an implementation detail
  855. of the path based finder, and in fact, if the path based finder were to be
  856. removed from <a class="reference internal" href="../library/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>, none of the path entry finder semantics
  857. would be invoked.</p>
  858. <section id="path-entry-finders">
  859. <h3><span class="section-number">5.5.1. </span>Path entry finders<a class="headerlink" href="#path-entry-finders" title="Link to this heading">¶</a></h3>
  860. <p id="index-17">The <a class="reference internal" href="../glossary.html#term-path-based-finder"><span class="xref std std-term">path based finder</span></a> is responsible for finding and loading
  861. Python modules and packages whose location is specified with a string
  862. <a class="reference internal" href="../glossary.html#term-path-entry"><span class="xref std std-term">path entry</span></a>. Most path entries name locations in the file system,
  863. but they need not be limited to this.</p>
  864. <p>As a meta path finder, the <a class="reference internal" href="../glossary.html#term-path-based-finder"><span class="xref std std-term">path based finder</span></a> implements the
  865. <a class="reference internal" href="../library/importlib.html#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> protocol previously
  866. described, however it exposes additional hooks that can be used to
  867. customize how modules are found and loaded from the <a class="reference internal" href="../glossary.html#term-import-path"><span class="xref std std-term">import path</span></a>.</p>
  868. <p>Three variables are used by the <a class="reference internal" href="../glossary.html#term-path-based-finder"><span class="xref std std-term">path based finder</span></a>, <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>,
  869. <a class="reference internal" href="../library/sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a> and <a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a>. The <code class="docutils literal notranslate"><span class="pre">__path__</span></code>
  870. attributes on package objects are also used. These provide additional ways
  871. that the import machinery can be customized.</p>
  872. <p><a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> contains a list of strings providing search locations for
  873. modules and packages. It is initialized from the <span class="target" id="index-18"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a>
  874. environment variable and various other installation- and
  875. implementation-specific defaults. Entries in <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> can name
  876. directories on the file system, zip files, and potentially other “locations”
  877. (see the <a class="reference internal" href="../library/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> module) that should be searched for modules, such as
  878. URLs, or database queries. Only strings should be present on
  879. <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>; all other data types are ignored.</p>
  880. <p>The <a class="reference internal" href="../glossary.html#term-path-based-finder"><span class="xref std std-term">path based finder</span></a> is a <a class="reference internal" href="../glossary.html#term-meta-path-finder"><span class="xref std std-term">meta path finder</span></a>, so the import
  881. machinery begins the <a class="reference internal" href="../glossary.html#term-import-path"><span class="xref std std-term">import path</span></a> search by calling the path
  882. based finder’s <a class="reference internal" href="../library/importlib.html#importlib.machinery.PathFinder.find_spec" title="importlib.machinery.PathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> method as
  883. described previously. When the <code class="docutils literal notranslate"><span class="pre">path</span></code> argument to
  884. <a class="reference internal" href="../library/importlib.html#importlib.machinery.PathFinder.find_spec" title="importlib.machinery.PathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> is given, it will be a
  885. list of string paths to traverse - typically a package’s <code class="docutils literal notranslate"><span class="pre">__path__</span></code>
  886. attribute for an import within that package. If the <code class="docutils literal notranslate"><span class="pre">path</span></code> argument is
  887. <code class="docutils literal notranslate"><span class="pre">None</span></code>, this indicates a top level import and <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> is used.</p>
  888. <p>The path based finder iterates over every entry in the search path, and
  889. for each of these, looks for an appropriate <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a>
  890. (<a class="reference internal" href="../library/importlib.html#importlib.abc.PathEntryFinder" title="importlib.abc.PathEntryFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">PathEntryFinder</span></code></a>) for the
  891. path entry. Because this can be an expensive operation (e.g. there may be
  892. <code class="docutils literal notranslate"><span class="pre">stat()</span></code> call overheads for this search), the path based finder maintains
  893. a cache mapping path entries to path entry finders. This cache is maintained
  894. in <a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a> (despite the name, this cache actually
  895. stores finder objects rather than being limited to <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a> objects).
  896. In this way, the expensive search for a particular <a class="reference internal" href="../glossary.html#term-path-entry"><span class="xref std std-term">path entry</span></a>
  897. location’s <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a> need only be done once. User code is
  898. free to remove cache entries from <a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a> forcing
  899. the path based finder to perform the path entry search again.</p>
  900. <p>If the path entry is not present in the cache, the path based finder iterates
  901. over every callable in <a class="reference internal" href="../library/sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a>. Each of the <a class="reference internal" href="../glossary.html#term-path-entry-hook"><span class="xref std std-term">path entry
  902. hooks</span></a> in this list is called with a single argument, the
  903. path entry to be searched. This callable may either return a <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path
  904. entry finder</span></a> that can handle the path entry, or it may raise
  905. <a class="reference internal" href="../library/exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a>. An <a class="reference internal" href="../library/exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> is used by the path based finder to
  906. signal that the hook cannot find a <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a>
  907. for that <a class="reference internal" href="../glossary.html#term-path-entry"><span class="xref std std-term">path entry</span></a>. The
  908. exception is ignored and <a class="reference internal" href="../glossary.html#term-import-path"><span class="xref std std-term">import path</span></a> iteration continues. The hook
  909. should expect either a string or bytes object; the encoding of bytes objects
  910. is up to the hook (e.g. it may be a file system encoding, UTF-8, or something
  911. else), and if the hook cannot decode the argument, it should raise
  912. <a class="reference internal" href="../library/exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a>.</p>
  913. <p>If <a class="reference internal" href="../library/sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a> iteration ends with no <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a>
  914. being returned, then the path based finder’s
  915. <a class="reference internal" href="../library/importlib.html#importlib.machinery.PathFinder.find_spec" title="importlib.machinery.PathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> method will store <code class="docutils literal notranslate"><span class="pre">None</span></code>
  916. in <a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a> (to indicate that there is no finder for
  917. this path entry) and return <code class="docutils literal notranslate"><span class="pre">None</span></code>, indicating that this
  918. <a class="reference internal" href="../glossary.html#term-meta-path-finder"><span class="xref std std-term">meta path finder</span></a> could not find the module.</p>
  919. <p>If a <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a> <em>is</em> returned by one of the <a class="reference internal" href="../glossary.html#term-path-entry-hook"><span class="xref std std-term">path entry
  920. hook</span></a> callables on <a class="reference internal" href="../library/sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a>, then the following protocol is used
  921. to ask the finder for a module spec, which is then used when loading the
  922. module.</p>
  923. <p>The current working directory – denoted by an empty string – is handled
  924. slightly differently from other entries on <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>. First, if the
  925. current working directory is found to not exist, no value is stored in
  926. <a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a>. Second, the value for the current working
  927. directory is looked up fresh for each module lookup. Third, the path used for
  928. <a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a> and returned by
  929. <a class="reference internal" href="../library/importlib.html#importlib.machinery.PathFinder.find_spec" title="importlib.machinery.PathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.machinery.PathFinder.find_spec()</span></code></a> will be the actual current
  930. working directory and not the empty string.</p>
  931. </section>
  932. <section id="path-entry-finder-protocol">
  933. <h3><span class="section-number">5.5.2. </span>Path entry finder protocol<a class="headerlink" href="#path-entry-finder-protocol" title="Link to this heading">¶</a></h3>
  934. <p>In order to support imports of modules and initialized packages and also to
  935. contribute portions to namespace packages, path entry finders must implement
  936. the <a class="reference internal" href="../library/importlib.html#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> method.</p>
  937. <p><a class="reference internal" href="../library/importlib.html#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> takes two arguments: the
  938. fully qualified name of the module being imported, and the (optional) target
  939. module. <code class="docutils literal notranslate"><span class="pre">find_spec()</span></code> returns a fully populated spec for the module.
  940. This spec will always have “loader” set (with one exception).</p>
  941. <p>To indicate to the import machinery that the spec represents a namespace
  942. <a class="reference internal" href="../glossary.html#term-portion"><span class="xref std std-term">portion</span></a>, the path entry finder sets <code class="docutils literal notranslate"><span class="pre">submodule_search_locations</span></code> to
  943. a list containing the portion.</p>
  944. <div class="versionchanged">
  945. <p><span class="versionmodified changed">Changed in version 3.4: </span><a class="reference internal" href="../library/importlib.html#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> replaced
  946. <code class="xref py py-meth docutils literal notranslate"><span class="pre">find_loader()</span></code> and
  947. <code class="xref py py-meth docutils literal notranslate"><span class="pre">find_module()</span></code>, both of which
  948. are now deprecated, but will be used if <code class="docutils literal notranslate"><span class="pre">find_spec()</span></code> is not defined.</p>
  949. <p>Older path entry finders may implement one of these two deprecated methods
  950. instead of <code class="docutils literal notranslate"><span class="pre">find_spec()</span></code>. The methods are still respected for the
  951. sake of backward compatibility. However, if <code class="docutils literal notranslate"><span class="pre">find_spec()</span></code> is
  952. implemented on the path entry finder, the legacy methods are ignored.</p>
  953. <p><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_loader()</span></code> takes one argument, the
  954. fully qualified name of the module being imported. <code class="docutils literal notranslate"><span class="pre">find_loader()</span></code>
  955. returns a 2-tuple where the first item is the loader and the second item
  956. is a namespace <a class="reference internal" href="../glossary.html#term-portion"><span class="xref std std-term">portion</span></a>.</p>
  957. <p>For backwards compatibility with other implementations of the import
  958. protocol, many path entry finders also support the same,
  959. traditional <code class="docutils literal notranslate"><span class="pre">find_module()</span></code> method that meta path finders support.
  960. However path entry finder <code class="docutils literal notranslate"><span class="pre">find_module()</span></code> methods are never called
  961. with a <code class="docutils literal notranslate"><span class="pre">path</span></code> argument (they are expected to record the appropriate
  962. path information from the initial call to the path hook).</p>
  963. <p>The <code class="docutils literal notranslate"><span class="pre">find_module()</span></code> method on path entry finders is deprecated,
  964. as it does not allow the path entry finder to contribute portions to
  965. namespace packages. If both <code class="docutils literal notranslate"><span class="pre">find_loader()</span></code> and <code class="docutils literal notranslate"><span class="pre">find_module()</span></code>
  966. exist on a path entry finder, the import system will always call
  967. <code class="docutils literal notranslate"><span class="pre">find_loader()</span></code> in preference to <code class="docutils literal notranslate"><span class="pre">find_module()</span></code>.</p>
  968. </div>
  969. <div class="versionchanged">
  970. <p><span class="versionmodified changed">Changed in version 3.10: </span>Calls to <code class="xref py py-meth docutils literal notranslate"><span class="pre">find_module()</span></code> and
  971. <code class="xref py py-meth docutils literal notranslate"><span class="pre">find_loader()</span></code> by the import
  972. system will raise <a class="reference internal" href="../library/exceptions.html#ImportWarning" title="ImportWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportWarning</span></code></a>.</p>
  973. </div>
  974. <div class="versionchanged">
  975. <p><span class="versionmodified changed">Changed in version 3.12: </span><code class="docutils literal notranslate"><span class="pre">find_module()</span></code> and <code class="docutils literal notranslate"><span class="pre">find_loader()</span></code> have been removed.</p>
  976. </div>
  977. </section>
  978. </section>
  979. <section id="replacing-the-standard-import-system">
  980. <h2><span class="section-number">5.6. </span>Replacing the standard import system<a class="headerlink" href="#replacing-the-standard-import-system" title="Link to this heading">¶</a></h2>
  981. <p>The most reliable mechanism for replacing the entire import system is to
  982. delete the default contents of <a class="reference internal" href="../library/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>, replacing them
  983. entirely with a custom meta path hook.</p>
  984. <p>If it is acceptable to only alter the behaviour of import statements
  985. without affecting other APIs that access the import system, then replacing
  986. the builtin <a class="reference internal" href="../library/functions.html#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> function may be sufficient. This technique
  987. may also be employed at the module level to only alter the behaviour of
  988. import statements within that module.</p>
  989. <p>To selectively prevent the import of some modules from a hook early on the
  990. meta path (rather than disabling the standard import system entirely),
  991. it is sufficient to raise <a class="reference internal" href="../library/exceptions.html#ModuleNotFoundError" title="ModuleNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ModuleNotFoundError</span></code></a> directly from
  992. <a class="reference internal" href="../library/importlib.html#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> instead of returning
  993. <code class="docutils literal notranslate"><span class="pre">None</span></code>. The latter indicates that the meta path search should continue,
  994. while raising an exception terminates it immediately.</p>
  995. </section>
  996. <section id="package-relative-imports">
  997. <span id="relativeimports"></span><h2><span class="section-number">5.7. </span>Package Relative Imports<a class="headerlink" href="#package-relative-imports" title="Link to this heading">¶</a></h2>
  998. <p>Relative imports use leading dots. A single leading dot indicates a relative
  999. import, starting with the current package. Two or more leading dots indicate a
  1000. relative import to the parent(s) of the current package, one level per dot
  1001. after the first. For example, given the following package layout:</p>
  1002. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">package</span><span class="o">/</span>
  1003. <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
  1004. <span class="n">subpackage1</span><span class="o">/</span>
  1005. <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
  1006. <span class="n">moduleX</span><span class="o">.</span><span class="n">py</span>
  1007. <span class="n">moduleY</span><span class="o">.</span><span class="n">py</span>
  1008. <span class="n">subpackage2</span><span class="o">/</span>
  1009. <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
  1010. <span class="n">moduleZ</span><span class="o">.</span><span class="n">py</span>
  1011. <span class="n">moduleA</span><span class="o">.</span><span class="n">py</span>
  1012. </pre></div>
  1013. </div>
  1014. <p>In either <code class="docutils literal notranslate"><span class="pre">subpackage1/moduleX.py</span></code> or <code class="docutils literal notranslate"><span class="pre">subpackage1/__init__.py</span></code>,
  1015. the following are valid relative imports:</p>
  1016. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">.moduleY</span> <span class="kn">import</span> <span class="n">spam</span>
  1017. <span class="kn">from</span> <span class="nn">.moduleY</span> <span class="kn">import</span> <span class="n">spam</span> <span class="k">as</span> <span class="n">ham</span>
  1018. <span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">moduleY</span>
  1019. <span class="kn">from</span> <span class="nn">..subpackage1</span> <span class="kn">import</span> <span class="n">moduleY</span>
  1020. <span class="kn">from</span> <span class="nn">..subpackage2.moduleZ</span> <span class="kn">import</span> <span class="n">eggs</span>
  1021. <span class="kn">from</span> <span class="nn">..moduleA</span> <span class="kn">import</span> <span class="n">foo</span>
  1022. </pre></div>
  1023. </div>
  1024. <p>Absolute imports may use either the <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">&lt;&gt;</span></code> or <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">&lt;&gt;</span> <span class="pre">import</span> <span class="pre">&lt;&gt;</span></code>
  1025. syntax, but relative imports may only use the second form; the reason
  1026. for this is that:</p>
  1027. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">XXX.YYY.ZZZ</span>
  1028. </pre></div>
  1029. </div>
  1030. <p>should expose <code class="docutils literal notranslate"><span class="pre">XXX.YYY.ZZZ</span></code> as a usable expression, but .moduleY is
  1031. not a valid expression.</p>
  1032. </section>
  1033. <section id="special-considerations-for-main">
  1034. <span id="import-dunder-main"></span><h2><span class="section-number">5.8. </span>Special considerations for __main__<a class="headerlink" href="#special-considerations-for-main" title="Link to this heading">¶</a></h2>
  1035. <p>The <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> module is a special case relative to Python’s import
  1036. system. As noted <a class="reference internal" href="toplevel_components.html#programs"><span class="std std-ref">elsewhere</span></a>, the <code class="docutils literal notranslate"><span class="pre">__main__</span></code> module
  1037. is directly initialized at interpreter startup, much like <a class="reference internal" href="../library/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> and
  1038. <a class="reference internal" href="../library/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>. However, unlike those two, it doesn’t strictly
  1039. qualify as a built-in module. This is because the manner in which
  1040. <code class="docutils literal notranslate"><span class="pre">__main__</span></code> is initialized depends on the flags and other options with
  1041. which the interpreter is invoked.</p>
  1042. <section id="main-spec">
  1043. <span id="id4"></span><h3><span class="section-number">5.8.1. </span>__main__.__spec__<a class="headerlink" href="#main-spec" title="Link to this heading">¶</a></h3>
  1044. <p>Depending on how <a class="reference internal" href="../library/__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> is initialized, <code class="docutils literal notranslate"><span class="pre">__main__.__spec__</span></code>
  1045. gets set appropriately or to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1046. <p>When Python is started with the <a class="reference internal" href="../using/cmdline.html#cmdoption-m"><code class="xref std std-option docutils literal notranslate"><span class="pre">-m</span></code></a> option, <code class="docutils literal notranslate"><span class="pre">__spec__</span></code> is set
  1047. to the module spec of the corresponding module or package. <code class="docutils literal notranslate"><span class="pre">__spec__</span></code> is
  1048. also populated when the <code class="docutils literal notranslate"><span class="pre">__main__</span></code> module is loaded as part of executing a
  1049. directory, zipfile or other <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> entry.</p>
  1050. <p>In <a class="reference internal" href="../using/cmdline.html#using-on-interface-options"><span class="std std-ref">the remaining cases</span></a>
  1051. <code class="docutils literal notranslate"><span class="pre">__main__.__spec__</span></code> is set to <code class="docutils literal notranslate"><span class="pre">None</span></code>, as the code used to populate the
  1052. <a class="reference internal" href="../library/__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> does not correspond directly with an importable module:</p>
  1053. <ul class="simple">
  1054. <li><p>interactive prompt</p></li>
  1055. <li><p><a class="reference internal" href="../using/cmdline.html#cmdoption-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a> option</p></li>
  1056. <li><p>running from stdin</p></li>
  1057. <li><p>running directly from a source or bytecode file</p></li>
  1058. </ul>
  1059. <p>Note that <code class="docutils literal notranslate"><span class="pre">__main__.__spec__</span></code> is always <code class="docutils literal notranslate"><span class="pre">None</span></code> in the last case,
  1060. <em>even if</em> the file could technically be imported directly as a module
  1061. instead. Use the <a class="reference internal" href="../using/cmdline.html#cmdoption-m"><code class="xref std std-option docutils literal notranslate"><span class="pre">-m</span></code></a> switch if valid module metadata is desired
  1062. in <a class="reference internal" href="../library/__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>.</p>
  1063. <p>Note also that even when <code class="docutils literal notranslate"><span class="pre">__main__</span></code> corresponds with an importable module
  1064. and <code class="docutils literal notranslate"><span class="pre">__main__.__spec__</span></code> is set accordingly, they’re still considered
  1065. <em>distinct</em> modules. This is due to the fact that blocks guarded by
  1066. <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">__name__</span> <span class="pre">==</span> <span class="pre">&quot;__main__&quot;:</span></code> checks only execute when the module is used
  1067. to populate the <code class="docutils literal notranslate"><span class="pre">__main__</span></code> namespace, and not during normal import.</p>
  1068. </section>
  1069. </section>
  1070. <section id="references">
  1071. <h2><span class="section-number">5.9. </span>References<a class="headerlink" href="#references" title="Link to this heading">¶</a></h2>
  1072. <p>The import machinery has evolved considerably since Python’s early days. The
  1073. original <a class="reference external" href="https://www.python.org/doc/essays/packages/">specification for packages</a> is still available to read,
  1074. although some details have changed since the writing of that document.</p>
  1075. <p>The original specification for <a class="reference internal" href="../library/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> was <span class="target" id="index-19"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a>, with
  1076. subsequent extension in <span class="target" id="index-20"></span><a class="pep reference external" href="https://peps.python.org/pep-0420/"><strong>PEP 420</strong></a>.</p>
  1077. <p><span class="target" id="index-21"></span><a class="pep reference external" href="https://peps.python.org/pep-0420/"><strong>PEP 420</strong></a> introduced <a class="reference internal" href="../glossary.html#term-namespace-package"><span class="xref std std-term">namespace packages</span></a> for
  1078. Python 3.3. <span class="target" id="index-22"></span><a class="pep reference external" href="https://peps.python.org/pep-0420/"><strong>PEP 420</strong></a> also introduced the <code class="xref py py-meth docutils literal notranslate"><span class="pre">find_loader()</span></code> protocol as an
  1079. alternative to <code class="xref py py-meth docutils literal notranslate"><span class="pre">find_module()</span></code>.</p>
  1080. <p><span class="target" id="index-23"></span><a class="pep reference external" href="https://peps.python.org/pep-0366/"><strong>PEP 366</strong></a> describes the addition of the <code class="docutils literal notranslate"><span class="pre">__package__</span></code> attribute for
  1081. explicit relative imports in main modules.</p>
  1082. <p><span class="target" id="index-24"></span><a class="pep reference external" href="https://peps.python.org/pep-0328/"><strong>PEP 328</strong></a> introduced absolute and explicit relative imports and initially
  1083. proposed <code class="docutils literal notranslate"><span class="pre">__name__</span></code> for semantics <span class="target" id="index-25"></span><a class="pep reference external" href="https://peps.python.org/pep-0366/"><strong>PEP 366</strong></a> would eventually specify for
  1084. <code class="docutils literal notranslate"><span class="pre">__package__</span></code>.</p>
  1085. <p><span class="target" id="index-26"></span><a class="pep reference external" href="https://peps.python.org/pep-0338/"><strong>PEP 338</strong></a> defines executing modules as scripts.</p>
  1086. <p><span class="target" id="index-27"></span><a class="pep reference external" href="https://peps.python.org/pep-0451/"><strong>PEP 451</strong></a> adds the encapsulation of per-module import state in spec
  1087. objects. It also off-loads most of the boilerplate responsibilities of
  1088. loaders back onto the import machinery. These changes allow the
  1089. deprecation of several APIs in the import system and also addition of new
  1090. methods to finders and loaders.</p>
  1091. <p class="rubric">Footnotes</p>
  1092. <aside class="footnote-list brackets">
  1093. <aside class="footnote brackets" id="fnmo" role="doc-footnote">
  1094. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>
  1095. <p>See <a class="reference internal" href="../library/types.html#types.ModuleType" title="types.ModuleType"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.ModuleType</span></code></a>.</p>
  1096. </aside>
  1097. <aside class="footnote brackets" id="fnlo" role="doc-footnote">
  1098. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id3">2</a><span class="fn-bracket">]</span></span>
  1099. <p>The importlib implementation avoids using the return value
  1100. directly. Instead, it gets the module object by looking the module name up
  1101. in <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>. The indirect effect of this is that an imported
  1102. module may replace itself in <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>. This is
  1103. implementation-specific behavior that is not guaranteed to work in other
  1104. Python implementations.</p>
  1105. </aside>
  1106. </aside>
  1107. </section>
  1108. </section>
  1109. <div class="clearer"></div>
  1110. </div>
  1111. </div>
  1112. </div>
  1113. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1114. <div class="sphinxsidebarwrapper">
  1115. <div>
  1116. <h3><a href="../contents.html">Table of Contents</a></h3>
  1117. <ul>
  1118. <li><a class="reference internal" href="#">5. The import system</a><ul>
  1119. <li><a class="reference internal" href="#importlib">5.1. <code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a></li>
  1120. <li><a class="reference internal" href="#packages">5.2. Packages</a><ul>
  1121. <li><a class="reference internal" href="#regular-packages">5.2.1. Regular packages</a></li>
  1122. <li><a class="reference internal" href="#namespace-packages">5.2.2. Namespace packages</a></li>
  1123. </ul>
  1124. </li>
  1125. <li><a class="reference internal" href="#searching">5.3. Searching</a><ul>
  1126. <li><a class="reference internal" href="#the-module-cache">5.3.1. The module cache</a></li>
  1127. <li><a class="reference internal" href="#finders-and-loaders">5.3.2. Finders and loaders</a></li>
  1128. <li><a class="reference internal" href="#import-hooks">5.3.3. Import hooks</a></li>
  1129. <li><a class="reference internal" href="#the-meta-path">5.3.4. The meta path</a></li>
  1130. </ul>
  1131. </li>
  1132. <li><a class="reference internal" href="#loading">5.4. Loading</a><ul>
  1133. <li><a class="reference internal" href="#loaders">5.4.1. Loaders</a></li>
  1134. <li><a class="reference internal" href="#submodules">5.4.2. Submodules</a></li>
  1135. <li><a class="reference internal" href="#module-spec">5.4.3. Module spec</a></li>
  1136. <li><a class="reference internal" href="#import-related-module-attributes">5.4.4. Import-related module attributes</a></li>
  1137. <li><a class="reference internal" href="#module-path">5.4.5. module.__path__</a></li>
  1138. <li><a class="reference internal" href="#module-reprs">5.4.6. Module reprs</a></li>
  1139. <li><a class="reference internal" href="#cached-bytecode-invalidation">5.4.7. Cached bytecode invalidation</a></li>
  1140. </ul>
  1141. </li>
  1142. <li><a class="reference internal" href="#the-path-based-finder">5.5. The Path Based Finder</a><ul>
  1143. <li><a class="reference internal" href="#path-entry-finders">5.5.1. Path entry finders</a></li>
  1144. <li><a class="reference internal" href="#path-entry-finder-protocol">5.5.2. Path entry finder protocol</a></li>
  1145. </ul>
  1146. </li>
  1147. <li><a class="reference internal" href="#replacing-the-standard-import-system">5.6. Replacing the standard import system</a></li>
  1148. <li><a class="reference internal" href="#package-relative-imports">5.7. Package Relative Imports</a></li>
  1149. <li><a class="reference internal" href="#special-considerations-for-main">5.8. Special considerations for __main__</a><ul>
  1150. <li><a class="reference internal" href="#main-spec">5.8.1. __main__.__spec__</a></li>
  1151. </ul>
  1152. </li>
  1153. <li><a class="reference internal" href="#references">5.9. References</a></li>
  1154. </ul>
  1155. </li>
  1156. </ul>
  1157. </div>
  1158. <div>
  1159. <h4>Previous topic</h4>
  1160. <p class="topless"><a href="executionmodel.html"
  1161. title="previous chapter"><span class="section-number">4. </span>Execution model</a></p>
  1162. </div>
  1163. <div>
  1164. <h4>Next topic</h4>
  1165. <p class="topless"><a href="expressions.html"
  1166. title="next chapter"><span class="section-number">6. </span>Expressions</a></p>
  1167. </div>
  1168. <div role="note" aria-label="source link">
  1169. <h3>This Page</h3>
  1170. <ul class="this-page-menu">
  1171. <li><a href="../bugs.html">Report a Bug</a></li>
  1172. <li>
  1173. <a href="https://github.com/python/cpython/blob/main/Doc/reference/import.rst"
  1174. rel="nofollow">Show Source
  1175. </a>
  1176. </li>
  1177. </ul>
  1178. </div>
  1179. </div>
  1180. <div id="sidebarbutton" title="Collapse sidebar">
  1181. <span>«</span>
  1182. </div>
  1183. </div>
  1184. <div class="clearer"></div>
  1185. </div>
  1186. <div class="related" role="navigation" aria-label="related navigation">
  1187. <h3>Navigation</h3>
  1188. <ul>
  1189. <li class="right" style="margin-right: 10px">
  1190. <a href="../genindex.html" title="General Index"
  1191. >index</a></li>
  1192. <li class="right" >
  1193. <a href="../py-modindex.html" title="Python Module Index"
  1194. >modules</a> |</li>
  1195. <li class="right" >
  1196. <a href="expressions.html" title="6. Expressions"
  1197. >next</a> |</li>
  1198. <li class="right" >
  1199. <a href="executionmodel.html" title="4. Execution model"
  1200. >previous</a> |</li>
  1201. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1202. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1203. <li class="switchers">
  1204. <div class="language_switcher_placeholder"></div>
  1205. <div class="version_switcher_placeholder"></div>
  1206. </li>
  1207. <li>
  1208. </li>
  1209. <li id="cpython-language-and-version">
  1210. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1211. </li>
  1212. <li class="nav-item nav-item-1"><a href="index.html" >The Python Language Reference</a> &#187;</li>
  1213. <li class="nav-item nav-item-this"><a href=""><span class="section-number">5. </span>The import system</a></li>
  1214. <li class="right">
  1215. <div class="inline-search" role="search">
  1216. <form class="inline-search" action="../search.html" method="get">
  1217. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1218. <input type="submit" value="Go" />
  1219. </form>
  1220. </div>
  1221. |
  1222. </li>
  1223. <li class="right">
  1224. <label class="theme-selector-label">
  1225. Theme
  1226. <select class="theme-selector" oninput="activateTheme(this.value)">
  1227. <option value="auto" selected>Auto</option>
  1228. <option value="light">Light</option>
  1229. <option value="dark">Dark</option>
  1230. </select>
  1231. </label> |</li>
  1232. </ul>
  1233. </div>
  1234. <div class="footer">
  1235. &copy;
  1236. <a href="../copyright.html">
  1237. Copyright
  1238. </a>
  1239. 2001-2024, Python Software Foundation.
  1240. <br />
  1241. This page is licensed under the Python Software Foundation License Version 2.
  1242. <br />
  1243. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1244. <br />
  1245. See <a href="/license.html">History and License</a> for more information.<br />
  1246. <br />
  1247. The Python Software Foundation is a non-profit corporation.
  1248. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1249. <br />
  1250. <br />
  1251. Last updated on Apr 09, 2024 (13:47 UTC).
  1252. <a href="/bugs.html">Found a bug</a>?
  1253. <br />
  1254. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1255. </div>
  1256. </body>
  1257. </html>
上海开阖软件有限公司 沪ICP备12045867号-1