Skip to content

Commit 165d7b7

Browse files
authored
Merge pull request #399 from CAM-Gerlach/clarify-import-vs-distribution-package
Clarify import vs. distribution packages in Using importlib_metadata docs
2 parents 6bee315 + 92a16db commit 165d7b7

File tree

2 files changed

+67
-27
lines changed

2 files changed

+67
-27
lines changed

docs/conf.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@
5656
),
5757
)
5858

59+
intersphinx_mapping.update(
60+
packaging=(
61+
'https://packaging.python.org/en/latest/',
62+
None,
63+
),
64+
)
65+
5966
# Workaround for #316
6067
nitpick_ignore = [
6168
('py:class', 'importlib_metadata.EntryPoints'),

docs/using.rst

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,39 @@
44
Using :mod:`!importlib_metadata`
55
=================================
66

7-
``importlib_metadata`` is a library that provides access to installed
8-
package metadata, such as its entry points or its
9-
top-level name. Built in part on Python's import system, this library
7+
``importlib_metadata`` is a library that provides access to
8+
the metadata of an installed :term:`packaging:Distribution Package`,
9+
such as its entry points
10+
or its top-level names (:term:`packaging:Import Package`s, modules, if any).
11+
Built in part on Python's import system, this library
1012
intends to replace similar functionality in the `entry point
1113
API`_ and `metadata API`_ of ``pkg_resources``. Along with
1214
:mod:`importlib.resources`,
1315
this package can eliminate the need to use the older and less efficient
1416
``pkg_resources`` package.
1517

16-
By "installed package" we generally mean a third-party package installed into
17-
Python's ``site-packages`` directory via tools such as `pip
18-
<https://pypi.org/project/pip/>`_. Specifically,
19-
it means a package with either a discoverable ``dist-info`` or ``egg-info``
20-
directory, and metadata defined by :pep:`566` or its older specifications.
21-
By default, package metadata can live on the file system or in zip archives on
18+
``importlib_metadata`` operates on third-party *distribution packages*
19+
installed into Python's ``site-packages`` directory via tools such as
20+
`pip <https://pypi.org/project/pip/>`_.
21+
Specifically, it works with distributions with discoverable
22+
``dist-info`` or ``egg-info`` directories,
23+
and metadata defined by the :ref:`packaging:core-metadata`.
24+
25+
.. important::
26+
27+
These are *not* necessarily equivalent to or correspond 1:1 with
28+
the top-level *import package* names
29+
that can be imported inside Python code.
30+
One *distribution package* can contain multiple *import packages*
31+
(and single modules),
32+
and one top-level *import package*
33+
may map to multiple *distribution packages*
34+
if it is a namespace package.
35+
You can use :ref:`package_distributions() <package-distributions>`
36+
to get a mapping between them.
37+
38+
By default, distribution metadata can live on the file system
39+
or in zip archives on
2240
:data:`sys.path`. Through an extension mechanism, the metadata can live almost
2341
anywhere.
2442

@@ -33,7 +51,8 @@ anywhere.
3351
Overview
3452
========
3553

36-
Let's say you wanted to get the version string for a package you've installed
54+
Let's say you wanted to get the version string for a
55+
:term:`packaging:Distribution Package` you've installed
3756
using ``pip``. We start by creating a virtual environment and installing
3857
something into it::
3958

@@ -151,7 +170,8 @@ interface to retrieve entry points by group.
151170
Distribution metadata
152171
---------------------
153172

154-
Every distribution includes some metadata, which you can extract using the
173+
Every :term:`packaging:Distribution Package` includes some metadata,
174+
which you can extract using the
155175
``metadata()`` function::
156176

157177
>>> wheel_metadata = metadata('wheel')
@@ -182,7 +202,8 @@ all the metadata in a JSON-compatible form per PEP 566::
182202
Distribution versions
183203
---------------------
184204

185-
The ``version()`` function is the quickest way to get a distribution's version
205+
The ``version()`` function is the quickest way to get a
206+
:term:`packaging:Distribution Package`'s version
186207
number, as a string::
187208

188209
>>> version('wheel')
@@ -195,7 +216,8 @@ Distribution files
195216
------------------
196217

197218
You can also get the full set of files contained within a distribution. The
198-
``files()`` function takes a distribution package name and returns all of the
219+
``files()`` function takes a :term:`packaging:Distribution Package` name
220+
and returns all of the
199221
files installed by this distribution. Each file object returned is a
200222
``PackagePath``, a :class:`pathlib.PurePath` derived object with additional ``dist``,
201223
``size``, and ``hash`` properties as indicated by the metadata. For example::
@@ -240,19 +262,24 @@ distribution is not known to have the metadata present.
240262
Distribution requirements
241263
-------------------------
242264

243-
To get the full set of requirements for a distribution, use the ``requires()``
265+
To get the full set of requirements for a :term:`packaging:Distribution Package`,
266+
use the ``requires()``
244267
function::
245268

246269
>>> requires('wheel')
247270
["pytest (>=3.0.0) ; extra == 'test'", "pytest-cov ; extra == 'test'"]
248271

249272

250-
Package distributions
251-
---------------------
273+
.. _package-distributions:
274+
.. _import-distribution-package-mapping:
275+
276+
Mapping import to distribution packages
277+
---------------------------------------
252278

253-
A convenience method to resolve the distribution or
254-
distributions (in the case of a namespace package) for top-level
255-
Python packages or modules::
279+
A convenience method to resolve the :term:`packaging:Distribution Package`
280+
name (or names, in the case of a namespace package)
281+
that provide each importable top-level
282+
Python module or :term:`packaging:Import Package`::
256283

257284
>>> packages_distributions()
258285
{'importlib_metadata': ['importlib-metadata'], 'yaml': ['PyYAML'], 'jaraco': ['jaraco.classes', 'jaraco.functools'], ...}
@@ -264,7 +291,8 @@ Distributions
264291

265292
While the above API is the most common and convenient usage, you can get all
266293
of that information from the ``Distribution`` class. A ``Distribution`` is an
267-
abstract object that represents the metadata for a Python package. You can
294+
abstract object that represents the metadata for
295+
a Python :term:`packaging:Distribution Package`. You can
268296
get the ``Distribution`` instance::
269297

270298
>>> from importlib_metadata import distribution
@@ -284,14 +312,16 @@ instance::
284312
>>> dist.metadata['License']
285313
'MIT'
286314

287-
The full set of available metadata is not described here. See :pep:`566`
288-
for additional details.
315+
The full set of available metadata is not described here.
316+
See the :ref:`packaging:core-metadata` for additional details.
289317

290318

291319
Distribution Discovery
292320
======================
293321

294-
By default, this package provides built-in support for discovery of metadata for file system and zip file packages. This metadata finder search defaults to ``sys.path``, but varies slightly in how it interprets those values from how other import machinery does. In particular:
322+
By default, this package provides built-in support for discovery of metadata
323+
for file system and zip file :term:`packaging:Distribution Package`\s.
324+
This metadata finder search defaults to ``sys.path``, but varies slightly in how it interprets those values from how other import machinery does. In particular:
295325

296326
- ``importlib_metadata`` does not honor :class:`bytes` objects on ``sys.path``.
297327
- ``importlib_metadata`` will incidentally honor :py:class:`pathlib.Path` objects on ``sys.path`` even though such values will be ignored for imports.
@@ -300,15 +330,18 @@ By default, this package provides built-in support for discovery of metadata for
300330
Extending the search algorithm
301331
==============================
302332

303-
Because package metadata is not available through :data:`sys.path` searches, or
304-
package loaders directly, the metadata for a package is found through import
333+
Because :term:`packaging:Distribution Package` metadata
334+
is not available through :data:`sys.path` searches, or
335+
package loaders directly,
336+
the metadata for a distribution is found through import
305337
system `finders`_. To find a distribution package's metadata,
306338
``importlib.metadata`` queries the list of :term:`meta path finders <meta path finder>` on
307339
:data:`sys.meta_path`.
308340

309341
By default ``importlib_metadata`` installs a finder for distribution packages
310-
found on the file system. This finder doesn't actually find any *packages*,
311-
but it can find the packages' metadata.
342+
found on the file system.
343+
This finder doesn't actually find any *distributions*,
344+
but it can find their metadata.
312345

313346
The abstract class :py:class:`importlib.abc.MetaPathFinder` defines the
314347
interface expected of finders by Python's import system.

0 commit comments

Comments
 (0)