parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="pkgutil — Package extension utility" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/pkgutil.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/pkgutil.py This module provides utilities for the import system, in particular package support." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_pkgutil_c0124e2d.png" />15<meta property="og:image:alt" content="Source code: Lib/pkgutil.py This module provides utilities for the import system, in particular package support." />16<meta name="description" content="Source code: Lib/pkgutil.py This module provides utilities for the import system, in particular package support." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>pkgutil — Package extension utility — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="modulefinder — Find modules used by a script" href="modulefinder.html" />43 <link rel="prev" title="zipimport — Import modules from Zip archives" href="zipimport.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/pkgutil.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h4>Previous topic</h4>106 <p class="topless"><a href="zipimport.html"107 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>108 </div>109 <div>110 <h4>Next topic</h4>111 <p class="topless"><a href="modulefinder.html"112 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>113 </div>114 <script>115 document.addEventListener('DOMContentLoaded', () => {116 const title = document.querySelector('meta[property="og:title"]').content;117 const elements = document.querySelectorAll('.improvepage');118 const pageurl = window.location.href.split('?')[0];119 elements.forEach(element => {120 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));121 url.searchParams.set('pagetitle', title);122 url.searchParams.set('pageurl', pageurl);123 url.searchParams.set('pagesource', "library/pkgutil.rst");124 element.href = url.toString();125 });126 });127 </script>128 <div role="note" aria-label="source link">129 <h3>This page</h3>130 <ul class="this-page-menu">131 <li><a href="../bugs.html">Report a bug</a></li>132 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>133 <li>134 <a href="https://github.com/python/cpython/blob/main/Doc/library/pkgutil.rst?plain=1"135 rel="nofollow">Show source136 </a>137 </li>138 139 </ul>140 </div>141 </nav>142 </div>143</div>144 145 146 <div class="related" role="navigation" aria-label="Related">147 <h3>Navigation</h3>148 <ul>149 <li class="right" style="margin-right: 10px">150 <a href="../genindex.html" title="General Index"151 accesskey="I">index</a></li>152 <li class="right" >153 <a href="../py-modindex.html" title="Python Module Index"154 >modules</a> |</li>155 <li class="right" >156 <a href="modulefinder.html" title="modulefinder — Find modules used by a script"157 accesskey="N">next</a> |</li>158 <li class="right" >159 <a href="zipimport.html" title="zipimport — Import modules from Zip archives"160 accesskey="P">previous</a> |</li>161 162 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>163 <li><a href="https://www.python.org/">Python</a> »</li>164 <li class="switchers">165 <div class="language_switcher_placeholder"></div>166 <div class="version_switcher_placeholder"></div>167 </li>168 <li>169 170 </li>171 <li id="cpython-language-and-version">172 <a href="../index.html">3.15.0a6 Documentation</a> »173 </li>174 175 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>176 <li class="nav-item nav-item-2"><a href="modules.html" accesskey="U">Importing Modules</a> »</li>177 <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>178 <li class="right">179 180 181 <div class="inline-search" role="search">182 <form class="inline-search" action="../search.html" method="get">183 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">184 <input type="submit" value="Go">185 </form>186 </div>187 |188 </li>189 <li class="right">190<label class="theme-selector-label">191 Theme192 <select class="theme-selector" oninput="activateTheme(this.value)">193 <option value="auto" selected>Auto</option>194 <option value="light">Light</option>195 <option value="dark">Dark</option>196 </select>197</label> |</li>198 199 </ul>200 </div> 201 202 <div class="document">203 <div class="documentwrapper">204 <div class="bodywrapper">205 <div class="body" role="main">206 207 <section id="module-pkgutil">208<span id="pkgutil-package-extension-utility"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">pkgutil</span></code> — Package extension utility<a class="headerlink" href="#module-pkgutil" title="Link to this heading">¶</a></h1>209<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/pkgutil.py">Lib/pkgutil.py</a></p>210<hr class="docutils" />211<p>This module provides utilities for the import system, in particular package212support.</p>213<dl class="py class">214<dt class="sig sig-object py" id="pkgutil.ModuleInfo">215<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">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>216<dd><p>A namedtuple that holds a brief summary of a module’s info.</p>217<div class="versionadded">218<p><span class="versionmodified added">Added in version 3.6.</span></p>219</div>220</dd></dl>221 222<dl class="py function">223<dt class="sig sig-object py" id="pkgutil.extend_path">224<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>225<dd><p>Extend the search path for the modules which comprise a package. Intended226use is to place the following code in a package’s <code class="file docutils literal notranslate"><span class="pre">__init__.py</span></code>:</p>227<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">pkgutil</span><span class="w"> </span><span class="kn">import</span> <span class="n">extend_path</span>228<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>229</pre></div>230</div>231<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 the232package name, add the subdirectory to the package’s233<a class="reference internal" href="../reference/datamodel.html#module.__path__" title="module.__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__path__</span></code></a>. This is useful234if one wants to distribute different parts of a single logical package as multiple235directories.</p>236<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 the237<em>name</em> argument. This feature is similar to <code class="file docutils literal notranslate"><span class="pre">*.pth</span></code> files (see the238<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-case239lines 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 face240value: apart from skipping blank lines and ignoring comments, all entries241found in a <code class="file docutils literal notranslate"><span class="pre">*.pkg</span></code> file are added to the path, regardless of whether242they exist on the filesystem (this is a feature).</p>243<p>If the input path is not a list (as is the case for frozen packages) it is244returned unchanged. The input path is not modified; an extended copy is245returned. Items are only appended to the copy at the end.</p>246<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 <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code>247that are not strings referring to existing directories are ignored. Unicode248items on <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code> that cause errors when used as filenames may cause249this 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>250behavior).</p>251</dd></dl>252 253<dl class="py function">254<dt class="sig sig-object py" id="pkgutil.get_importer">255<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>256<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>257<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 was258newly created by a path hook.</p>259<p>The cache (or part of it) can be cleared manually if a rescan of260<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>261<div class="versionchanged">262<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 relying263on 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>264</div>265</dd></dl>266 267<dl class="py function">268<dt class="sig sig-object py" id="pkgutil.iter_importers">269<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>270<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>271<p>If <em>fullname</em> contains a <code class="docutils literal notranslate"><span class="pre">'.'</span></code>, the finders will be for the package272containing <em>fullname</em>, otherwise they will be all registered top level273finders (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>274<p>If the named module is in a package, that package is imported as a side275effect of invoking this function.</p>276<p>If no module name is specified, all top level finders are produced.</p>277<div class="versionchanged">278<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 relying279on the package internal <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a> import emulation.</p>280</div>281</dd></dl>282 283<dl class="py function">284<dt class="sig sig-object py" id="pkgutil.iter_modules">285<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>286<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, if287<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>288<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>289<p><em>prefix</em> is a string to output on the front of every module name on output.</p>290<div class="admonition note">291<p class="admonition-title">Note</p>292<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>293method. This interface is non-standard, so the module also provides294implementations 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> and295<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>296</div>297<div class="versionchanged">298<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 relying299on 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>300</div>301</dd></dl>302 303<dl class="py function">304<dt class="sig sig-object py" id="pkgutil.walk_packages">305<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>306<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 on307<em>path</em>, or, if <em>path</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, all accessible modules.</p>308<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>309<p><em>prefix</em> is a string to output on the front of every module name on output.</p>310<p>Note that this function must import all <em>packages</em> (<em>not</em> all modules!) on311the given <em>path</em>, in order to access the <code class="docutils literal notranslate"><span class="pre">__path__</span></code> attribute to find312submodules.</p>313<p><em>onerror</em> is a function which gets called with one argument (the name of the314package which was being imported) if any exception occurs while trying to315import 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>s316are caught and ignored, while all other exceptions are propagated,317terminating the search.</p>318<p>Examples:</p>319<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># list all modules python can access</span>320<span class="n">walk_packages</span><span class="p">()</span>321 322<span class="c1"># list all submodules of ctypes</span>323<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">'.'</span><span class="p">)</span>324</pre></div>325</div>326<div class="admonition note">327<p class="admonition-title">Note</p>328<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>329method. This interface is non-standard, so the module also provides330implementations 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> and331<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>332</div>333<div class="versionchanged">334<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 relying335on 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>336</div>337</dd></dl>338 339<dl class="py function">340<dt class="sig sig-object py" id="pkgutil.get_data">341<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>342<dd><p>Get a resource from a package.</p>343<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>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> API. The345<em>package</em> argument should be the name of a package, in standard module format346(<code class="docutils literal notranslate"><span class="pre">foo.bar</span></code>). The <em>resource</em> argument should be in the form of a relative347filename, using <code class="docutils literal notranslate"><span class="pre">/</span></code> as the path separator. The parent directory name348<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>349<p>The function returns a binary string that is the contents of the specified350resource.</p>351<p>For packages located in the filesystem, which have already been imported,352this is the rough equivalent of:</p>353<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>354<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">'rb'</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>355</pre></div>356</div>357<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>358which 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>,359then <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned. In particular, the <span class="xref std std-term">loader</span> for360<a class="reference internal" href="../glossary.html#term-namespace-package"><span class="xref std std-term">namespace packages</span></a> does not support361<code class="xref py py-meth docutils literal notranslate"><span class="pre">get_data</span></code>.</p>362</dd></dl>363 364<dl class="py function">365<dt class="sig sig-object py" id="pkgutil.resolve_name">366<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>367<dd><p>Resolve a name to an object.</p>368<p>This functionality is used in numerous places in the standard library (see369<a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=12915">bpo-12915</a>) - and equivalent functionality is also in widely used370third-party packages such as setuptools, Django and Pyramid.</p>371<p>It is expected that <em>name</em> will be a string in one of the following372formats, where W is shorthand for a valid Python identifier and dot stands373for a literal period in these pseudo-regexes:</p>374<ul class="simple">375<li><p><code class="docutils literal notranslate"><span class="pre">W(.W)*</span></code></p></li>376<li><p><code class="docutils literal notranslate"><span class="pre">W(.W)*:(W(.W)*)?</span></code></p></li>377</ul>378<p>The first form is intended for backward compatibility only. It assumes that379some part of the dotted name is a package, and the rest is an object380somewhere within that package, possibly nested inside other objects.381Because the place where the package stops and the object hierarchy starts382can’t be inferred by inspection, repeated attempts to import must be done383with this form.</p>384<p>In the second form, the caller makes the division point clear through the385provision of a single colon: the dotted name to the left of the colon is a386package to be imported, and the dotted name to the right is the object387hierarchy within that package. Only one import is needed in this form. If388it ends with the colon, then a module object is returned.</p>389<p>The function will return an object (which might be a module), or raise one390of the following exceptions:</p>391<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>392<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>393<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 object394hierarchy within the imported package to get to the desired object.</p>395<div class="versionadded">396<p><span class="versionmodified added">Added in version 3.9.</span></p>397</div>398</dd></dl>399 400</section>401 402 403 <div class="clearer"></div>404 </div>405 </div>406 </div>407 <div class="sphinxsidebar" role="navigation" aria-label="Main">408 <div class="sphinxsidebarwrapper">409 <div>410 <h4>Previous topic</h4>411 <p class="topless"><a href="zipimport.html"412 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>413 </div>414 <div>415 <h4>Next topic</h4>416 <p class="topless"><a href="modulefinder.html"417 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>418 </div>419 <script>420 document.addEventListener('DOMContentLoaded', () => {421 const title = document.querySelector('meta[property="og:title"]').content;422 const elements = document.querySelectorAll('.improvepage');423 const pageurl = window.location.href.split('?')[0];424 elements.forEach(element => {425 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));426 url.searchParams.set('pagetitle', title);427 url.searchParams.set('pageurl', pageurl);428 url.searchParams.set('pagesource', "library/pkgutil.rst");429 element.href = url.toString();430 });431 });432 </script>433 <div role="note" aria-label="source link">434 <h3>This page</h3>435 <ul class="this-page-menu">436 <li><a href="../bugs.html">Report a bug</a></li>437 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>438 <li>439 <a href="https://github.com/python/cpython/blob/main/Doc/library/pkgutil.rst?plain=1"440 rel="nofollow">Show source441 </a>442 </li>443 444 </ul>445 </div>446 </div>447<div id="sidebarbutton" title="Collapse sidebar">448<span>«</span>449</div>450 451 </div>452 <div class="clearer"></div>453 </div> 454 <div class="related" role="navigation" aria-label="Related">455 <h3>Navigation</h3>456 <ul>457 <li class="right" style="margin-right: 10px">458 <a href="../genindex.html" title="General Index"459 >index</a></li>460 <li class="right" >461 <a href="../py-modindex.html" title="Python Module Index"462 >modules</a> |</li>463 <li class="right" >464 <a href="modulefinder.html" title="modulefinder — Find modules used by a script"465 >next</a> |</li>466 <li class="right" >467 <a href="zipimport.html" title="zipimport — Import modules from Zip archives"468 >previous</a> |</li>469 470 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>471 <li><a href="https://www.python.org/">Python</a> »</li>472 <li class="switchers">473 <div class="language_switcher_placeholder"></div>474 <div class="version_switcher_placeholder"></div>475 </li>476 <li>477 478 </li>479 <li id="cpython-language-and-version">480 <a href="../index.html">3.15.0a6 Documentation</a> »481 </li>482 483 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>484 <li class="nav-item nav-item-2"><a href="modules.html" >Importing Modules</a> »</li>485 <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>486 <li class="right">487 488 489 <div class="inline-search" role="search">490 <form class="inline-search" action="../search.html" method="get">491 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">492 <input type="submit" value="Go">493 </form>494 </div>495 |496 </li>497 <li class="right">498<label class="theme-selector-label">499 Theme500 <select class="theme-selector" oninput="activateTheme(this.value)">501 <option value="auto" selected>Auto</option>502 <option value="light">Light</option>503 <option value="dark">Dark</option>504 </select>505</label> |</li>506 507 </ul>508 </div> 509 <div class="footer">510 © <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.511 <br>512 This page is licensed under the Python Software Foundation License Version 2.513 <br>514 Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.515 <br>516 517 See <a href="/license.html">History and License</a> for more information.<br>518 519 520 <br>521 522 The Python Software Foundation is a non-profit corporation.523<a href="https://www.python.org/psf/donations/">Please donate.</a>524<br>525 <br>526 Last updated on Mar 10, 2026 (08:58 UTC).527 528 <a href="/bugs.html">Found a bug</a>?529 530 <br>531 532 Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.533 </div>534 535 </body>536</html>