458 lines
35 KiB
HTML
458 lines
35 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<title>使用 importlib.metadata — Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<script src="../_static/translations.js"></script>
|
||
|
||
<script src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="在 Python 3.8.20 文档 中搜索"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="关于这些文档" href="../about.html" />
|
||
<link rel="index" title="索引" href="../genindex.html" />
|
||
<link rel="search" title="搜索" href="../search.html" />
|
||
<link rel="copyright" title="版权所有" href="../copyright.html" />
|
||
<link rel="next" title="Python 语言服务" href="language.html" />
|
||
<link rel="prev" title="importlib --- import 的实现" href="importlib.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/importlib.metadata.html" />
|
||
|
||
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/menu.js"></script>
|
||
|
||
</head>
|
||
<body>
|
||
<div class="mobile-nav">
|
||
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
||
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
||
<label for="menuToggler" class="toggler__label">
|
||
<span></span>
|
||
</label>
|
||
<nav class="nav-content" role="navigation">
|
||
<a href="https://www.python.org/" class="nav-logo">
|
||
<img src="../_static/py.svg" alt="Logo"/>
|
||
</a>
|
||
<div class="version_switcher_placeholder"></div>
|
||
<form role="search" class="search" action="../search.html" method="get">
|
||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
||
<path fill-rule="nonzero"
|
||
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" fill="#444"></path>
|
||
</svg>
|
||
<input type="text" name="q" aria-label="快速搜索"/>
|
||
<input type="submit" value="转向"/>
|
||
</form>
|
||
</nav>
|
||
<div class="menu-wrapper">
|
||
<nav class="menu" role="navigation" aria-label="main navigation">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#">使用 <code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.metadata</span></code></a><ul>
|
||
<li><a class="reference internal" href="#overview">概述</a></li>
|
||
<li><a class="reference internal" href="#functional-api">函数式 API</a><ul>
|
||
<li><a class="reference internal" href="#entry-points">入口点</a></li>
|
||
<li><a class="reference internal" href="#distribution-metadata">分发的元数据</a></li>
|
||
<li><a class="reference internal" href="#distribution-versions">分发包的版本</a></li>
|
||
<li><a class="reference internal" href="#distribution-files">分发包的文件</a></li>
|
||
<li><a class="reference internal" href="#distribution-requirements">分发包的依赖</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#distributions">分发包对象</a></li>
|
||
<li><a class="reference internal" href="#extending-the-search-algorithm">扩展搜索算法</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="importlib.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code> --- <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code> 的实现</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="language.html"
|
||
title="下一章">Python 语言服务</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/importlib.metadata.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
||
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
||
你应当升级版本,并阅读
|
||
<a href="/3/library/importlib.metadata.html"> Python 当前稳定版本的文档</a>.
|
||
</div>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
accesskey="I">索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="language.html" title="Python 语言服务"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="importlib.html" title="importlib --- import 的实现"
|
||
accesskey="P">上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="modules.html" accesskey="U">导入模块</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<section id="module-importlib.metadata">
|
||
<span id="using-importlib-metadata"></span><span id="using"></span><h1>使用 <code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.metadata</span></code><a class="headerlink" href="#module-importlib.metadata" title="永久链接至标题">¶</a></h1>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.8 新版功能.</span></p>
|
||
</div>
|
||
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/importlib/metadata.py">Lib/importlib/metadata.py</a></p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>这个功能是暂定的,可能会偏离标准库通常的版本语义。</p>
|
||
</div>
|
||
<p><code class="docutils literal notranslate"><span class="pre">importlib.metadata</span></code> 是一个提供对已安装包的元数据访问的库。这个库部分建立在 Python 的导入系统上,旨在取代 <code class="docutils literal notranslate"><span class="pre">pkg_resources</span></code> 的 <a class="reference external" href="https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points">entry point API</a> 和 <a class="reference external" href="https://setuptools.readthedocs.io/en/latest/pkg_resources.html#metadata-api">metadata API</a> 中的类似功能。 通过和 Python 3.7 或更高版本中的 <a class="reference internal" href="importlib.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> 一同使用(对于旧版本的 Python 则作为 <a class="reference external" href="https://importlib-resources.readthedocs.io/en/latest/index.html">importlib_resources</a> 向后移植),这可以消除对使用较旧且较为低效的 <code class="docutils literal notranslate"><span class="pre">pkg_resources</span></code> 包的需要。</p>
|
||
<p>此处所说的 “已安装的包” 通常指通过 <a class="reference external" href="https://pypi.org/project/pip/">pip</a> 等工具安装在 Python <code class="docutils literal notranslate"><span class="pre">site-packages</span></code> 目录下的第三方包。具体来说,它指的是一个具有可发现的 <code class="docutils literal notranslate"><span class="pre">dist-info</span></code> 或 <code class="docutils literal notranslate"><span class="pre">egg-info</span></code> 目录以及 <span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0566"><strong>PEP 566</strong></a> 或其更早的规范所定义的元数据的包。默认情况下,包的元数据可以存在于文件系统中或 <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 上的压缩文件中。 通过扩展机制,元数据几乎可以存在于任何地方。</p>
|
||
<section id="overview">
|
||
<h2>概述<a class="headerlink" href="#overview" title="永久链接至标题">¶</a></h2>
|
||
<p>假设你想得到你用 <code class="docutils literal notranslate"><span class="pre">pip</span></code> 安装的一个包的版本字符串。我们在创建的虚拟环境中安装一些东西:</p>
|
||
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python3<span class="w"> </span>-m<span class="w"> </span>venv<span class="w"> </span>example
|
||
<span class="gp">$ </span><span class="nb">source</span><span class="w"> </span>example/bin/activate
|
||
<span class="gp gp-VirtualEnv">(example)</span> <span class="gp">$ </span>pip<span class="w"> </span>install<span class="w"> </span>wheel
|
||
</pre></div>
|
||
</div>
|
||
<p>你可以通过运行以下代码得到 <code class="docutils literal notranslate"><span class="pre">wheel</span></code> 的版本字符串:</p>
|
||
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="go">(example) $ python</span>
|
||
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">importlib.metadata</span> <span class="kn">import</span> <span class="n">version</span>
|
||
<span class="gp">>>> </span><span class="n">version</span><span class="p">(</span><span class="s1">'wheel'</span><span class="p">)</span>
|
||
<span class="go">'0.32.3'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>你也可以获得以组名为关键字的入口点集合,比如 <code class="docutils literal notranslate"><span class="pre">console_scripts</span></code> 和 <code class="docutils literal notranslate"><span class="pre">distutils.commands</span></code> 。每个组包含一个 <a class="reference internal" href="#entry-points"><span class="std std-ref">入口点</span></a> 对象的序列。</p>
|
||
<p>你可以获得 <a class="reference internal" href="#metadata"><span class="std std-ref">分发的元数据</span></a>:</p>
|
||
<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>
|
||
<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>
|
||
</pre></div>
|
||
</div>
|
||
<p>你也可以获得 <a class="reference internal" href="#version"><span class="std std-ref">分发包的版本号</span></a>,列出它的 <a class="reference internal" href="#files"><span class="std std-ref">构成文件</span></a>,并且得到分发包的 <a class="reference internal" href="#requirements"><span class="std std-ref">分发包的依赖</span></a> 列表。</p>
|
||
</section>
|
||
<section id="functional-api">
|
||
<h2>函数式 API<a class="headerlink" href="#functional-api" title="永久链接至标题">¶</a></h2>
|
||
<p>这个包的公开 API 提供了以下功能。</p>
|
||
<section id="entry-points">
|
||
<span id="id1"></span><h3>入口点<a class="headerlink" href="#entry-points" title="永久链接至标题">¶</a></h3>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">entry_points()</span></code> function returns a dictionary of all entry points,
|
||
keyed by group. Entry points are represented by <code class="docutils literal notranslate"><span class="pre">EntryPoint</span></code> instances;
|
||
each <code class="docutils literal notranslate"><span class="pre">EntryPoint</span></code> has a <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> attributes and
|
||
a <code class="docutils literal notranslate"><span class="pre">.load()</span></code> method to resolve the value.</p>
|
||
<div class="doctest highlight-default 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>
|
||
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">eps</span><span class="p">)</span>
|
||
<span class="go">['console_scripts', 'distutils.commands', 'distutils.setup_keywords', 'egg_info.writers', 'setuptools.installation']</span>
|
||
<span class="gp">>>> </span><span class="n">scripts</span> <span class="o">=</span> <span class="n">eps</span><span class="p">[</span><span class="s1">'console_scripts'</span><span class="p">]</span>
|
||
<span class="gp">>>> </span><span class="n">wheel</span> <span class="o">=</span> <span class="p">[</span><span class="n">ep</span> <span class="k">for</span> <span class="n">ep</span> <span class="ow">in</span> <span class="n">scripts</span> <span class="k">if</span> <span class="n">ep</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s1">'wheel'</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
|
||
<span class="gp">>>> </span><span class="n">wheel</span>
|
||
<span class="go">EntryPoint(name='wheel', value='wheel.cli:main', group='console_scripts')</span>
|
||
<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>
|
||
<span class="gp">>>> </span><span class="n">main</span>
|
||
<span class="go"><function main at 0x103528488></span>
|
||
</pre></div>
|
||
</div>
|
||
<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 author
|
||
and usually a client will wish to resolve all entry points for a particular
|
||
group. Read <a class="reference external" href="https://setuptools.readthedocs.io/en/latest/setuptools.html#dynamic-discovery-of-services-and-plugins">the setuptools docs</a>
|
||
for more information on entrypoints, their definition, and usage.</p>
|
||
</section>
|
||
<section id="distribution-metadata">
|
||
<span id="metadata"></span><h3>分发的元数据<a class="headerlink" href="#distribution-metadata" title="永久链接至标题">¶</a></h3>
|
||
<p>每个分发都包含某些元数据,你可以通过 <code class="docutils literal notranslate"><span class="pre">metadata()</span></code> 函数提取它们:</p>
|
||
<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>
|
||
</pre></div>
|
||
</div>
|
||
<p>The keys of the returned data structure <a class="footnote-reference brackets" href="#f1" id="id2">1</a> name the metadata keywords, and
|
||
their values are returned unparsed from the distribution metadata:</p>
|
||
<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>
|
||
<span class="go">'>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="distribution-versions">
|
||
<span id="version"></span><h3>分发包的版本<a class="headerlink" href="#distribution-versions" title="永久链接至标题">¶</a></h3>
|
||
<p><code class="docutils literal notranslate"><span class="pre">version()</span></code> 函数是以字符串形式获取分发的版本号的最快方式:</p>
|
||
<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>
|
||
<span class="go">'0.32.3'</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="distribution-files">
|
||
<span id="files"></span><h3>分发包的文件<a class="headerlink" href="#distribution-files" title="永久链接至标题">¶</a></h3>
|
||
<p>You can also get the full set of files contained within a distribution. The
|
||
<code class="docutils literal notranslate"><span class="pre">files()</span></code> function takes a distribution package name and returns all of the
|
||
files installed by this distribution. Each file object returned is a
|
||
<code class="docutils literal notranslate"><span class="pre">PackagePath</span></code>, a <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> derived object with additional <code class="docutils literal notranslate"><span class="pre">dist</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">size</span></code>, and <code class="docutils literal notranslate"><span class="pre">hash</span></code> properties as indicated by the metadata. For example:</p>
|
||
<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>
|
||
<span class="gp">>>> </span><span class="n">util</span>
|
||
<span class="go">PackagePath('wheel/util.py')</span>
|
||
<span class="gp">>>> </span><span class="n">util</span><span class="o">.</span><span class="n">size</span>
|
||
<span class="go">859</span>
|
||
<span class="gp">>>> </span><span class="n">util</span><span class="o">.</span><span class="n">dist</span>
|
||
<span class="go"><importlib.metadata._hooks.PathDistribution object at 0x101e0cef0></span>
|
||
<span class="gp">>>> </span><span class="n">util</span><span class="o">.</span><span class="n">hash</span>
|
||
<span class="go"><FileHash mode: sha256 value: bYkw5oMccfazVCoYQwKkkemoVyMAFoR34mmKBx8R1NI></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>当你获得了文件对象,你可以读取其内容:</p>
|
||
<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>
|
||
<span class="go">import base64</span>
|
||
<span class="go">import sys</span>
|
||
<span class="go">...</span>
|
||
<span class="go">def as_bytes(s):</span>
|
||
<span class="go"> if isinstance(s, text_type):</span>
|
||
<span class="go"> return s.encode('utf-8')</span>
|
||
<span class="go"> return s</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>当列出包含文件的元数据文件(RECORD 或 SOURCES.txt)不存在时, <code class="docutils literal notranslate"><span class="pre">files()</span></code> 函数将返回 <code class="docutils literal notranslate"><span class="pre">None</span></code> 。调用者可能会想要将对 <code class="docutils literal notranslate"><span class="pre">files()</span></code> 的调用封装在 <a class="reference external" href="https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.always_iterable">always_iterable</a> 中,或者用其他方法来应对目标分发元数据存在性未知的情况。</p>
|
||
</section>
|
||
<section id="distribution-requirements">
|
||
<span id="requirements"></span><h3>分发包的依赖<a class="headerlink" href="#distribution-requirements" title="永久链接至标题">¶</a></h3>
|
||
<p>使用 <code class="docutils literal notranslate"><span class="pre">requires()</span></code> 函数来获得一个 分发 的所有依赖:</p>
|
||
<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>
|
||
<span class="go">["pytest (>=3.0.0) ; extra == 'test'", "pytest-cov ; extra == 'test'"]</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="distributions">
|
||
<h2>分发包对象<a class="headerlink" href="#distributions" title="永久链接至标题">¶</a></h2>
|
||
<p>以上的 API 是最常见而方便的用法,但是你也可以通过 <code class="docutils literal notranslate"><span class="pre">Distribution</span></code> 类获得以上所有信息。 <code class="docutils literal notranslate"><span class="pre">Distribution</span></code> 是一个代表 Python 包的元数据的抽象对象。你可以这样获取 <code class="docutils literal notranslate"><span class="pre">Distribution</span></code> 实例:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">importlib.metadata</span> <span class="kn">import</span> <span class="n">distribution</span>
|
||
<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>
|
||
</pre></div>
|
||
</div>
|
||
<p>因此,可以通过 <code class="docutils literal notranslate"><span class="pre">Distribution</span></code> 实例获得版本号:</p>
|
||
<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>
|
||
<span class="go">'0.32.3'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p><code class="docutils literal notranslate"><span class="pre">Distribution</span></code> 实例具有所有可用的附加元数据:</p>
|
||
<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>
|
||
<span class="go">'>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'</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">'License'</span><span class="p">]</span>
|
||
<span class="go">'MIT'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>此处并未描述全部可用的元数据集合。 请参见 <span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0566"><strong>PEP 566</strong></a> 以了解更多细节。</p>
|
||
</section>
|
||
<section id="extending-the-search-algorithm">
|
||
<h2>扩展搜索算法<a class="headerlink" href="#extending-the-search-algorithm" title="永久链接至标题">¶</a></h2>
|
||
<p>包的元数据无法通过搜索 <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> 或通过包加载器获得,而是通过导入系统的 <a class="reference internal" href="../reference/import.html#finders-and-loaders"><span class="std std-ref">查找器</span></a> 找到的。 <code class="docutils literal notranslate"><span class="pre">importlib.metadata</span></code> 查询在 <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> 上的 <a class="reference internal" href="../glossary.html#term-meta-path-finder"><span class="xref std std-term">元数据查找器</span></a> 列表以获得分发包的元数据。</p>
|
||
<p>Python 默认的 <code class="docutils literal notranslate"><span class="pre">PathFinder</span></code> 包含一个调用 <code class="docutils literal notranslate"><span class="pre">importlib.metadata.MetadataPathFinder</span></code> 来查找从典型的文件系统路径加载发布的钩子。</p>
|
||
<p>抽象基类 <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> 定义了 Python 导入系统期望的查找器接口。 <code class="docutils literal notranslate"><span class="pre">importlib.metadata</span></code> 通过寻找 <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> 上查找器可选的 <code class="docutils literal notranslate"><span class="pre">find_distributions</span></code> 可调用的属性扩展这个协议,并将这个扩展接口作为 <code class="docutils literal notranslate"><span class="pre">DistributionFinder</span></code> 抽象基类提供,它定义了这个抽象方法:</p>
|
||
<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>
|
||
<span class="k">def</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="w"> </span><span class="sd">"""Return an iterable of all Distribution instances capable of</span>
|
||
<span class="sd"> loading the metadata for packages for the indicated ``context``.</span>
|
||
<span class="sd"> """</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">DistributionFinder.Context</span></code> object provides <code class="docutils literal notranslate"><span class="pre">.path</span></code> and <code class="docutils literal notranslate"><span class="pre">.name</span></code>
|
||
properties indicating the path to search and names to match and may
|
||
supply other relevant context.</p>
|
||
<p>这在实践中意味着要支持在文件系统外的其他位置查找分发包的元数据,你需要子类化 <code class="docutils literal notranslate"><span class="pre">Distribution</span></code> 并实现抽象方法,之后从一个自定义查找器的 <code class="docutils literal notranslate"><span class="pre">find_distributions()</span></code> 方法返回这个派生的 <code class="docutils literal notranslate"><span class="pre">Distribution</span></code> 实例。</p>
|
||
<p class="rubric">备注</p>
|
||
<dl class="footnote brackets">
|
||
<dt class="label" id="f1"><span class="brackets"><a class="fn-backref" href="#id2">1</a></span></dt>
|
||
<dd><p>Technically, the returned distribution metadata object is an
|
||
<a class="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">email.message.EmailMessage</span></code></a>
|
||
instance, but this is an implementation detail, and not part of the
|
||
stable API. You should only use dictionary-like methods and syntax
|
||
to access the metadata contents.</p>
|
||
</dd>
|
||
</dl>
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#">使用 <code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.metadata</span></code></a><ul>
|
||
<li><a class="reference internal" href="#overview">概述</a></li>
|
||
<li><a class="reference internal" href="#functional-api">函数式 API</a><ul>
|
||
<li><a class="reference internal" href="#entry-points">入口点</a></li>
|
||
<li><a class="reference internal" href="#distribution-metadata">分发的元数据</a></li>
|
||
<li><a class="reference internal" href="#distribution-versions">分发包的版本</a></li>
|
||
<li><a class="reference internal" href="#distribution-files">分发包的文件</a></li>
|
||
<li><a class="reference internal" href="#distribution-requirements">分发包的依赖</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#distributions">分发包对象</a></li>
|
||
<li><a class="reference internal" href="#extending-the-search-algorithm">扩展搜索算法</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="importlib.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code> --- <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code> 的实现</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="language.html"
|
||
title="下一章">Python 语言服务</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/importlib.metadata.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
>索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="language.html" title="Python 语言服务"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="importlib.html" title="importlib --- import 的实现"
|
||
>上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="modules.html" >导入模块</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
|
||
<br />
|
||
This page is licensed under the Python Software Foundation License Version 2.
|
||
<br />
|
||
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
|
||
<br />
|
||
|
||
<br />
|
||
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
<br />
|
||
|
||
最后更新于 12月 09, 2024.
|
||
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
|
||
<br />
|
||
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |