1
1
.. _using :
2
2
3
- ==========================
4
- Using importlib.metadata
5
- ==========================
3
+ =================================
4
+ Using :mod: ` ! importlib.metadata`
5
+ =================================
6
6
7
7
.. note ::
8
8
This functionality is provisional and may deviate from the usual
12
12
package metadata. Built in part on Python's import system, this library
13
13
intends to replace similar functionality in the `entry point
14
14
API `_ and `metadata API `_ of ``pkg_resources ``. Along with
15
- `` importlib.resources `` in ` Python 3.7
16
- and newer `_ (backported as `importlib_resources `_ for older versions of
15
+ :mod: ` importlib.resources ` in Python 3.7
16
+ and newer (backported as `importlib_resources `_ for older versions of
17
17
Python), this can eliminate the need to use the older and less efficient
18
18
``pkg_resources `` package.
19
19
20
20
By "installed package" we generally mean a third-party package installed into
21
21
Python's ``site-packages `` directory via tools such as `pip
22
22
<https://pypi.org/project/pip/> `_. Specifically,
23
23
it means a package with either a discoverable ``dist-info `` or ``egg-info ``
24
- directory, and metadata defined by ` PEP 566 `_ or its older specifications.
24
+ directory, and metadata defined by :pep: ` 566 ` or its older specifications.
25
25
By default, package metadata can live on the file system or in zip archives on
26
- `` sys.path ` `. Through an extension mechanism, the metadata can live almost
26
+ :data: ` sys.path `. Through an extension mechanism, the metadata can live almost
27
27
anywhere.
28
28
29
29
@@ -134,7 +134,7 @@ Distribution files
134
134
You can also get the full set of files contained within a distribution. The
135
135
``files() `` function takes a distribution package name and returns all of the
136
136
files installed by this distribution. Each file object returned is a
137
- ``PackagePath ``, a `pathlib.Path `_ derived object with additional ``dist ``,
137
+ ``PackagePath ``, a :class: `pathlib.Path ` derived object with additional ``dist ``,
138
138
``size ``, and ``hash `` properties as indicated by the metadata. For example::
139
139
140
140
>>> util = [p for p in files('wheel') if 'util.py' in str(p)][0] # doctest: +SKIP
@@ -203,18 +203,18 @@ instance::
203
203
>>> d.metadata['License'] # doctest: +SKIP
204
204
'MIT'
205
205
206
- The full set of available metadata is not described here. See ` PEP 566
207
- <https://www.python.org/dev/peps/pep-0566/> `_ for additional details.
206
+ The full set of available metadata is not described here. See :pep: ` 566 `
207
+ for additional details.
208
208
209
209
210
210
Extending the search algorithm
211
211
==============================
212
212
213
- Because package metadata is not available through `` sys.path ` ` searches, or
213
+ Because package metadata is not available through :data: ` sys.path ` searches, or
214
214
package loaders directly, the metadata for a package is found through import
215
- system `finders `_ . To find a distribution package's metadata,
216
- ``importlib.metadata `` queries the list of `meta path finders `_ on
217
- `sys.meta_path `_ .
215
+ system :ref: `finders < finders-and-loaders >` . To find a distribution package's metadata,
216
+ ``importlib.metadata `` queries the list of :term: `meta path finders <meta path finder> ` on
217
+ :data: `sys.meta_path `.
218
218
219
219
The default ``PathFinder `` for Python includes a hook that calls into
220
220
``importlib.metadata.MetadataPathFinder `` for finding distributions
@@ -224,7 +224,7 @@ The abstract class :py:class:`importlib.abc.MetaPathFinder` defines the
224
224
interface expected of finders by Python's import system.
225
225
``importlib.metadata `` extends this protocol by looking for an optional
226
226
``find_distributions `` callable on the finders from
227
- `` sys.meta_path ` ` and presents this extended interface as the
227
+ :data: ` sys.meta_path ` and presents this extended interface as the
228
228
``DistributionFinder `` abstract base class, which defines this abstract
229
229
method::
230
230
@@ -247,20 +247,13 @@ a custom finder, return instances of this derived ``Distribution`` in the
247
247
248
248
.. _`entry point API` : https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points
249
249
.. _`metadata API` : https://setuptools.readthedocs.io/en/latest/pkg_resources.html#metadata-api
250
- .. _`Python 3.7 and newer` : https://docs.python.org/3/library/importlib.html#module-importlib.resources
251
250
.. _`importlib_resources` : https://importlib-resources.readthedocs.io/en/latest/index.html
252
- .. _`PEP 566` : https://www.python.org/dev/peps/pep-0566/
253
- .. _`finders` : https://docs.python.org/3/reference/import.html#finders-and-loaders
254
- .. _`meta path finders` : https://docs.python.org/3/glossary.html#term-meta-path-finder
255
- .. _`sys.meta_path` : https://docs.python.org/3/library/sys.html#sys.meta_path
256
- .. _`pathlib.Path` : https://docs.python.org/3/library/pathlib.html#pathlib.Path
257
251
258
252
259
253
.. rubric :: Footnotes
260
254
261
255
.. [#f1 ] Technically, the returned distribution metadata object is an
262
- `email.message.Message
263
- <https://docs.python.org/3/library/email.message.html#email.message.EmailMessage> `_
256
+ :class: `email.message.EmailMessage `
264
257
instance, but this is an implementation detail, and not part of the
265
258
stable API. You should only use dictionary-like methods and syntax
266
259
to access the metadata contents.
0 commit comments