parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="importlib.metadata – Accessing package metadata" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/importlib.metadata.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/importlib/metadata/__init__.py importlib.metadata is a library that provides access to the metadata of an installed Distribution Package, such as its entry points or its top-level ..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_importlib.metadata_256c90c9.png" />15<meta property="og:image:alt" content="Source code: Lib/importlib/metadata/__init__.py importlib.metadata is a library that provides access to the metadata of an installed Distribution Package, such as its entry points or its top-level ..." />16<meta name="description" content="Source code: Lib/importlib/metadata/__init__.py importlib.metadata is a library that provides access to the metadata of an installed Distribution Package, such as its entry points or its top-level ..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>importlib.metadata – Accessing package metadata — 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="The initialization of the sys.path module search path" href="sys_path_init.html" />43 <link rel="prev" title="importlib.resources.abc – Abstract base classes for resources" href="importlib.resources.abc.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/importlib.metadata.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.metadata</span></code> – Accessing package metadata</a><ul>108<li><a class="reference internal" href="#overview">Overview</a></li>109<li><a class="reference internal" href="#functional-api">Functional API</a><ul>110<li><a class="reference internal" href="#entry-points">Entry points</a></li>111<li><a class="reference internal" href="#distribution-metadata">Distribution metadata</a></li>112<li><a class="reference internal" href="#distribution-versions">Distribution versions</a></li>113<li><a class="reference internal" href="#distribution-files">Distribution files</a></li>114<li><a class="reference internal" href="#distribution-requirements">Distribution requirements</a></li>115<li><a class="reference internal" href="#mapping-import-to-distribution-packages">Mapping import to distribution packages</a></li>116</ul>117</li>118<li><a class="reference internal" href="#distributions">Distributions</a></li>119<li><a class="reference internal" href="#distribution-discovery">Distribution Discovery</a></li>120<li><a class="reference internal" href="#implementing-custom-providers">Implementing Custom Providers</a><ul>121<li><a class="reference internal" href="#example">Example</a></li>122</ul>123</li>124</ul>125</li>126</ul>127 128 </div>129 <div>130 <h4>Previous topic</h4>131 <p class="topless"><a href="importlib.resources.abc.html"132 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.resources.abc</span></code> – Abstract base classes for resources</a></p>133 </div>134 <div>135 <h4>Next topic</h4>136 <p class="topless"><a href="sys_path_init.html"137 title="next chapter">The initialization of the <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code> module search path</a></p>138 </div>139 <script>140 document.addEventListener('DOMContentLoaded', () => {141 const title = document.querySelector('meta[property="og:title"]').content;142 const elements = document.querySelectorAll('.improvepage');143 const pageurl = window.location.href.split('?')[0];144 elements.forEach(element => {145 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));146 url.searchParams.set('pagetitle', title);147 url.searchParams.set('pageurl', pageurl);148 url.searchParams.set('pagesource', "library/importlib.metadata.rst");149 element.href = url.toString();150 });151 });152 </script>153 <div role="note" aria-label="source link">154 <h3>This page</h3>155 <ul class="this-page-menu">156 <li><a href="../bugs.html">Report a bug</a></li>157 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>158 <li>159 <a href="https://github.com/python/cpython/blob/main/Doc/library/importlib.metadata.rst?plain=1"160 rel="nofollow">Show source161 </a>162 </li>163 164 </ul>165 </div>166 </nav>167 </div>168</div>169 170 171 <div class="related" role="navigation" aria-label="Related">172 <h3>Navigation</h3>173 <ul>174 <li class="right" style="margin-right: 10px">175 <a href="../genindex.html" title="General Index"176 accesskey="I">index</a></li>177 <li class="right" >178 <a href="../py-modindex.html" title="Python Module Index"179 >modules</a> |</li>180 <li class="right" >181 <a href="sys_path_init.html" title="The initialization of the sys.path module search path"182 accesskey="N">next</a> |</li>183 <li class="right" >184 <a href="importlib.resources.abc.html" title="importlib.resources.abc – Abstract base classes for resources"185 accesskey="P">previous</a> |</li>186 187 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>188 <li><a href="https://www.python.org/">Python</a> »</li>189 <li class="switchers">190 <div class="language_switcher_placeholder"></div>191 <div class="version_switcher_placeholder"></div>192 </li>193 <li>194 195 </li>196 <li id="cpython-language-and-version">197 <a href="../index.html">3.15.0a6 Documentation</a> »198 </li>199 200 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>201 <li class="nav-item nav-item-2"><a href="modules.html" accesskey="U">Importing Modules</a> »</li>202 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.metadata</span></code> – Accessing package metadata</a></li>203 <li class="right">204 205 206 <div class="inline-search" role="search">207 <form class="inline-search" action="../search.html" method="get">208 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">209 <input type="submit" value="Go">210 </form>211 </div>212 |213 </li>214 <li class="right">215<label class="theme-selector-label">216 Theme217 <select class="theme-selector" oninput="activateTheme(this.value)">218 <option value="auto" selected>Auto</option>219 <option value="light">Light</option>220 <option value="dark">Dark</option>221 </select>222</label> |</li>223 224 </ul>225 </div> 226 227 <div class="document">228 <div class="documentwrapper">229 <div class="bodywrapper">230 <div class="body" role="main">231 232 <section id="module-importlib.metadata">233<span id="importlib-metadata-accessing-package-metadata"></span><span id="using"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.metadata</span></code> – Accessing package metadata<a class="headerlink" href="#module-importlib.metadata" title="Link to this heading">¶</a></h1>234<div class="versionadded">235<p><span class="versionmodified added">Added in version 3.8.</span></p>236</div>237<div class="versionchanged">238<p><span class="versionmodified changed">Changed in version 3.10: </span><code class="docutils literal notranslate"><span class="pre">importlib.metadata</span></code> is no longer provisional.</p>239</div>240<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/importlib/metadata/__init__.py">Lib/importlib/metadata/__init__.py</a></p>241<p><code class="docutils literal notranslate"><span class="pre">importlib.metadata</span></code> is a library that provides access to242the metadata of an installed <a class="reference external" href="https://packaging.python.org/en/latest/glossary/#term-Distribution-Package">Distribution Package</a>,243such as its entry points244or its top-level names (<a class="reference external" href="https://packaging.python.org/en/latest/glossary/#term-Import-Package">Import Package</a>s, modules, if any).245Built in part on Python’s import system, this library246intends to replace similar functionality in the <a class="reference external" href="https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points">entry point247API</a> and <a class="reference external" href="https://setuptools.readthedocs.io/en/latest/pkg_resources.html#metadata-api">metadata API</a> of <code class="docutils literal notranslate"><span class="pre">pkg_resources</span></code>. Along with248<a class="reference internal" href="importlib.resources.html#module-importlib.resources" title="importlib.resources: Package resource reading, opening, and access"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.resources</span></code></a>,249this package can eliminate the need to use the older and less efficient250<code class="docutils literal notranslate"><span class="pre">pkg_resources</span></code> package.</p>251<p><code class="docutils literal notranslate"><span class="pre">importlib.metadata</span></code> operates on third-party <em>distribution packages</em>252installed into Python’s <code class="docutils literal notranslate"><span class="pre">site-packages</span></code> directory via tools such as253<a class="extlink-pypi reference external" href="https://pypi.org/project/pip/">pip</a>.254Specifically, it works with distributions with discoverable255<code class="docutils literal notranslate"><span class="pre">dist-info</span></code> or <code class="docutils literal notranslate"><span class="pre">egg-info</span></code> directories,256and metadata defined by the <a class="reference external" href="https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata">Core metadata specifications</a>.</p>257<div class="admonition important">258<p class="admonition-title">Important</p>259<p>These are <em>not</em> necessarily equivalent to or correspond 1:1 with260the top-level <em>import package</em> names261that can be imported inside Python code.262One <em>distribution package</em> can contain multiple <em>import packages</em>263(and single modules),264and one top-level <em>import package</em>265may map to multiple <em>distribution packages</em>266if it is a namespace package.267You can use <a class="reference internal" href="#package-distributions"><span class="std std-ref">packages_distributions()</span></a>268to get a mapping between them.</p>269</div>270<p>By default, distribution metadata can live on the file system271or in zip archives on272<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>. Through an extension mechanism, the metadata can live almost273anywhere.</p>274<div class="admonition seealso">275<p class="admonition-title">See also</p>276<dl class="simple">277<dt><a class="reference external" href="https://importlib-metadata.readthedocs.io/">https://importlib-metadata.readthedocs.io/</a></dt><dd><p>The documentation for <code class="docutils literal notranslate"><span class="pre">importlib_metadata</span></code>, which supplies a278backport of <code class="docutils literal notranslate"><span class="pre">importlib.metadata</span></code>.279This includes an <a class="reference external" href="https://importlib-metadata.readthedocs.io/en/latest/api.html">API reference</a>280for this module’s classes and functions,281as well as a <a class="reference external" href="https://importlib-metadata.readthedocs.io/en/latest/migration.html">migration guide</a>282for existing users of <code class="docutils literal notranslate"><span class="pre">pkg_resources</span></code>.</p>283</dd>284</dl>285</div>286<section id="overview">287<h2>Overview<a class="headerlink" href="#overview" title="Link to this heading">¶</a></h2>288<p>Let’s say you wanted to get the version string for a289<a class="reference external" href="https://packaging.python.org/en/latest/glossary/#term-Distribution-Package">Distribution Package</a> you’ve installed290using <code class="docutils literal notranslate"><span class="pre">pip</span></code>. We start by creating a virtual environment and installing291something into it:</p>292<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>venv<span class="w"> </span>example293<span class="gp">$ </span><span class="nb">source</span><span class="w"> </span>example/bin/activate294<span class="gp gp-VirtualEnv">(example)</span> <span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>wheel295</pre></div>296</div>297<p>You can get the version string for <code class="docutils literal notranslate"><span class="pre">wheel</span></code> by running the following:</p>298<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="go">(example) $ python</span>299<span class="gp">>>> </span><span class="kn">from</span><span class="w"> </span><span class="nn">importlib.metadata</span><span class="w"> </span><span class="kn">import</span> <span class="n">version</span>300<span class="gp">>>> </span><span class="n">version</span><span class="p">(</span><span class="s1">'wheel'</span><span class="p">)</span>301<span class="go">'0.32.3'</span>302</pre></div>303</div>304<p>You can also get a collection of entry points selectable by properties of the EntryPoint (typically ‘group’ or ‘name’), such as305<code class="docutils literal notranslate"><span class="pre">console_scripts</span></code>, <code class="docutils literal notranslate"><span class="pre">distutils.commands</span></code> and others. Each group contains a306collection of <a class="reference internal" href="#entry-points"><span class="std std-ref">EntryPoint</span></a> objects.</p>307<p>You can get the <a class="reference internal" href="#metadata"><span class="std std-ref">metadata for a distribution</span></a>:</p>308<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">metadata</span><span class="p">(</span><span class="s1">'wheel'</span><span class="p">))</span>309<span class="go">['Metadata-Version', 'Name', 'Version', 'Summary', 'Home-page', 'Author', 'Author-email', 'Maintainer', 'Maintainer-email', 'License', 'Project-URL', 'Project-URL', 'Project-URL', 'Keywords', 'Platform', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Requires-Python', 'Provides-Extra', 'Requires-Dist', 'Requires-Dist']</span>310</pre></div>311</div>312<p>You can also get a <a class="reference internal" href="#version"><span class="std std-ref">distribution’s version number</span></a>, list its313<a class="reference internal" href="#files"><span class="std std-ref">constituent files</span></a>, and get a list of the distribution’s314<a class="reference internal" href="#requirements"><span class="std std-ref">Distribution requirements</span></a>.</p>315<dl class="py exception">316<dt class="sig sig-object py" id="importlib.metadata.PackageNotFoundError">317<em class="property"><span class="k"><span class="pre">exception</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">PackageNotFoundError</span></span><a class="headerlink" href="#importlib.metadata.PackageNotFoundError" title="Link to this definition">¶</a></dt>318<dd><p>Subclass of <a class="reference internal" href="exceptions.html#ModuleNotFoundError" title="ModuleNotFoundError"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModuleNotFoundError</span></code></a> raised by several functions in this319module when queried for a distribution package which is not installed in the320current Python environment.</p>321</dd></dl>322 323</section>324<section id="functional-api">325<h2>Functional API<a class="headerlink" href="#functional-api" title="Link to this heading">¶</a></h2>326<p>This package provides the following functionality via its public API.</p>327<section id="entry-points">328<span id="id2"></span><h3>Entry points<a class="headerlink" href="#entry-points" title="Link to this heading">¶</a></h3>329<dl class="py function">330<dt class="sig sig-object py" id="importlib.metadata.entry_points">331<span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">entry_points</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">select_params</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.metadata.entry_points" title="Link to this definition">¶</a></dt>332<dd><p>Returns a <a class="reference internal" href="#importlib.metadata.EntryPoints" title="importlib.metadata.EntryPoints"><code class="xref py py-class docutils literal notranslate"><span class="pre">EntryPoints</span></code></a> instance describing entry points for the333current environment. Any given keyword parameters are passed to the334<code class="xref py py-meth docutils literal notranslate"><span class="pre">select()</span></code> method for comparison to the attributes of335the individual entry point definitions.</p>336<p>Note: to query for entry points based on <code class="xref py py-attr docutils literal notranslate"><span class="pre">EntryPoint.dist</span></code> attribute,337use <a class="reference internal" href="#importlib.metadata.Distribution.entry_points" title="importlib.metadata.Distribution.entry_points"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Distribution.entry_points()</span></code></a> instead (as different <a class="reference internal" href="#importlib.metadata.Distribution" title="importlib.metadata.Distribution"><code class="xref py py-class docutils literal notranslate"><span class="pre">Distribution</span></code></a>338instances do not currently compare equal, even if they have the same attributes)</p>339</dd></dl>340 341<dl class="py class">342<dt class="sig sig-object py" id="importlib.metadata.EntryPoints">343<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">EntryPoints</span></span><a class="headerlink" href="#importlib.metadata.EntryPoints" title="Link to this definition">¶</a></dt>344<dd><p>Details of a collection of installed entry points.</p>345<p>Also provides a <code class="docutils literal notranslate"><span class="pre">.groups</span></code> attribute that reports all identified entry346point groups, and a <code class="docutils literal notranslate"><span class="pre">.names</span></code> attribute that reports all identified entry347point names.</p>348</dd></dl>349 350<dl class="py class">351<dt class="sig sig-object py" id="importlib.metadata.EntryPoint">352<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">EntryPoint</span></span><a class="headerlink" href="#importlib.metadata.EntryPoint" title="Link to this definition">¶</a></dt>353<dd><p>Details of an installed entry point.</p>354<p>Each <code class="xref py py-class docutils literal notranslate"><span class="pre">EntryPoint</span></code> instance has <code class="docutils literal notranslate"><span class="pre">.name</span></code>, <code class="docutils literal notranslate"><span class="pre">.group</span></code>, and <code class="docutils literal notranslate"><span class="pre">.value</span></code>355attributes and a <code class="docutils literal notranslate"><span class="pre">.load()</span></code> method to resolve the value. There are also356<code class="docutils literal notranslate"><span class="pre">.module</span></code>, <code class="docutils literal notranslate"><span class="pre">.attr</span></code>, and <code class="docutils literal notranslate"><span class="pre">.extras</span></code> attributes for getting the357components of the <code class="docutils literal notranslate"><span class="pre">.value</span></code> attribute, and <code class="docutils literal notranslate"><span class="pre">.dist</span></code> for obtaining358information regarding the distribution package that provides the entry point.</p>359</dd></dl>360 361<p>Query all entry points:</p>362<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">eps</span> <span class="o">=</span> <span class="n">entry_points</span><span class="p">()</span>363</pre></div>364</div>365<p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">entry_points()</span></code> function returns a <code class="xref py py-class docutils literal notranslate"><span class="pre">EntryPoints</span></code> object,366a collection of all <code class="xref py py-class docutils literal notranslate"><span class="pre">EntryPoint</span></code> objects with <code class="docutils literal notranslate"><span class="pre">names</span></code> and <code class="docutils literal notranslate"><span class="pre">groups</span></code>367attributes for convenience:</p>368<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">sorted</span><span class="p">(</span><span class="n">eps</span><span class="o">.</span><span class="n">groups</span><span class="p">)</span>369<span class="go">['console_scripts', 'distutils.commands', 'distutils.setup_keywords', 'egg_info.writers', 'setuptools.installation']</span>370</pre></div>371</div>372<p><code class="xref py py-class docutils literal notranslate"><span class="pre">EntryPoints</span></code> has a <code class="xref py py-meth docutils literal notranslate"><span class="pre">select()</span></code> method to select entry points373matching specific properties. Select entry points in the374<code class="docutils literal notranslate"><span class="pre">console_scripts</span></code> group:</p>375<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">scripts</span> <span class="o">=</span> <span class="n">eps</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">group</span><span class="o">=</span><span class="s1">'console_scripts'</span><span class="p">)</span>376</pre></div>377</div>378<p>Equivalently, since <code class="xref py py-func docutils literal notranslate"><span class="pre">entry_points()</span></code> passes keyword arguments379through to select:</p>380<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">scripts</span> <span class="o">=</span> <span class="n">entry_points</span><span class="p">(</span><span class="n">group</span><span class="o">=</span><span class="s1">'console_scripts'</span><span class="p">)</span>381</pre></div>382</div>383<p>Pick out a specific script named “wheel” (found in the wheel project):</p>384<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'wheel'</span> <span class="ow">in</span> <span class="n">scripts</span><span class="o">.</span><span class="n">names</span>385<span class="go">True</span>386<span class="gp">>>> </span><span class="n">wheel</span> <span class="o">=</span> <span class="n">scripts</span><span class="p">[</span><span class="s1">'wheel'</span><span class="p">]</span>387</pre></div>388</div>389<p>Equivalently, query for that entry point during selection:</p>390<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">(</span><span class="n">wheel</span><span class="p">,)</span> <span class="o">=</span> <span class="n">entry_points</span><span class="p">(</span><span class="n">group</span><span class="o">=</span><span class="s1">'console_scripts'</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'wheel'</span><span class="p">)</span>391<span class="gp">>>> </span><span class="p">(</span><span class="n">wheel</span><span class="p">,)</span> <span class="o">=</span> <span class="n">entry_points</span><span class="p">()</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">group</span><span class="o">=</span><span class="s1">'console_scripts'</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'wheel'</span><span class="p">)</span>392</pre></div>393</div>394<p>Inspect the resolved entry point:</p>395<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">wheel</span>396<span class="go">EntryPoint(name='wheel', value='wheel.cli:main', group='console_scripts')</span>397<span class="gp">>>> </span><span class="n">wheel</span><span class="o">.</span><span class="n">module</span>398<span class="go">'wheel.cli'</span>399<span class="gp">>>> </span><span class="n">wheel</span><span class="o">.</span><span class="n">attr</span>400<span class="go">'main'</span>401<span class="gp">>>> </span><span class="n">wheel</span><span class="o">.</span><span class="n">extras</span>402<span class="go">[]</span>403<span class="gp">>>> </span><span class="n">main</span> <span class="o">=</span> <span class="n">wheel</span><span class="o">.</span><span class="n">load</span><span class="p">()</span>404<span class="gp">>>> </span><span class="n">main</span>405<span class="go"><function main at 0x103528488></span>406</pre></div>407</div>408<p>The <code class="docutils literal notranslate"><span class="pre">group</span></code> and <code class="docutils literal notranslate"><span class="pre">name</span></code> are arbitrary values defined by the package author409and usually a client will wish to resolve all entry points for a particular410group. Read <a class="reference external" href="https://setuptools.pypa.io/en/latest/userguide/entry_point.html">the setuptools docs</a>411for more information on entry points, their definition, and usage.</p>412<div class="versionchanged">413<p><span class="versionmodified changed">Changed in version 3.12: </span>The “selectable” entry points were introduced in <code class="docutils literal notranslate"><span class="pre">importlib_metadata</span></code>4143.6 and Python 3.10. Prior to those changes, <code class="docutils literal notranslate"><span class="pre">entry_points</span></code> accepted415no parameters and always returned a dictionary of entry points, keyed416by group. With <code class="docutils literal notranslate"><span class="pre">importlib_metadata</span></code> 5.0 and Python 3.12,417<code class="docutils literal notranslate"><span class="pre">entry_points</span></code> always returns an <code class="docutils literal notranslate"><span class="pre">EntryPoints</span></code> object. See418<a class="extlink-pypi reference external" href="https://pypi.org/project/backports.entry_points_selectable/">backports.entry_points_selectable</a>419for compatibility options.</p>420</div>421<div class="versionchanged">422<p><span class="versionmodified changed">Changed in version 3.13: </span><code class="docutils literal notranslate"><span class="pre">EntryPoint</span></code> objects no longer present a tuple-like interface423(<a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a>).</p>424</div>425</section>426<section id="distribution-metadata">427<span id="metadata"></span><h3>Distribution metadata<a class="headerlink" href="#distribution-metadata" title="Link to this heading">¶</a></h3>428<dl class="py function">429<dt class="sig sig-object py" id="importlib.metadata.metadata">430<span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">metadata</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">distribution_name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.metadata.metadata" title="Link to this definition">¶</a></dt>431<dd><p>Return the distribution metadata corresponding to the named432distribution package as a <a class="reference internal" href="#importlib.metadata.PackageMetadata" title="importlib.metadata.PackageMetadata"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageMetadata</span></code></a> instance.</p>433<p>Raises <a class="reference internal" href="#importlib.metadata.PackageNotFoundError" title="importlib.metadata.PackageNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PackageNotFoundError</span></code></a> if the named distribution434package is not installed in the current Python environment.</p>435</dd></dl>436 437<dl class="py class">438<dt class="sig sig-object py" id="importlib.metadata.PackageMetadata">439<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">PackageMetadata</span></span><a class="headerlink" href="#importlib.metadata.PackageMetadata" title="Link to this definition">¶</a></dt>440<dd><p>A concrete implementation of the441<a class="reference external" href="https://importlib-metadata.readthedocs.io/en/latest/api.html#importlib_metadata.PackageMetadata">PackageMetadata protocol</a>.</p>442<p>In addition to providing the defined protocol methods and attributes, subscripting443the instance is equivalent to calling the <code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code> method.</p>444</dd></dl>445 446<p>Every <a class="reference external" href="https://packaging.python.org/en/latest/glossary/#term-Distribution-Package">Distribution Package</a>447includes some metadata, which you can extract using the <code class="xref py py-func docutils literal notranslate"><span class="pre">metadata()</span></code> function:</p>448<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">wheel_metadata</span> <span class="o">=</span> <span class="n">metadata</span><span class="p">(</span><span class="s1">'wheel'</span><span class="p">)</span>449</pre></div>450</div>451<p>The keys of the returned data structure name the metadata keywords, and452the values are returned unparsed from the distribution metadata:</p>453<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">wheel_metadata</span><span class="p">[</span><span class="s1">'Requires-Python'</span><span class="p">]</span>454<span class="go">'>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'</span>455</pre></div>456</div>457<p><a class="reference internal" href="#importlib.metadata.PackageMetadata" title="importlib.metadata.PackageMetadata"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageMetadata</span></code></a> also presents a <code class="xref py py-attr docutils literal notranslate"><span class="pre">json</span></code> attribute that returns458all the metadata in a JSON-compatible form per <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0566/"><strong>PEP 566</strong></a>:</p>459<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">wheel_metadata</span><span class="o">.</span><span class="n">json</span><span class="p">[</span><span class="s1">'requires_python'</span><span class="p">]</span>460<span class="go">'>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'</span>461</pre></div>462</div>463<p>The full set of available metadata is not described here.464See the PyPA <a class="reference external" href="https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata">Core metadata specification</a> for additional details.</p>465<div class="versionchanged">466<p><span class="versionmodified changed">Changed in version 3.10: </span>The <code class="docutils literal notranslate"><span class="pre">Description</span></code> is now included in the metadata when presented467through the payload. Line continuation characters have been removed.</p>468<p>The <code class="docutils literal notranslate"><span class="pre">json</span></code> attribute was added.</p>469</div>470</section>471<section id="distribution-versions">472<span id="version"></span><h3>Distribution versions<a class="headerlink" href="#distribution-versions" title="Link to this heading">¶</a></h3>473<dl class="py function">474<dt class="sig sig-object py" id="importlib.metadata.version">475<span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">version</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">distribution_name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.metadata.version" title="Link to this definition">¶</a></dt>476<dd><p>Return the installed distribution package477<a class="reference external" href="https://packaging.python.org/en/latest/specifications/core-metadata/#version">version</a>478for the named distribution package.</p>479<p>Raises <a class="reference internal" href="#importlib.metadata.PackageNotFoundError" title="importlib.metadata.PackageNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PackageNotFoundError</span></code></a> if the named distribution480package is not installed in the current Python environment.</p>481</dd></dl>482 483<p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">version()</span></code> function is the quickest way to get a484<a class="reference external" href="https://packaging.python.org/en/latest/glossary/#term-Distribution-Package">Distribution Package</a>’s version485number, as a string:</p>486<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">version</span><span class="p">(</span><span class="s1">'wheel'</span><span class="p">)</span>487<span class="go">'0.32.3'</span>488</pre></div>489</div>490</section>491<section id="distribution-files">492<span id="files"></span><h3>Distribution files<a class="headerlink" href="#distribution-files" title="Link to this heading">¶</a></h3>493<dl class="py function">494<dt class="sig sig-object py" id="importlib.metadata.files">495<span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">files</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">distribution_name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.metadata.files" title="Link to this definition">¶</a></dt>496<dd><p>Return the full set of files contained within the named497distribution package as <a class="reference internal" href="#importlib.metadata.PackagePath" title="importlib.metadata.PackagePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackagePath</span></code></a> instances.</p>498<p>Raises <a class="reference internal" href="#importlib.metadata.PackageNotFoundError" title="importlib.metadata.PackageNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PackageNotFoundError</span></code></a> if the named distribution499package is not installed in the current Python environment.</p>500<p>Returns <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> if the distribution is found but the installation501database records reporting the files associated with the distribution package502are missing.</p>503</dd></dl>504 505<dl class="py class">506<dt class="sig sig-object py" id="importlib.metadata.PackagePath">507<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">PackagePath</span></span><a class="headerlink" href="#importlib.metadata.PackagePath" title="Link to this definition">¶</a></dt>508<dd><p>A <a class="reference internal" href="pathlib.html#pathlib.PurePath" title="pathlib.PurePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">pathlib.PurePath</span></code></a> derived object with additional <code class="docutils literal notranslate"><span class="pre">dist</span></code>,509<code class="docutils literal notranslate"><span class="pre">size</span></code>, and <code class="docutils literal notranslate"><span class="pre">hash</span></code> properties corresponding to the distribution510package’s installation metadata for that file, also:</p>511<dl class="py method">512<dt class="sig sig-object py" id="importlib.metadata.PackagePath.locate">513<span class="sig-name descname"><span class="pre">locate</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.metadata.PackagePath.locate" title="Link to this definition">¶</a></dt>514<dd><p>If possible, return the concrete <a class="reference internal" href="#importlib.metadata.SimplePath" title="importlib.metadata.SimplePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimplePath</span></code></a> allowing to access data,515or raise a <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> otherwise.</p>516</dd></dl>517 518</dd></dl>519 520<dl class="py class">521<dt class="sig sig-object py" id="importlib.metadata.SimplePath">522<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">SimplePath</span></span><a class="headerlink" href="#importlib.metadata.SimplePath" title="Link to this definition">¶</a></dt>523<dd><p>A protocol representing a minimal subset of <a class="reference internal" href="pathlib.html#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">pathlib.Path</span></code></a> that allows to524check if it <code class="docutils literal notranslate"><span class="pre">exists()</span></code>, to traverse using <code class="docutils literal notranslate"><span class="pre">joinpath()</span></code> and <code class="docutils literal notranslate"><span class="pre">parent</span></code>,525and to retrieve data using <code class="docutils literal notranslate"><span class="pre">read_text()</span></code> and <code class="docutils literal notranslate"><span class="pre">read_bytes()</span></code>.</p>526</dd></dl>527 528<p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">files()</span></code> function takes a529<a class="reference external" href="https://packaging.python.org/en/latest/glossary/#term-Distribution-Package">Distribution Package</a>530name and returns all of the files installed by this distribution. For example:</p>531<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">util</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">files</span><span class="p">(</span><span class="s1">'wheel'</span><span class="p">)</span> <span class="k">if</span> <span class="s1">'util.py'</span> <span class="ow">in</span> <span class="nb">str</span><span class="p">(</span><span class="n">p</span><span class="p">)][</span><span class="mi">0</span><span class="p">]</span>532<span class="gp">>>> </span><span class="n">util</span>533<span class="go">PackagePath('wheel/util.py')</span>534<span class="gp">>>> </span><span class="n">util</span><span class="o">.</span><span class="n">size</span>535<span class="go">859</span>536<span class="gp">>>> </span><span class="n">util</span><span class="o">.</span><span class="n">dist</span>537<span class="go"><importlib.metadata._hooks.PathDistribution object at 0x101e0cef0></span>538<span class="gp">>>> </span><span class="n">util</span><span class="o">.</span><span class="n">hash</span>539<span class="go"><FileHash mode: sha256 value: bYkw5oMccfazVCoYQwKkkemoVyMAFoR34mmKBx8R1NI></span>540</pre></div>541</div>542<p>Once you have the file, you can also read its contents:</p>543<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">util</span><span class="o">.</span><span class="n">read_text</span><span class="p">())</span>544<span class="go">import base64</span>545<span class="go">import sys</span>546<span class="go">...</span>547<span class="go">def as_bytes(s):</span>548<span class="go"> if isinstance(s, text_type):</span>549<span class="go"> return s.encode('utf-8')</span>550<span class="go"> return s</span>551</pre></div>552</div>553<p>You can also use the <code class="xref py py-meth docutils literal notranslate"><span class="pre">locate()</span></code> method to get the absolute554path to the file:</p>555<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">util</span><span class="o">.</span><span class="n">locate</span><span class="p">()</span>556<span class="go">PosixPath('/home/gustav/example/lib/site-packages/wheel/util.py')</span>557</pre></div>558</div>559<p>In the case where the metadata file listing files560(<code class="docutils literal notranslate"><span class="pre">RECORD</span></code> or <code class="docutils literal notranslate"><span class="pre">SOURCES.txt</span></code>) is missing, <code class="xref py py-func docutils literal notranslate"><span class="pre">files()</span></code> will561return <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>. The caller may wish to wrap calls to562<code class="xref py py-func docutils literal notranslate"><span class="pre">files()</span></code> in <a class="reference external" href="https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.always_iterable">always_iterable</a>563or otherwise guard against this condition if the target564distribution is not known to have the metadata present.</p>565</section>566<section id="distribution-requirements">567<span id="requirements"></span><h3>Distribution requirements<a class="headerlink" href="#distribution-requirements" title="Link to this heading">¶</a></h3>568<dl class="py function">569<dt class="sig sig-object py" id="importlib.metadata.requires">570<span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">requires</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">distribution_name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.metadata.requires" title="Link to this definition">¶</a></dt>571<dd><p>Return the declared dependency specifiers for the named572distribution package.</p>573<p>Raises <a class="reference internal" href="#importlib.metadata.PackageNotFoundError" title="importlib.metadata.PackageNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PackageNotFoundError</span></code></a> if the named distribution574package is not installed in the current Python environment.</p>575</dd></dl>576 577<p>To get the full set of requirements for a <a class="reference external" href="https://packaging.python.org/en/latest/glossary/#term-Distribution-Package">Distribution Package</a>,578use the <code class="xref py py-func docutils literal notranslate"><span class="pre">requires()</span></code>579function:</p>580<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">requires</span><span class="p">(</span><span class="s1">'wheel'</span><span class="p">)</span>581<span class="go">["pytest (>=3.0.0) ; extra == 'test'", "pytest-cov ; extra == 'test'"]</span>582</pre></div>583</div>584</section>585<section id="mapping-import-to-distribution-packages">586<span id="import-distribution-package-mapping"></span><span id="package-distributions"></span><h3>Mapping import to distribution packages<a class="headerlink" href="#mapping-import-to-distribution-packages" title="Link to this heading">¶</a></h3>587<dl class="py function">588<dt class="sig sig-object py" id="importlib.metadata.packages_distributions">589<span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">packages_distributions</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.metadata.packages_distributions" title="Link to this definition">¶</a></dt>590<dd><p>Return a mapping from the top level module and import package591names found via <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> to the names of the distribution592packages (if any) that provide the corresponding files.</p>593<p>To allow for namespace packages (which may have members provided by594multiple distribution packages), each top level import name maps to a595list of distribution names rather than mapping directly to a single name.</p>596</dd></dl>597 598<p>A convenience method to resolve the <a class="reference external" href="https://packaging.python.org/en/latest/glossary/#term-Distribution-Package">Distribution Package</a>599name (or names, in the case of a namespace package)600that provide each importable top-level601Python module or <a class="reference external" href="https://packaging.python.org/en/latest/glossary/#term-Import-Package">Import Package</a>:</p>602<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">packages_distributions</span><span class="p">()</span>603<span class="go">{'importlib_metadata': ['importlib-metadata'], 'yaml': ['PyYAML'], 'jaraco': ['jaraco.classes', 'jaraco.functools'], ...}</span>604</pre></div>605</div>606<p>Some editable installs, <a class="reference external" href="https://github.com/pypa/packaging-problems/issues/609">do not supply top-level names</a>, and thus this607function is not reliable with such installs.</p>608<div class="versionadded">609<p><span class="versionmodified added">Added in version 3.10.</span></p>610</div>611</section>612</section>613<section id="distributions">614<span id="id9"></span><h2>Distributions<a class="headerlink" href="#distributions" title="Link to this heading">¶</a></h2>615<p>While the module level API described above is the most common and convenient usage,616all that information is accessible from the <a class="reference internal" href="#importlib.metadata.Distribution" title="importlib.metadata.Distribution"><code class="xref py py-class docutils literal notranslate"><span class="pre">Distribution</span></code></a> class.617<code class="xref py py-class docutils literal notranslate"><span class="pre">Distribution</span></code> is an abstract object that represents the metadata for618a Python <a class="reference external" href="https://packaging.python.org/en/latest/glossary/#term-Distribution-Package">Distribution Package</a>.619Get the concrete <code class="xref py py-class docutils literal notranslate"><span class="pre">Distribution</span></code> subclass instance for an installed620distribution package by calling the <a class="reference internal" href="#importlib.metadata.distribution" title="importlib.metadata.distribution"><code class="xref py py-func docutils literal notranslate"><span class="pre">distribution()</span></code></a> function:</p>621<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span><span class="w"> </span><span class="nn">importlib.metadata</span><span class="w"> </span><span class="kn">import</span> <span class="n">distribution</span>622<span class="gp">>>> </span><span class="n">dist</span> <span class="o">=</span> <span class="n">distribution</span><span class="p">(</span><span class="s1">'wheel'</span><span class="p">)</span>623<span class="gp">>>> </span><span class="nb">type</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>624<span class="go"><class 'importlib.metadata.PathDistribution'></span>625</pre></div>626</div>627<dl class="py function">628<dt class="sig sig-object py" id="importlib.metadata.distribution">629<span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">distribution</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">distribution_name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.metadata.distribution" title="Link to this definition">¶</a></dt>630<dd><p>Return a <a class="reference internal" href="#importlib.metadata.Distribution" title="importlib.metadata.Distribution"><code class="xref py py-class docutils literal notranslate"><span class="pre">Distribution</span></code></a> instance describing the named631distribution package.</p>632<p>Raises <a class="reference internal" href="#importlib.metadata.PackageNotFoundError" title="importlib.metadata.PackageNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PackageNotFoundError</span></code></a> if the named distribution633package is not installed in the current Python environment.</p>634</dd></dl>635 636<p>Thus, an alternative way to get e.g. the version number is through the637<a class="reference internal" href="#importlib.metadata.Distribution.version" title="importlib.metadata.Distribution.version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Distribution.version</span></code></a> attribute:</p>638<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">dist</span><span class="o">.</span><span class="n">version</span>639<span class="go">'0.32.3'</span>640</pre></div>641</div>642<p>The same applies for <a class="reference internal" href="#importlib.metadata.entry_points" title="importlib.metadata.entry_points"><code class="xref py py-func docutils literal notranslate"><span class="pre">entry_points()</span></code></a> and <a class="reference internal" href="#importlib.metadata.files" title="importlib.metadata.files"><code class="xref py py-func docutils literal notranslate"><span class="pre">files()</span></code></a>.</p>643<dl class="py class">644<dt class="sig sig-object py" id="importlib.metadata.Distribution">645<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">Distribution</span></span><a class="headerlink" href="#importlib.metadata.Distribution" title="Link to this definition">¶</a></dt>646<dd><p>Details of an installed distribution package.</p>647<p>Note: different <code class="xref py py-class docutils literal notranslate"><span class="pre">Distribution</span></code> instances do not currently compare648equal, even if they relate to the same installed distribution and649accordingly have the same attributes.</p>650<dl class="py method">651<dt class="sig sig-object py" id="importlib.metadata.Distribution.at">652<em class="property"><span class="k"><span class="pre">static</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">at</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.metadata.Distribution.at" title="Link to this definition">¶</a></dt>653<dd></dd></dl>654 655<dl class="py method">656<dt class="sig sig-object py" id="importlib.metadata.Distribution.from_name">657<em class="property"><span class="k"><span class="pre">classmethod</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">from_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="#importlib.metadata.Distribution.from_name" title="Link to this definition">¶</a></dt>658<dd><p>Return a <code class="xref py py-class docutils literal notranslate"><span class="pre">Distribution</span></code> instance at the given path or659with the given name.</p>660</dd></dl>661 662<dl class="py method">663<dt class="sig sig-object py" id="importlib.metadata.Distribution.discover">664<em class="property"><span class="k"><span class="pre">classmethod</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">discover</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.metadata.Distribution.discover" title="Link to this definition">¶</a></dt>665<dd><p>Returns an iterable of <code class="xref py py-class docutils literal notranslate"><span class="pre">Distribution</span></code> instances for all packages666(see <a class="reference internal" href="#distribution-discovery">distribution-discovery</a>).</p>667<p>The optional argument <em>context</em> is a <a class="reference internal" href="#importlib.metadata.DistributionFinder.Context" title="importlib.metadata.DistributionFinder.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">DistributionFinder.Context</span></code></a>668instance, used to modify the search for distributions. Alternatively,669<em>kwargs</em> may contain keyword arguments for constructing a new670<code class="xref py py-class docutils literal notranslate"><span class="pre">DistributionFinder.Context</span></code>.</p>671</dd></dl>672 673<dl class="py attribute">674<dt class="sig sig-object py" id="importlib.metadata.Distribution.metadata">675<span class="sig-name descname"><span class="pre">metadata</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="#importlib.metadata.PackageMetadata" title="importlib.metadata.PackageMetadata"><span class="pre">PackageMetadata</span></a></em><a class="headerlink" href="#importlib.metadata.Distribution.metadata" title="Link to this definition">¶</a></dt>676<dd><p>There are all kinds of additional metadata available on <code class="xref py py-class docutils literal notranslate"><span class="pre">Distribution</span></code>677instances as a <a class="reference internal" href="#importlib.metadata.PackageMetadata" title="importlib.metadata.PackageMetadata"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageMetadata</span></code></a> instance:</p>678<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">dist</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s1">'Requires-Python'</span><span class="p">]</span>679<span class="go">'>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'</span>680<span class="gp">>>> </span><span class="n">dist</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s1">'License'</span><span class="p">]</span>681<span class="go">'MIT'</span>682</pre></div>683</div>684<p>The full set of available metadata is not described here.685See the PyPA <a class="reference external" href="https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata">Core metadata specification</a> for additional details.</p>686</dd></dl>687 688<dl class="py attribute">689<dt class="sig sig-object py" id="importlib.metadata.Distribution.name">690<span class="sig-name descname"><span class="pre">name</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="stdtypes.html#str" title="str"><span class="pre">str</span></a></em><a class="headerlink" href="#importlib.metadata.Distribution.name" title="Link to this definition">¶</a></dt>691<dd></dd></dl>692 693<dl class="py attribute">694<dt class="sig sig-object py" id="importlib.metadata.Distribution.requires">695<span class="sig-name descname"><span class="pre">requires</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="stdtypes.html#list" title="list"><span class="pre">list</span></a><span class="p"><span class="pre">[</span></span><a class="reference internal" href="stdtypes.html#str" title="str"><span class="pre">str</span></a><span class="p"><span class="pre">]</span></span></em><a class="headerlink" href="#importlib.metadata.Distribution.requires" title="Link to this definition">¶</a></dt>696<dd></dd></dl>697 698<dl class="py attribute">699<dt class="sig sig-object py" id="importlib.metadata.Distribution.version">700<span class="sig-name descname"><span class="pre">version</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="stdtypes.html#str" title="str"><span class="pre">str</span></a></em><a class="headerlink" href="#importlib.metadata.Distribution.version" title="Link to this definition">¶</a></dt>701<dd><p>A few metadata fields are also available as shortcut properties.</p>702<div class="versionadded">703<p><span class="versionmodified added">Added in version 3.10: </span>The <code class="docutils literal notranslate"><span class="pre">name</span></code> shortcut was added.</p>704</div>705</dd></dl>706 707<dl class="py attribute">708<dt class="sig sig-object py" id="importlib.metadata.Distribution.origin">709<span class="sig-name descname"><span class="pre">origin</span></span><a class="headerlink" href="#importlib.metadata.Distribution.origin" title="Link to this definition">¶</a></dt>710<dd><p>For editable packages, an <code class="docutils literal notranslate"><span class="pre">origin</span></code> property may present <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0610/"><strong>PEP 610</strong></a>711metadata (for non-editable packages, <code class="docutils literal notranslate"><span class="pre">origin</span></code> is <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>):</p>712<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">dist</span><span class="o">.</span><span class="n">origin</span><span class="o">.</span><span class="n">url</span>713<span class="go">'file:///path/to/wheel-0.32.3.editable-py3-none-any.whl'</span>714</pre></div>715</div>716<p>The <code class="docutils literal notranslate"><span class="pre">origin</span></code> object follows the <a class="reference external" href="https://packaging.python.org/en/latest/specifications/direct-url-data-structure/">Direct URL Data Structure</a>.</p>717<div class="versionadded">718<p><span class="versionmodified added">Added in version 3.13.</span></p>719</div>720</dd></dl>721 722<dl class="py attribute">723<dt class="sig sig-object py" id="importlib.metadata.Distribution.entry_points">724<span class="sig-name descname"><span class="pre">entry_points</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="#importlib.metadata.EntryPoints" title="importlib.metadata.EntryPoints"><span class="pre">EntryPoints</span></a></em><a class="headerlink" href="#importlib.metadata.Distribution.entry_points" title="Link to this definition">¶</a></dt>725<dd><p>The entry points provided by this distribution package.</p>726</dd></dl>727 728<dl class="py attribute">729<dt class="sig sig-object py" id="importlib.metadata.Distribution.files">730<span class="sig-name descname"><span class="pre">files</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="stdtypes.html#list" title="list"><span class="pre">list</span></a><span class="p"><span class="pre">[</span></span><a class="reference internal" href="#importlib.metadata.PackagePath" title="importlib.metadata.PackagePath"><span class="pre">PackagePath</span></a><span class="p"><span class="pre">]</span></span><span class="w"> </span><span class="p"><span class="pre">|</span></span><span class="w"> </span><a class="reference internal" href="constants.html#None" title="None"><span class="pre">None</span></a></em><a class="headerlink" href="#importlib.metadata.Distribution.files" title="Link to this definition">¶</a></dt>731<dd><p>All files contained in this distribution package.732Like <a class="reference internal" href="#importlib.metadata.files" title="importlib.metadata.files"><code class="xref py py-func docutils literal notranslate"><span class="pre">files()</span></code></a>, this returns <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> if there are no records.</p>733</dd></dl>734 735<p>The following two abstract methods need to be implemented when <a class="reference internal" href="#implementing-custom-providers">implementing-custom-providers</a>:</p>736<dl class="py method">737<dt class="sig sig-object py" id="importlib.metadata.Distribution.locate_file">738<span class="sig-name descname"><span class="pre">locate_file</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.metadata.Distribution.locate_file" title="Link to this definition">¶</a></dt>739<dd><p>Like <code class="xref py py-meth docutils literal notranslate"><span class="pre">PackagePath.locate()</span></code>, return a <a class="reference internal" href="#importlib.metadata.SimplePath" title="importlib.metadata.SimplePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimplePath</span></code></a> for the given path.740Takes a <a class="reference internal" href="os.html#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">os.PathLike</span></code></a> or a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>.</p>741</dd></dl>742 743<dl class="py method">744<dt class="sig sig-object py" id="importlib.metadata.Distribution.read_text">745<span class="sig-name descname"><span class="pre">read_text</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.metadata.Distribution.read_text" title="Link to this definition">¶</a></dt>746<dd><p>A shortcut for <code class="docutils literal notranslate"><span class="pre">distribution.locate_file(filename).read_text()</span></code>.</p>747</dd></dl>748 749</dd></dl>750 751</section>752<section id="distribution-discovery">753<span id="id12"></span><h2>Distribution Discovery<a class="headerlink" href="#distribution-discovery" title="Link to this heading">¶</a></h2>754<p>By default, this package provides built-in support for discovery of metadata755for file system and zip file <a class="reference external" href="https://packaging.python.org/en/latest/glossary/#term-Distribution-Package">Distribution Package</a>s.756This metadata finder search defaults to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>, but varies slightly in how it interprets those values from how other import machinery does. In particular:</p>757<ul class="simple">758<li><p><code class="docutils literal notranslate"><span class="pre">importlib.metadata</span></code> does not honor <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> objects on <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>.</p></li>759<li><p><code class="docutils literal notranslate"><span class="pre">importlib.metadata</span></code> will incidentally honor <a class="reference internal" href="pathlib.html#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">pathlib.Path</span></code></a> objects on <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> even though such values will be ignored for imports.</p></li>760</ul>761<dl class="py class">762<dt class="sig sig-object py" id="importlib.metadata.DistributionFinder">763<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">DistributionFinder</span></span><a class="headerlink" href="#importlib.metadata.DistributionFinder" title="Link to this definition">¶</a></dt>764<dd><p>A <a class="reference internal" href="importlib.html#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">MetaPathFinder</span></code></a> subclass capable of discovering765installed distributions.</p>766<p>Custom providers should implement this interface in order to767supply metadata.</p>768<blockquote>769<div><dl class="py class">770<dt class="sig sig-object py" id="importlib.metadata.DistributionFinder.Context">771<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">Context</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.metadata.DistributionFinder.Context" title="Link to this definition">¶</a></dt>772<dd><p>A <code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code> gives a custom provider a means to773solicit additional details from the callers of distribution discovery774functions like <a class="reference internal" href="#importlib.metadata.distributions" title="importlib.metadata.distributions"><code class="xref py py-func docutils literal notranslate"><span class="pre">distributions()</span></code></a> or <a class="reference internal" href="#importlib.metadata.Distribution.discover" title="importlib.metadata.Distribution.discover"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Distribution.discover()</span></code></a>775beyond <code class="xref py py-attr docutils literal notranslate"><span class="pre">.name</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">.path</span></code> when searching776for distributions.</p>777<p>For example, a provider could expose suites of packages in either a778“public” or “private” <code class="docutils literal notranslate"><span class="pre">realm</span></code>. A caller of distribution discovery779functions may wish to query only for distributions in a particular realm780and could call <code class="docutils literal notranslate"><span class="pre">distributions(realm="private")</span></code> to signal to the781custom provider to only include distributions from that782realm.</p>783<p>Each <code class="xref py py-class docutils literal notranslate"><span class="pre">DistributionFinder</span></code> must expect any parameters and should784attempt to honor the canonical parameters defined below when785appropriate.</p>786<p>See the section on <a class="reference internal" href="#implementing-custom-providers"><span class="std std-ref">Implementing Custom Providers</span></a> for more details.</p>787<dl class="py attribute">788<dt class="sig sig-object py" id="importlib.metadata.DistributionFinder.Context.name">789<span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#importlib.metadata.DistributionFinder.Context.name" title="Link to this definition">¶</a></dt>790<dd><p>Specific name for which a distribution finder should match.</p>791<p>A <code class="xref py py-attr docutils literal notranslate"><span class="pre">.name</span></code> of <code class="docutils literal notranslate"><span class="pre">None</span></code> matches all distributions.</p>792</dd></dl>793 794<dl class="py attribute">795<dt class="sig sig-object py" id="importlib.metadata.DistributionFinder.Context.path">796<span class="sig-name descname"><span class="pre">path</span></span><a class="headerlink" href="#importlib.metadata.DistributionFinder.Context.path" title="Link to this definition">¶</a></dt>797<dd><p>A property providing the sequence of directory paths that a798distribution finder should search.</p>799<p>Typically refers to Python installed package paths such as800“site-packages” directories and defaults to <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path</span></code></a>.</p>801</dd></dl>802 803</dd></dl>804 805</div></blockquote>806</dd></dl>807 808<dl class="py function">809<dt class="sig sig-object py" id="importlib.metadata.distributions">810<span class="sig-prename descclassname"><span class="pre">importlib.metadata.</span></span><span class="sig-name descname"><span class="pre">distributions</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.metadata.distributions" title="Link to this definition">¶</a></dt>811<dd><p>Returns an iterable of <a class="reference internal" href="#importlib.metadata.Distribution" title="importlib.metadata.Distribution"><code class="xref py py-class docutils literal notranslate"><span class="pre">Distribution</span></code></a> instances for all packages.</p>812<p>The <em>kwargs</em> argument may contain either a keyword argument <code class="docutils literal notranslate"><span class="pre">context</span></code>, a813<a class="reference internal" href="#importlib.metadata.DistributionFinder.Context" title="importlib.metadata.DistributionFinder.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">DistributionFinder.Context</span></code></a> instance, or pass keyword arguments for814constructing a new <code class="xref py py-class docutils literal notranslate"><span class="pre">DistributionFinder.Context</span></code>. The815<code class="xref py py-class docutils literal notranslate"><span class="pre">DistributionFinder.Context</span></code> is used to modify the search for816distributions.</p>817</dd></dl>818 819</section>820<section id="implementing-custom-providers">821<span id="id14"></span><h2>Implementing Custom Providers<a class="headerlink" href="#implementing-custom-providers" title="Link to this heading">¶</a></h2>822<p><code class="docutils literal notranslate"><span class="pre">importlib.metadata</span></code> address two API surfaces, one for <em>consumers</em>823and another for <em>providers</em>. Most users are consumers, consuming824metadata provided by the packages. There are other use-cases, however,825where users wish to expose metadata through some other mechanism,826such as alongside a custom importer. Such a use case calls for a827<em>custom provider</em>.</p>828<p>Because <a class="reference external" href="https://packaging.python.org/en/latest/glossary/#term-Distribution-Package">Distribution Package</a> metadata829is not available through <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> searches, or830package loaders directly,831the metadata for a distribution is found through import832system <a class="reference internal" href="../reference/import.html#finders-and-loaders"><span class="std std-ref">finders</span></a>. To find a distribution package’s metadata,833<code class="docutils literal notranslate"><span class="pre">importlib.metadata</span></code> queries the list of <a class="reference internal" href="../glossary.html#term-meta-path-finder"><span class="xref std std-term">meta path finders</span></a> on834<a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a>.</p>835<p>The implementation has hooks integrated into the <code class="docutils literal notranslate"><span class="pre">PathFinder</span></code>,836serving metadata for distribution packages found on the file system.</p>837<p>The abstract class <a class="reference internal" href="importlib.html#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder</span></code></a> defines the838interface expected of finders by Python’s import system.839<code class="docutils literal notranslate"><span class="pre">importlib.metadata</span></code> extends this protocol by looking for an optional840<code class="docutils literal notranslate"><span class="pre">find_distributions</span></code> callable on the finders from841<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 presents this extended interface as the842<a class="reference internal" href="#importlib.metadata.DistributionFinder" title="importlib.metadata.DistributionFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">DistributionFinder</span></code></a> abstract base class, which defines this abstract843method:</p>844<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>845<span class="k">def</span><span class="w"> </span><span class="nf">find_distributions</span><span class="p">(</span><span class="n">context</span><span class="o">=</span><span class="n">DistributionFinder</span><span class="o">.</span><span class="n">Context</span><span class="p">())</span> <span class="o">-></span> <span class="n">Iterable</span><span class="p">[</span><span class="n">Distribution</span><span class="p">]:</span>846<span class="w"> </span><span class="sd">"""Return an iterable of all Distribution instances capable of</span>847<span class="sd"> loading the metadata for packages for the indicated ``context``.</span>848<span class="sd"> """</span>849</pre></div>850</div>851<p>The <a class="reference internal" href="#importlib.metadata.DistributionFinder.Context" title="importlib.metadata.DistributionFinder.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">DistributionFinder.Context</span></code></a> object provides852<a class="reference internal" href="#importlib.metadata.DistributionFinder.Context.path" title="importlib.metadata.DistributionFinder.Context.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a> and853<a class="reference internal" href="#importlib.metadata.DistributionFinder.Context.name" title="importlib.metadata.DistributionFinder.Context.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a> properties indicating the path to854search and name to match and may supply other relevant context sought by the855consumer.</p>856<p>In practice, to support finding distribution package857metadata in locations other than the file system, subclass858<code class="xref py py-class docutils literal notranslate"><span class="pre">Distribution</span></code> and implement the abstract methods. Then from859a custom finder, return instances of this derived <code class="xref py py-class docutils literal notranslate"><span class="pre">Distribution</span></code> in the860<code class="docutils literal notranslate"><span class="pre">find_distributions()</span></code> method.</p>861<section id="example">862<h3>Example<a class="headerlink" href="#example" title="Link to this heading">¶</a></h3>863<p>Imagine a custom finder that loads Python modules from a database:</p>864<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">DatabaseImporter</span><span class="p">(</span><span class="n">importlib</span><span class="o">.</span><span class="n">abc</span><span class="o">.</span><span class="n">MetaPathFinder</span><span class="p">):</span>865 <span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db</span><span class="p">):</span>866 <span class="bp">self</span><span class="o">.</span><span class="n">db</span> <span class="o">=</span> <span class="n">db</span>867 868 <span class="k">def</span><span class="w"> </span><span class="nf">find_spec</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fullname</span><span class="p">,</span> <span class="n">target</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> <span class="o">-></span> <span class="n">ModuleSpec</span><span class="p">:</span>869 <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">spec_from_name</span><span class="p">(</span><span class="n">fullname</span><span class="p">)</span>870 871<span class="n">sys</span><span class="o">.</span><span class="n">meta_path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">DatabaseImporter</span><span class="p">(</span><span class="n">connect_db</span><span class="p">(</span><span class="o">...</span><span class="p">)))</span>872</pre></div>873</div>874<p>That importer now presumably provides importable modules from a875database, but it provides no metadata or entry points. For this876custom importer to provide metadata, it would also need to implement877<a class="reference internal" href="#importlib.metadata.DistributionFinder" title="importlib.metadata.DistributionFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">DistributionFinder</span></code></a>:</p>878<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">importlib.metadata</span><span class="w"> </span><span class="kn">import</span> <span class="n">DistributionFinder</span>879 880<span class="k">class</span><span class="w"> </span><span class="nc">DatabaseImporter</span><span class="p">(</span><span class="n">DistributionFinder</span><span class="p">):</span>881 <span class="o">...</span>882 883 <span class="k">def</span><span class="w"> </span><span class="nf">find_distributions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="n">DistributionFinder</span><span class="o">.</span><span class="n">Context</span><span class="p">()):</span>884 <span class="n">query</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">context</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="k">if</span> <span class="n">context</span><span class="o">.</span><span class="n">name</span> <span class="k">else</span> <span class="p">{}</span>885 <span class="k">for</span> <span class="n">dist_record</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">query_distributions</span><span class="p">(</span><span class="n">query</span><span class="p">):</span>886 <span class="k">yield</span> <span class="n">DatabaseDistribution</span><span class="p">(</span><span class="n">dist_record</span><span class="p">)</span>887</pre></div>888</div>889<p>In this way, <code class="docutils literal notranslate"><span class="pre">query_distributions</span></code> would return records for890each distribution served by the database matching the query. For891example, if <code class="docutils literal notranslate"><span class="pre">requests-1.0</span></code> is in the database, <code class="docutils literal notranslate"><span class="pre">find_distributions</span></code>892would yield a <code class="docutils literal notranslate"><span class="pre">DatabaseDistribution</span></code> for <code class="docutils literal notranslate"><span class="pre">Context(name='requests')</span></code>893or <code class="docutils literal notranslate"><span class="pre">Context(name=None)</span></code>.</p>894<p>For the sake of simplicity, this example ignores <code class="docutils literal notranslate"><span class="pre">context.path</span></code>. The895<code class="docutils literal notranslate"><span class="pre">path</span></code> attribute defaults to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> and is the set of import paths to896be considered in the search. A <code class="docutils literal notranslate"><span class="pre">DatabaseImporter</span></code> could potentially function897without any concern for a search path. Assuming the importer does no898partitioning, the “path” would be irrelevant. In order to illustrate the899purpose of <code class="docutils literal notranslate"><span class="pre">path</span></code>, the example would need to illustrate a more complex900<code class="docutils literal notranslate"><span class="pre">DatabaseImporter</span></code> whose behavior varied depending on901<code class="docutils literal notranslate"><span class="pre">sys.path</span></code>/<code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code>. In that case, the <code class="docutils literal notranslate"><span class="pre">find_distributions</span></code> should902honor the <code class="docutils literal notranslate"><span class="pre">context.path</span></code> and only yield <code class="docutils literal notranslate"><span class="pre">Distribution</span></code>s pertinent to that903path.</p>904<p><code class="docutils literal notranslate"><span class="pre">DatabaseDistribution</span></code>, then, would look something like:</p>905<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">DatabaseDistribution</span><span class="p">(</span><span class="n">importlib</span><span class="o">.</span><span class="n">metadata</span><span class="o">.</span><span class="n">Distribution</span><span class="p">):</span>906 <span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">record</span><span class="p">):</span>907 <span class="bp">self</span><span class="o">.</span><span class="n">record</span> <span class="o">=</span> <span class="n">record</span>908 909 <span class="k">def</span><span class="w"> </span><span class="nf">read_text</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>910<span class="w"> </span><span class="sd">"""</span>911<span class="sd"> Read a file like "METADATA" for the current distribution.</span>912<span class="sd"> """</span>913 <span class="k">if</span> <span class="n">filename</span> <span class="o">==</span> <span class="s2">"METADATA"</span><span class="p">:</span>914 <span class="k">return</span> <span class="sa">f</span><span class="s2">"""Name: </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">record</span><span class="o">.</span><span class="n">name</span><span class="si">}</span>915<span class="s2">Version: </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">record</span><span class="o">.</span><span class="n">version</span><span class="si">}</span>916<span class="s2">"""</span>917 <span class="k">if</span> <span class="n">filename</span> <span class="o">==</span> <span class="s2">"entry_points.txt"</span><span class="p">:</span>918 <span class="k">return</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="o">.</span><span class="n">join</span><span class="p">(</span>919 <span class="sa">f</span><span class="s2">"""[</span><span class="si">{</span><span class="n">ep</span><span class="o">.</span><span class="n">group</span><span class="si">}</span><span class="s2">]</span><span class="se">\n</span><span class="si">{</span><span class="n">ep</span><span class="o">.</span><span class="n">name</span><span class="si">}</span><span class="s2">=</span><span class="si">{</span><span class="n">ep</span><span class="o">.</span><span class="n">value</span><span class="si">}</span><span class="s2">"""</span>920 <span class="k">for</span> <span class="n">ep</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">record</span><span class="o">.</span><span class="n">entry_points</span><span class="p">)</span>921 922 <span class="k">def</span><span class="w"> </span><span class="nf">locate_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>923 <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">"This distribution has no file system"</span><span class="p">)</span>924</pre></div>925</div>926<p>This basic implementation should provide metadata and entry points for927packages served by the <code class="docutils literal notranslate"><span class="pre">DatabaseImporter</span></code>, assuming that the928<code class="docutils literal notranslate"><span class="pre">record</span></code> supplies suitable <code class="docutils literal notranslate"><span class="pre">.name</span></code>, <code class="docutils literal notranslate"><span class="pre">.version</span></code>, and929<code class="docutils literal notranslate"><span class="pre">.entry_points</span></code> attributes.</p>930<p>The <code class="docutils literal notranslate"><span class="pre">DatabaseDistribution</span></code> may also provide other metadata files, like931<code class="docutils literal notranslate"><span class="pre">RECORD</span></code> (required for <code class="xref py py-attr docutils literal notranslate"><span class="pre">Distribution.files</span></code>) or override the932implementation of <code class="xref py py-attr docutils literal notranslate"><span class="pre">Distribution.files</span></code>. See the source for more inspiration.</p>933</section>934</section>935</section>936 937 938 <div class="clearer"></div>939 </div>940 </div>941 </div>942 <div class="sphinxsidebar" role="navigation" aria-label="Main">943 <div class="sphinxsidebarwrapper">944 <div>945 <h3><a href="../contents.html">Table of Contents</a></h3>946 <ul>947<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.metadata</span></code> – Accessing package metadata</a><ul>948<li><a class="reference internal" href="#overview">Overview</a></li>949<li><a class="reference internal" href="#functional-api">Functional API</a><ul>950<li><a class="reference internal" href="#entry-points">Entry points</a></li>951<li><a class="reference internal" href="#distribution-metadata">Distribution metadata</a></li>952<li><a class="reference internal" href="#distribution-versions">Distribution versions</a></li>953<li><a class="reference internal" href="#distribution-files">Distribution files</a></li>954<li><a class="reference internal" href="#distribution-requirements">Distribution requirements</a></li>955<li><a class="reference internal" href="#mapping-import-to-distribution-packages">Mapping import to distribution packages</a></li>956</ul>957</li>958<li><a class="reference internal" href="#distributions">Distributions</a></li>959<li><a class="reference internal" href="#distribution-discovery">Distribution Discovery</a></li>960<li><a class="reference internal" href="#implementing-custom-providers">Implementing Custom Providers</a><ul>961<li><a class="reference internal" href="#example">Example</a></li>962</ul>963</li>964</ul>965</li>966</ul>967 968 </div>969 <div>970 <h4>Previous topic</h4>971 <p class="topless"><a href="importlib.resources.abc.html"972 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.resources.abc</span></code> – Abstract base classes for resources</a></p>973 </div>974 <div>975 <h4>Next topic</h4>976 <p class="topless"><a href="sys_path_init.html"977 title="next chapter">The initialization of the <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code> module search path</a></p>978 </div>979 <script>980 document.addEventListener('DOMContentLoaded', () => {981 const title = document.querySelector('meta[property="og:title"]').content;982 const elements = document.querySelectorAll('.improvepage');983 const pageurl = window.location.href.split('?')[0];984 elements.forEach(element => {985 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));986 url.searchParams.set('pagetitle', title);987 url.searchParams.set('pageurl', pageurl);988 url.searchParams.set('pagesource', "library/importlib.metadata.rst");989 element.href = url.toString();990 });991 });992 </script>993 <div role="note" aria-label="source link">994 <h3>This page</h3>995 <ul class="this-page-menu">996 <li><a href="../bugs.html">Report a bug</a></li>997 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>998 <li>999 <a href="https://github.com/python/cpython/blob/main/Doc/library/importlib.metadata.rst?plain=1"1000 rel="nofollow">Show source1001 </a>1002 </li>1003 1004 </ul>1005 </div>1006 </div>1007<div id="sidebarbutton" title="Collapse sidebar">1008<span>«</span>1009</div>1010 1011 </div>1012 <div class="clearer"></div>1013 </div> 1014 <div class="related" role="navigation" aria-label="Related">1015 <h3>Navigation</h3>1016 <ul>1017 <li class="right" style="margin-right: 10px">1018 <a href="../genindex.html" title="General Index"1019 >index</a></li>1020 <li class="right" >1021 <a href="../py-modindex.html" title="Python Module Index"1022 >modules</a> |</li>1023 <li class="right" >1024 <a href="sys_path_init.html" title="The initialization of the sys.path module search path"1025 >next</a> |</li>1026 <li class="right" >1027 <a href="importlib.resources.abc.html" title="importlib.resources.abc – Abstract base classes for resources"1028 >previous</a> |</li>1029 1030 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>1031 <li><a href="https://www.python.org/">Python</a> »</li>1032 <li class="switchers">1033 <div class="language_switcher_placeholder"></div>1034 <div class="version_switcher_placeholder"></div>1035 </li>1036 <li>1037 1038 </li>1039 <li id="cpython-language-and-version">1040 <a href="../index.html">3.15.0a6 Documentation</a> »1041 </li>1042 1043 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>1044 <li class="nav-item nav-item-2"><a href="modules.html" >Importing Modules</a> »</li>1045 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.metadata</span></code> – Accessing package metadata</a></li>1046 <li class="right">1047 1048 1049 <div class="inline-search" role="search">1050 <form class="inline-search" action="../search.html" method="get">1051 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">1052 <input type="submit" value="Go">1053 </form>1054 </div>1055 |1056 </li>1057 <li class="right">1058<label class="theme-selector-label">1059 Theme1060 <select class="theme-selector" oninput="activateTheme(this.value)">1061 <option value="auto" selected>Auto</option>1062 <option value="light">Light</option>1063 <option value="dark">Dark</option>1064 </select>1065</label> |</li>1066 1067 </ul>1068 </div> 1069 <div class="footer">1070 © <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.1071 <br>1072 This page is licensed under the Python Software Foundation License Version 2.1073 <br>1074 Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.1075 <br>1076 1077 See <a href="/license.html">History and License</a> for more information.<br>1078 1079 1080 <br>1081 1082 The Python Software Foundation is a non-profit corporation.1083<a href="https://www.python.org/psf/donations/">Please donate.</a>1084<br>1085 <br>1086 Last updated on Mar 10, 2026 (08:58 UTC).1087 1088 <a href="/bugs.html">Found a bug</a>?1089 1090 <br>1091 1092 Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.1093 </div>1094 1095 </body>1096</html>