gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

541 行
42KB

  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="pkgutil — Package extension utility" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/pkgutil.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/pkgutil.py This module provides utilities for the import system, in particular package support." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/pkgutil.py This module provides utilities for the import system, in particular package support." />
  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>pkgutil — Package extension utility &#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="modulefinder — Find modules used by a script" href="modulefinder.html" />
  33. <link rel="prev" title="zipimport — Import modules from Zip archives" href="zipimport.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/pkgutil.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h4>Previous topic</h4>
  84. <p class="topless"><a href="zipimport.html"
  85. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code> — Import modules from Zip archives</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="modulefinder.html"
  90. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">modulefinder</span></code> — Find modules used by a script</a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/library/pkgutil.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="modulefinder.html" title="modulefinder — Find modules used by a script"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="zipimport.html" title="zipimport — Import modules from Zip archives"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.3 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  133. <li class="nav-item nav-item-2"><a href="modules.html" accesskey="U">Importing Modules</a> &#187;</li>
  134. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">pkgutil</span></code> — Package extension utility</a></li>
  135. <li class="right">
  136. <div class="inline-search" role="search">
  137. <form class="inline-search" action="../search.html" method="get">
  138. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  139. <input type="submit" value="Go" />
  140. </form>
  141. </div>
  142. |
  143. </li>
  144. <li class="right">
  145. <label class="theme-selector-label">
  146. Theme
  147. <select class="theme-selector" oninput="activateTheme(this.value)">
  148. <option value="auto" selected>Auto</option>
  149. <option value="light">Light</option>
  150. <option value="dark">Dark</option>
  151. </select>
  152. </label> |</li>
  153. </ul>
  154. </div>
  155. <div class="document">
  156. <div class="documentwrapper">
  157. <div class="bodywrapper">
  158. <div class="body" role="main">
  159. <section id="module-pkgutil">
  160. <span id="pkgutil-package-extension-utility"></span><h1><a class="reference internal" href="#module-pkgutil" title="pkgutil: Utilities for the import system."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pkgutil</span></code></a> — Package extension utility<a class="headerlink" href="#module-pkgutil" title="Link to this heading">¶</a></h1>
  161. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/pkgutil.py">Lib/pkgutil.py</a></p>
  162. <hr class="docutils" />
  163. <p>This module provides utilities for the import system, in particular package
  164. support.</p>
  165. <dl class="py class">
  166. <dt class="sig sig-object py" id="pkgutil.ModuleInfo">
  167. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">pkgutil.</span></span><span class="sig-name descname"><span class="pre">ModuleInfo</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module_finder</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ispkg</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.ModuleInfo" title="Link to this definition">¶</a></dt>
  168. <dd><p>A namedtuple that holds a brief summary of a module’s info.</p>
  169. <div class="versionadded">
  170. <p><span class="versionmodified added">New in version 3.6.</span></p>
  171. </div>
  172. </dd></dl>
  173. <dl class="py function">
  174. <dt class="sig sig-object py" id="pkgutil.extend_path">
  175. <span class="sig-prename descclassname"><span class="pre">pkgutil.</span></span><span class="sig-name descname"><span class="pre">extend_path</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.extend_path" title="Link to this definition">¶</a></dt>
  176. <dd><p>Extend the search path for the modules which comprise a package. Intended
  177. use is to place the following code in a package’s <code class="file docutils literal notranslate"><span class="pre">__init__.py</span></code>:</p>
  178. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pkgutil</span> <span class="kn">import</span> <span class="n">extend_path</span>
  179. <span class="n">__path__</span> <span class="o">=</span> <span class="n">extend_path</span><span class="p">(</span><span class="n">__path__</span><span class="p">,</span> <span class="vm">__name__</span><span class="p">)</span>
  180. </pre></div>
  181. </div>
  182. <p>For each directory on <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> that has a subdirectory that matches the
  183. package name, add the subdirectory to the package’s <a class="reference internal" href="../reference/import.html#path__" title="__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__path__</span></code></a>. This is useful
  184. if one wants to distribute different parts of a single logical package as multiple
  185. directories.</p>
  186. <p>It also looks for <code class="file docutils literal notranslate"><span class="pre">*.pkg</span></code> files beginning where <code class="docutils literal notranslate"><span class="pre">*</span></code> matches the
  187. <em>name</em> argument. This feature is similar to <code class="file docutils literal notranslate"><span class="pre">*.pth</span></code> files (see the
  188. <a class="reference internal" href="site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> module for more information), except that it doesn’t special-case
  189. lines starting with <code class="docutils literal notranslate"><span class="pre">import</span></code>. A <code class="file docutils literal notranslate"><span class="pre">*.pkg</span></code> file is trusted at face
  190. value: apart from checking for duplicates, all entries found in a
  191. <code class="file docutils literal notranslate"><span class="pre">*.pkg</span></code> file are added to the path, regardless of whether they exist
  192. on the filesystem. (This is a feature.)</p>
  193. <p>If the input path is not a list (as is the case for frozen packages) it is
  194. returned unchanged. The input path is not modified; an extended copy is
  195. returned. Items are only appended to the copy at the end.</p>
  196. <p>It is assumed that <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> is a sequence. Items of <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>
  197. that are not strings referring to existing directories are ignored. Unicode
  198. items on <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> that cause errors when used as filenames may cause
  199. this function to raise an exception (in line with <a class="reference internal" href="os.path.html#os.path.isdir" title="os.path.isdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.isdir()</span></code></a>
  200. behavior).</p>
  201. </dd></dl>
  202. <dl class="py function">
  203. <dt class="sig sig-object py" id="pkgutil.find_loader">
  204. <span class="sig-prename descclassname"><span class="pre">pkgutil.</span></span><span class="sig-name descname"><span class="pre">find_loader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.find_loader" title="Link to this definition">¶</a></dt>
  205. <dd><p>Retrieve a module <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> for the given <em>fullname</em>.</p>
  206. <p>This is a backwards compatibility wrapper around
  207. <a class="reference internal" href="importlib.html#importlib.util.find_spec" title="importlib.util.find_spec"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.find_spec()</span></code></a> that converts most failures to
  208. <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> and only returns the loader rather than the full
  209. <a class="reference internal" href="importlib.html#importlib.machinery.ModuleSpec" title="importlib.machinery.ModuleSpec"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.ModuleSpec</span></code></a>.</p>
  210. <div class="versionchanged">
  211. <p><span class="versionmodified changed">Changed in version 3.3: </span>Updated to be based directly on <a class="reference internal" href="importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> rather than relying
  212. on the package internal <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a> import emulation.</p>
  213. </div>
  214. <div class="versionchanged">
  215. <p><span class="versionmodified changed">Changed in version 3.4: </span>Updated to be based on <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0451/"><strong>PEP 451</strong></a></p>
  216. </div>
  217. <div class="deprecated-removed">
  218. <p><span class="versionmodified">Deprecated since version 3.12, will be removed in version 3.14: </span>Use <a class="reference internal" href="importlib.html#importlib.util.find_spec" title="importlib.util.find_spec"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.find_spec()</span></code></a> instead.</p>
  219. </div>
  220. </dd></dl>
  221. <dl class="py function">
  222. <dt class="sig sig-object py" id="pkgutil.get_importer">
  223. <span class="sig-prename descclassname"><span class="pre">pkgutil.</span></span><span class="sig-name descname"><span class="pre">get_importer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path_item</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.get_importer" title="Link to this definition">¶</a></dt>
  224. <dd><p>Retrieve a <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finder</span></a> for the given <em>path_item</em>.</p>
  225. <p>The returned finder is cached in <a class="reference internal" href="sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a> if it was
  226. newly created by a path hook.</p>
  227. <p>The cache (or part of it) can be cleared manually if a rescan of
  228. <a class="reference internal" href="sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a> is necessary.</p>
  229. <div class="versionchanged">
  230. <p><span class="versionmodified changed">Changed in version 3.3: </span>Updated to be based directly on <a class="reference internal" href="importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> rather than relying
  231. on the package internal <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a> import emulation.</p>
  232. </div>
  233. </dd></dl>
  234. <dl class="py function">
  235. <dt class="sig sig-object py" id="pkgutil.get_loader">
  236. <span class="sig-prename descclassname"><span class="pre">pkgutil.</span></span><span class="sig-name descname"><span class="pre">get_loader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module_or_name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.get_loader" title="Link to this definition">¶</a></dt>
  237. <dd><p>Get a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> object for <em>module_or_name</em>.</p>
  238. <p>If the module or package is accessible via the normal import mechanism, a
  239. wrapper around the relevant part of that machinery is returned. Returns
  240. <code class="docutils literal notranslate"><span class="pre">None</span></code> if the module cannot be found or imported. If the named module is
  241. not already imported, its containing package (if any) is imported, in order
  242. to establish the package <code class="docutils literal notranslate"><span class="pre">__path__</span></code>.</p>
  243. <div class="versionchanged">
  244. <p><span class="versionmodified changed">Changed in version 3.3: </span>Updated to be based directly on <a class="reference internal" href="importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> rather than relying
  245. on the package internal <span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a> import emulation.</p>
  246. </div>
  247. <div class="versionchanged">
  248. <p><span class="versionmodified changed">Changed in version 3.4: </span>Updated to be based on <span class="target" id="index-4"></span><a class="pep reference external" href="https://peps.python.org/pep-0451/"><strong>PEP 451</strong></a></p>
  249. </div>
  250. <div class="deprecated-removed">
  251. <p><span class="versionmodified">Deprecated since version 3.12, will be removed in version 3.14: </span>Use <a class="reference internal" href="importlib.html#importlib.util.find_spec" title="importlib.util.find_spec"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.find_spec()</span></code></a> instead.</p>
  252. </div>
  253. </dd></dl>
  254. <dl class="py function">
  255. <dt class="sig sig-object py" id="pkgutil.iter_importers">
  256. <span class="sig-prename descclassname"><span class="pre">pkgutil.</span></span><span class="sig-name descname"><span class="pre">iter_importers</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fullname</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.iter_importers" title="Link to this definition">¶</a></dt>
  257. <dd><p>Yield <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finder</span></a> objects for the given module name.</p>
  258. <p>If fullname contains a <code class="docutils literal notranslate"><span class="pre">'.'</span></code>, the finders will be for the package
  259. containing fullname, otherwise they will be all registered top level
  260. finders (i.e. those on both <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a> and <a class="reference internal" href="sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a>).</p>
  261. <p>If the named module is in a package, that package is imported as a side
  262. effect of invoking this function.</p>
  263. <p>If no module name is specified, all top level finders are produced.</p>
  264. <div class="versionchanged">
  265. <p><span class="versionmodified changed">Changed in version 3.3: </span>Updated to be based directly on <a class="reference internal" href="importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> rather than relying
  266. on the package internal <span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a> import emulation.</p>
  267. </div>
  268. </dd></dl>
  269. <dl class="py function">
  270. <dt class="sig sig-object py" id="pkgutil.iter_modules">
  271. <span class="sig-prename descclassname"><span class="pre">pkgutil.</span></span><span class="sig-name descname"><span class="pre">iter_modules</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prefix</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.iter_modules" title="Link to this definition">¶</a></dt>
  272. <dd><p>Yields <a class="reference internal" href="#pkgutil.ModuleInfo" title="pkgutil.ModuleInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModuleInfo</span></code></a> for all submodules on <em>path</em>, or, if
  273. <em>path</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, all top-level modules on <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>.</p>
  274. <p><em>path</em> should be either <code class="docutils literal notranslate"><span class="pre">None</span></code> or a list of paths to look for modules in.</p>
  275. <p><em>prefix</em> is a string to output on the front of every module name on output.</p>
  276. <div class="admonition note">
  277. <p class="admonition-title">Note</p>
  278. <p>Only works for a <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finder</span></a> which defines an <code class="docutils literal notranslate"><span class="pre">iter_modules()</span></code>
  279. method. This interface is non-standard, so the module also provides
  280. implementations for <a class="reference internal" href="importlib.html#importlib.machinery.FileFinder" title="importlib.machinery.FileFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.FileFinder</span></code></a> and
  281. <a class="reference internal" href="zipimport.html#zipimport.zipimporter" title="zipimport.zipimporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">zipimport.zipimporter</span></code></a>.</p>
  282. </div>
  283. <div class="versionchanged">
  284. <p><span class="versionmodified changed">Changed in version 3.3: </span>Updated to be based directly on <a class="reference internal" href="importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> rather than relying
  285. on the package internal <span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a> import emulation.</p>
  286. </div>
  287. </dd></dl>
  288. <dl class="py function">
  289. <dt class="sig sig-object py" id="pkgutil.walk_packages">
  290. <span class="sig-prename descclassname"><span class="pre">pkgutil.</span></span><span class="sig-name descname"><span class="pre">walk_packages</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prefix</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">onerror</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.walk_packages" title="Link to this definition">¶</a></dt>
  291. <dd><p>Yields <a class="reference internal" href="#pkgutil.ModuleInfo" title="pkgutil.ModuleInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModuleInfo</span></code></a> for all modules recursively on
  292. <em>path</em>, or, if <em>path</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, all accessible modules.</p>
  293. <p><em>path</em> should be either <code class="docutils literal notranslate"><span class="pre">None</span></code> or a list of paths to look for modules in.</p>
  294. <p><em>prefix</em> is a string to output on the front of every module name on output.</p>
  295. <p>Note that this function must import all <em>packages</em> (<em>not</em> all modules!) on
  296. the given <em>path</em>, in order to access the <code class="docutils literal notranslate"><span class="pre">__path__</span></code> attribute to find
  297. submodules.</p>
  298. <p><em>onerror</em> is a function which gets called with one argument (the name of the
  299. package which was being imported) if any exception occurs while trying to
  300. import a package. If no <em>onerror</em> function is supplied, <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a>s
  301. are caught and ignored, while all other exceptions are propagated,
  302. terminating the search.</p>
  303. <p>Examples:</p>
  304. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># list all modules python can access</span>
  305. <span class="n">walk_packages</span><span class="p">()</span>
  306. <span class="c1"># list all submodules of ctypes</span>
  307. <span class="n">walk_packages</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">__path__</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">+</span> <span class="s1">&#39;.&#39;</span><span class="p">)</span>
  308. </pre></div>
  309. </div>
  310. <div class="admonition note">
  311. <p class="admonition-title">Note</p>
  312. <p>Only works for a <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finder</span></a> which defines an <code class="docutils literal notranslate"><span class="pre">iter_modules()</span></code>
  313. method. This interface is non-standard, so the module also provides
  314. implementations for <a class="reference internal" href="importlib.html#importlib.machinery.FileFinder" title="importlib.machinery.FileFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.FileFinder</span></code></a> and
  315. <a class="reference internal" href="zipimport.html#zipimport.zipimporter" title="zipimport.zipimporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">zipimport.zipimporter</span></code></a>.</p>
  316. </div>
  317. <div class="versionchanged">
  318. <p><span class="versionmodified changed">Changed in version 3.3: </span>Updated to be based directly on <a class="reference internal" href="importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> rather than relying
  319. on the package internal <span class="target" id="index-7"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a> import emulation.</p>
  320. </div>
  321. </dd></dl>
  322. <dl class="py function">
  323. <dt class="sig sig-object py" id="pkgutil.get_data">
  324. <span class="sig-prename descclassname"><span class="pre">pkgutil.</span></span><span class="sig-name descname"><span class="pre">get_data</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">package</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">resource</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.get_data" title="Link to this definition">¶</a></dt>
  325. <dd><p>Get a resource from a package.</p>
  326. <p>This is a wrapper for the <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a>
  327. <a class="reference internal" href="importlib.html#importlib.abc.ResourceLoader.get_data" title="importlib.abc.ResourceLoader.get_data"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_data</span></code></a> API. The
  328. <em>package</em> argument should be the name of a package, in standard module format
  329. (<code class="docutils literal notranslate"><span class="pre">foo.bar</span></code>). The <em>resource</em> argument should be in the form of a relative
  330. filename, using <code class="docutils literal notranslate"><span class="pre">/</span></code> as the path separator. The parent directory name
  331. <code class="docutils literal notranslate"><span class="pre">..</span></code> is not allowed, and nor is a rooted name (starting with a <code class="docutils literal notranslate"><span class="pre">/</span></code>).</p>
  332. <p>The function returns a binary string that is the contents of the specified
  333. resource.</p>
  334. <p>For packages located in the filesystem, which have already been imported,
  335. this is the rough equivalent of:</p>
  336. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">d</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">package</span><span class="p">]</span><span class="o">.</span><span class="vm">__file__</span><span class="p">)</span>
  337. <span class="n">data</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">resource</span><span class="p">),</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  338. </pre></div>
  339. </div>
  340. <p>If the package cannot be located or loaded, or it uses a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a>
  341. which does not support <a class="reference internal" href="importlib.html#importlib.abc.ResourceLoader.get_data" title="importlib.abc.ResourceLoader.get_data"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_data</span></code></a>,
  342. then <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned. In particular, the <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> for
  343. <a class="reference internal" href="../glossary.html#term-namespace-package"><span class="xref std std-term">namespace packages</span></a> does not support
  344. <a class="reference internal" href="importlib.html#importlib.abc.ResourceLoader.get_data" title="importlib.abc.ResourceLoader.get_data"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_data</span></code></a>.</p>
  345. </dd></dl>
  346. <dl class="py function">
  347. <dt class="sig sig-object py" id="pkgutil.resolve_name">
  348. <span class="sig-prename descclassname"><span class="pre">pkgutil.</span></span><span class="sig-name descname"><span class="pre">resolve_name</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pkgutil.resolve_name" title="Link to this definition">¶</a></dt>
  349. <dd><p>Resolve a name to an object.</p>
  350. <p>This functionality is used in numerous places in the standard library (see
  351. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12915">bpo-12915</a>) - and equivalent functionality is also in widely used
  352. third-party packages such as setuptools, Django and Pyramid.</p>
  353. <p>It is expected that <em>name</em> will be a string in one of the following
  354. formats, where W is shorthand for a valid Python identifier and dot stands
  355. for a literal period in these pseudo-regexes:</p>
  356. <ul class="simple">
  357. <li><p><code class="docutils literal notranslate"><span class="pre">W(.W)*</span></code></p></li>
  358. <li><p><code class="docutils literal notranslate"><span class="pre">W(.W)*:(W(.W)*)?</span></code></p></li>
  359. </ul>
  360. <p>The first form is intended for backward compatibility only. It assumes that
  361. some part of the dotted name is a package, and the rest is an object
  362. somewhere within that package, possibly nested inside other objects.
  363. Because the place where the package stops and the object hierarchy starts
  364. can’t be inferred by inspection, repeated attempts to import must be done
  365. with this form.</p>
  366. <p>In the second form, the caller makes the division point clear through the
  367. provision of a single colon: the dotted name to the left of the colon is a
  368. package to be imported, and the dotted name to the right is the object
  369. hierarchy within that package. Only one import is needed in this form. If
  370. it ends with the colon, then a module object is returned.</p>
  371. <p>The function will return an object (which might be a module), or raise one
  372. of the following exceptions:</p>
  373. <p><a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> – if <em>name</em> isn’t in a recognised format.</p>
  374. <p><a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> – if an import failed when it shouldn’t have.</p>
  375. <p><a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a> – If a failure occurred when traversing the object
  376. hierarchy within the imported package to get to the desired object.</p>
  377. <div class="versionadded">
  378. <p><span class="versionmodified added">New in version 3.9.</span></p>
  379. </div>
  380. </dd></dl>
  381. </section>
  382. <div class="clearer"></div>
  383. </div>
  384. </div>
  385. </div>
  386. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  387. <div class="sphinxsidebarwrapper">
  388. <div>
  389. <h4>Previous topic</h4>
  390. <p class="topless"><a href="zipimport.html"
  391. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code> — Import modules from Zip archives</a></p>
  392. </div>
  393. <div>
  394. <h4>Next topic</h4>
  395. <p class="topless"><a href="modulefinder.html"
  396. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">modulefinder</span></code> — Find modules used by a script</a></p>
  397. </div>
  398. <div role="note" aria-label="source link">
  399. <h3>This Page</h3>
  400. <ul class="this-page-menu">
  401. <li><a href="../bugs.html">Report a Bug</a></li>
  402. <li>
  403. <a href="https://github.com/python/cpython/blob/main/Doc/library/pkgutil.rst"
  404. rel="nofollow">Show Source
  405. </a>
  406. </li>
  407. </ul>
  408. </div>
  409. </div>
  410. <div id="sidebarbutton" title="Collapse sidebar">
  411. <span>«</span>
  412. </div>
  413. </div>
  414. <div class="clearer"></div>
  415. </div>
  416. <div class="related" role="navigation" aria-label="related navigation">
  417. <h3>Navigation</h3>
  418. <ul>
  419. <li class="right" style="margin-right: 10px">
  420. <a href="../genindex.html" title="General Index"
  421. >index</a></li>
  422. <li class="right" >
  423. <a href="../py-modindex.html" title="Python Module Index"
  424. >modules</a> |</li>
  425. <li class="right" >
  426. <a href="modulefinder.html" title="modulefinder — Find modules used by a script"
  427. >next</a> |</li>
  428. <li class="right" >
  429. <a href="zipimport.html" title="zipimport — Import modules from Zip archives"
  430. >previous</a> |</li>
  431. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  432. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  433. <li class="switchers">
  434. <div class="language_switcher_placeholder"></div>
  435. <div class="version_switcher_placeholder"></div>
  436. </li>
  437. <li>
  438. </li>
  439. <li id="cpython-language-and-version">
  440. <a href="../index.html">3.12.3 Documentation</a> &#187;
  441. </li>
  442. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  443. <li class="nav-item nav-item-2"><a href="modules.html" >Importing Modules</a> &#187;</li>
  444. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">pkgutil</span></code> — Package extension utility</a></li>
  445. <li class="right">
  446. <div class="inline-search" role="search">
  447. <form class="inline-search" action="../search.html" method="get">
  448. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  449. <input type="submit" value="Go" />
  450. </form>
  451. </div>
  452. |
  453. </li>
  454. <li class="right">
  455. <label class="theme-selector-label">
  456. Theme
  457. <select class="theme-selector" oninput="activateTheme(this.value)">
  458. <option value="auto" selected>Auto</option>
  459. <option value="light">Light</option>
  460. <option value="dark">Dark</option>
  461. </select>
  462. </label> |</li>
  463. </ul>
  464. </div>
  465. <div class="footer">
  466. &copy;
  467. <a href="../copyright.html">
  468. Copyright
  469. </a>
  470. 2001-2024, Python Software Foundation.
  471. <br />
  472. This page is licensed under the Python Software Foundation License Version 2.
  473. <br />
  474. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  475. <br />
  476. See <a href="/license.html">History and License</a> for more information.<br />
  477. <br />
  478. The Python Software Foundation is a non-profit corporation.
  479. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  480. <br />
  481. <br />
  482. Last updated on Apr 09, 2024 (13:47 UTC).
  483. <a href="/bugs.html">Found a bug</a>?
  484. <br />
  485. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  486. </div>
  487. </body>
  488. </html>
上海开阖软件有限公司 沪ICP备12045867号-1