1.. _using:
2
3=================================
4 Using :mod:`!importlib.metadata`
5=================================
6
7.. module:: importlib.metadata
8   :synopsis: The implementation of the importlib metadata.
9
10.. versionadded:: 3.8
11.. versionchanged:: 3.10
12   ``importlib.metadata`` is no longer provisional.
13
14**Source code:** :source:`Lib/importlib/metadata/__init__.py`
15
16``importlib_metadata`` is a library that provides access to
17the metadata of an installed `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_,
18such as its entry points
19or its top-level names (`Import Package <https://packaging.python.org/en/latest/glossary/#term-Import-Package>`_\s, modules, if any).
20Built in part on Python's import system, this library
21intends to replace similar functionality in the `entry point
22API`_ and `metadata API`_ of ``pkg_resources``.  Along with
23:mod:`importlib.resources`,
24this package can eliminate the need to use the older and less efficient
25``pkg_resources`` package.
26
27``importlib_metadata`` operates on third-party *distribution packages*
28installed into Python's ``site-packages`` directory via tools such as
29`pip <https://pypi.org/project/pip/>`_.
30Specifically, it works with distributions with discoverable
31``dist-info`` or ``egg-info`` directories,
32and metadata defined by the `Core metadata specifications <https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata>`_.
33
34.. important::
35
36   These are *not* necessarily equivalent to or correspond 1:1 with
37   the top-level *import package* names
38   that can be imported inside Python code.
39   One *distribution package* can contain multiple *import packages*
40   (and single modules),
41   and one top-level *import package*
42   may map to multiple *distribution packages*
43   if it is a namespace package.
44   You can use :ref:`package_distributions() <package-distributions>`
45   to get a mapping between them.
46
47By default, distribution metadata can live on the file system
48or in zip archives on
49:data:`sys.path`.  Through an extension mechanism, the metadata can live almost
50anywhere.
51
52
53.. seealso::
54
55   https://importlib-metadata.readthedocs.io/
56      The documentation for ``importlib_metadata``, which supplies a
57      backport of ``importlib.metadata``.
58      This includes an `API reference
59      <https://importlib-metadata.readthedocs.io/en/latest/api.html>`__
60      for this module's classes and functions,
61      as well as a `migration guide
62      <https://importlib-metadata.readthedocs.io/en/latest/migration.html>`__
63      for existing users of ``pkg_resources``.
64
65
66Overview
67========
68
69Let's say you wanted to get the version string for a
70`Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_ you've installed
71using ``pip``.  We start by creating a virtual environment and installing
72something into it:
73
74.. code-block:: shell-session
75
76    $ python -m venv example
77    $ source example/bin/activate
78    (example) $ python -m pip install wheel
79
80You can get the version string for ``wheel`` by running the following:
81
82.. code-block:: pycon
83
84    (example) $ python
85    >>> from importlib.metadata import version  # doctest: +SKIP
86    >>> version('wheel')  # doctest: +SKIP
87    '0.32.3'
88
89You can also get a collection of entry points selectable by properties of the EntryPoint (typically 'group' or 'name'), such as
90``console_scripts``, ``distutils.commands`` and others.  Each group contains a
91collection of :ref:`EntryPoint <entry-points>` objects.
92
93You can get the :ref:`metadata for a distribution <metadata>`::
94
95    >>> list(metadata('wheel'))  # doctest: +SKIP
96    ['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']
97
98You can also get a :ref:`distribution's version number <version>`, list its
99:ref:`constituent files <files>`, and get a list of the distribution's
100:ref:`requirements`.
101
102
103Functional API
104==============
105
106This package provides the following functionality via its public API.
107
108
109.. _entry-points:
110
111Entry points
112------------
113
114The ``entry_points()`` function returns a collection of entry points.
115Entry points are represented by ``EntryPoint`` instances;
116each ``EntryPoint`` has a ``.name``, ``.group``, and ``.value`` attributes and
117a ``.load()`` method to resolve the value.  There are also ``.module``,
118``.attr``, and ``.extras`` attributes for getting the components of the
119``.value`` attribute.
120
121Query all entry points::
122
123    >>> eps = entry_points()  # doctest: +SKIP
124
125The ``entry_points()`` function returns an ``EntryPoints`` object,
126a collection of all ``EntryPoint`` objects with ``names`` and ``groups``
127attributes for convenience::
128
129    >>> sorted(eps.groups)  # doctest: +SKIP
130    ['console_scripts', 'distutils.commands', 'distutils.setup_keywords', 'egg_info.writers', 'setuptools.installation']
131
132``EntryPoints`` has a ``select`` method to select entry points
133matching specific properties. Select entry points in the
134``console_scripts`` group::
135
136    >>> scripts = eps.select(group='console_scripts')  # doctest: +SKIP
137
138Equivalently, since ``entry_points`` passes keyword arguments
139through to select::
140
141    >>> scripts = entry_points(group='console_scripts')  # doctest: +SKIP
142
143Pick out a specific script named "wheel" (found in the wheel project)::
144
145    >>> 'wheel' in scripts.names  # doctest: +SKIP
146    True
147    >>> wheel = scripts['wheel']  # doctest: +SKIP
148
149Equivalently, query for that entry point during selection::
150
151    >>> (wheel,) = entry_points(group='console_scripts', name='wheel')  # doctest: +SKIP
152    >>> (wheel,) = entry_points().select(group='console_scripts', name='wheel')  # doctest: +SKIP
153
154Inspect the resolved entry point::
155
156    >>> wheel  # doctest: +SKIP
157    EntryPoint(name='wheel', value='wheel.cli:main', group='console_scripts')
158    >>> wheel.module  # doctest: +SKIP
159    'wheel.cli'
160    >>> wheel.attr  # doctest: +SKIP
161    'main'
162    >>> wheel.extras  # doctest: +SKIP
163    []
164    >>> main = wheel.load()  # doctest: +SKIP
165    >>> main  # doctest: +SKIP
166    <function main at 0x103528488>
167
168The ``group`` and ``name`` are arbitrary values defined by the package author
169and usually a client will wish to resolve all entry points for a particular
170group.  Read `the setuptools docs
171<https://setuptools.pypa.io/en/latest/userguide/entry_point.html>`_
172for more information on entry points, their definition, and usage.
173
174*Compatibility Note*
175
176The "selectable" entry points were introduced in ``importlib_metadata``
1773.6 and Python 3.10. Prior to those changes, ``entry_points`` accepted
178no parameters and always returned a dictionary of entry points, keyed
179by group. For compatibility, if no parameters are passed to entry_points,
180a ``SelectableGroups`` object is returned, implementing that dict
181interface. In the future, calling ``entry_points`` with no parameters
182will return an ``EntryPoints`` object. Users should rely on the selection
183interface to retrieve entry points by group.
184
185
186.. _metadata:
187
188Distribution metadata
189---------------------
190
191Every `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_ includes some metadata,
192which you can extract using the
193``metadata()`` function::
194
195    >>> wheel_metadata = metadata('wheel')  # doctest: +SKIP
196
197The keys of the returned data structure, a ``PackageMetadata``,
198name the metadata keywords, and
199the values are returned unparsed from the distribution metadata::
200
201    >>> wheel_metadata['Requires-Python']  # doctest: +SKIP
202    '>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'
203
204``PackageMetadata`` also presents a ``json`` attribute that returns
205all the metadata in a JSON-compatible form per :PEP:`566`::
206
207    >>> wheel_metadata.json['requires_python']
208    '>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'
209
210.. note::
211
212    The actual type of the object returned by ``metadata()`` is an
213    implementation detail and should be accessed only through the interface
214    described by the
215    `PackageMetadata protocol <https://importlib-metadata.readthedocs.io/en/latest/api.html#importlib_metadata.PackageMetadata>`_.
216
217.. versionchanged:: 3.10
218   The ``Description`` is now included in the metadata when presented
219   through the payload. Line continuation characters have been removed.
220
221.. versionadded:: 3.10
222   The ``json`` attribute was added.
223
224
225.. _version:
226
227Distribution versions
228---------------------
229
230The ``version()`` function is the quickest way to get a
231`Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_'s version
232number, as a string::
233
234    >>> version('wheel')  # doctest: +SKIP
235    '0.32.3'
236
237
238.. _files:
239
240Distribution files
241------------------
242
243You can also get the full set of files contained within a distribution.  The
244``files()`` function takes a `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_ name
245and returns all of the
246files installed by this distribution.  Each file object returned is a
247``PackagePath``, a :class:`pathlib.PurePath` derived object with additional ``dist``,
248``size``, and ``hash`` properties as indicated by the metadata.  For example::
249
250    >>> util = [p for p in files('wheel') if 'util.py' in str(p)][0]  # doctest: +SKIP
251    >>> util  # doctest: +SKIP
252    PackagePath('wheel/util.py')
253    >>> util.size  # doctest: +SKIP
254    859
255    >>> util.dist  # doctest: +SKIP
256    <importlib.metadata._hooks.PathDistribution object at 0x101e0cef0>
257    >>> util.hash  # doctest: +SKIP
258    <FileHash mode: sha256 value: bYkw5oMccfazVCoYQwKkkemoVyMAFoR34mmKBx8R1NI>
259
260Once you have the file, you can also read its contents::
261
262    >>> print(util.read_text())  # doctest: +SKIP
263    import base64
264    import sys
265    ...
266    def as_bytes(s):
267        if isinstance(s, text_type):
268            return s.encode('utf-8')
269        return s
270
271You can also use the ``locate`` method to get a the absolute path to the
272file::
273
274    >>> util.locate()  # doctest: +SKIP
275    PosixPath('/home/gustav/example/lib/site-packages/wheel/util.py')
276
277In the case where the metadata file listing files
278(RECORD or SOURCES.txt) is missing, ``files()`` will
279return ``None``. The caller may wish to wrap calls to
280``files()`` in `always_iterable
281<https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.always_iterable>`_
282or otherwise guard against this condition if the target
283distribution is not known to have the metadata present.
284
285.. _requirements:
286
287Distribution requirements
288-------------------------
289
290To get the full set of requirements for a `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_,
291use the ``requires()``
292function::
293
294    >>> requires('wheel')  # doctest: +SKIP
295    ["pytest (>=3.0.0) ; extra == 'test'", "pytest-cov ; extra == 'test'"]
296
297
298.. _package-distributions:
299.. _import-distribution-package-mapping:
300
301Mapping import to distribution packages
302---------------------------------------
303
304A convenience method to resolve the `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_
305name (or names, in the case of a namespace package)
306that provide each importable top-level
307Python module or `Import Package <https://packaging.python.org/en/latest/glossary/#term-Import-Package>`_::
308
309    >>> packages_distributions()
310    {'importlib_metadata': ['importlib-metadata'], 'yaml': ['PyYAML'], 'jaraco': ['jaraco.classes', 'jaraco.functools'], ...}
311
312.. versionadded:: 3.10
313
314.. _distributions:
315
316Distributions
317=============
318
319While the above API is the most common and convenient usage, you can get all
320of that information from the ``Distribution`` class.  A ``Distribution`` is an
321abstract object that represents the metadata for
322a Python `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_.  You can
323get the ``Distribution`` instance::
324
325    >>> from importlib.metadata import distribution  # doctest: +SKIP
326    >>> dist = distribution('wheel')  # doctest: +SKIP
327
328Thus, an alternative way to get the version number is through the
329``Distribution`` instance::
330
331    >>> dist.version  # doctest: +SKIP
332    '0.32.3'
333
334There are all kinds of additional metadata available on the ``Distribution``
335instance::
336
337    >>> dist.metadata['Requires-Python']  # doctest: +SKIP
338    '>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'
339    >>> dist.metadata['License']  # doctest: +SKIP
340    'MIT'
341
342The full set of available metadata is not described here.
343See the `Core metadata specifications <https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata>`_ for additional details.
344
345
346Distribution Discovery
347======================
348
349By default, this package provides built-in support for discovery of metadata
350for file system and zip file `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_\s.
351This metadata finder search defaults to ``sys.path``, but varies slightly in how it interprets those values from how other import machinery does. In particular:
352
353- ``importlib.metadata`` does not honor :class:`bytes` objects on ``sys.path``.
354- ``importlib.metadata`` will incidentally honor :py:class:`pathlib.Path` objects on ``sys.path`` even though such values will be ignored for imports.
355
356
357Extending the search algorithm
358==============================
359
360Because `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_ metadata
361is not available through :data:`sys.path` searches, or
362package loaders directly,
363the metadata for a distribution is found through import
364system `finders`_.  To find a distribution package's metadata,
365``importlib.metadata`` queries the list of :term:`meta path finders <meta path finder>` on
366:data:`sys.meta_path`.
367
368By default ``importlib_metadata`` installs a finder for distribution packages
369found on the file system.
370This finder doesn't actually find any *distributions*,
371but it can find their metadata.
372
373The abstract class :py:class:`importlib.abc.MetaPathFinder` defines the
374interface expected of finders by Python's import system.
375``importlib.metadata`` extends this protocol by looking for an optional
376``find_distributions`` callable on the finders from
377:data:`sys.meta_path` and presents this extended interface as the
378``DistributionFinder`` abstract base class, which defines this abstract
379method::
380
381    @abc.abstractmethod
382    def find_distributions(context=DistributionFinder.Context()):
383        """Return an iterable of all Distribution instances capable of
384        loading the metadata for packages for the indicated ``context``.
385        """
386
387The ``DistributionFinder.Context`` object provides ``.path`` and ``.name``
388properties indicating the path to search and name to match and may
389supply other relevant context.
390
391What this means in practice is that to support finding distribution package
392metadata in locations other than the file system, subclass
393``Distribution`` and implement the abstract methods. Then from
394a custom finder, return instances of this derived ``Distribution`` in the
395``find_distributions()`` method.
396
397
398.. _`entry point API`: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points
399.. _`metadata API`: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#metadata-api
400.. _`finders`: https://docs.python.org/3/reference/import.html#finders-and-loaders
401