using.rst 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. .. _using:
  2. ==========================
  3. Using importlib_metadata
  4. ==========================
  5. ``importlib_metadata`` is a library that provides for access to installed
  6. package metadata. Built in part on Python's import system, this library
  7. intends to replace similar functionality in the `entry point
  8. API`_ and `metadata API`_ of ``pkg_resources``. Along with
  9. ``importlib.resources`` in `Python 3.7
  10. and newer`_ (backported as `importlib_resources`_ for older versions of
  11. Python), this can eliminate the need to use the older and less efficient
  12. ``pkg_resources`` package.
  13. By "installed package" we generally mean a third-party package installed into
  14. Python's ``site-packages`` directory via tools such as `pip
  15. <https://pypi.org/project/pip/>`_. Specifically,
  16. it means a package with either a discoverable ``dist-info`` or ``egg-info``
  17. directory, and metadata defined by `PEP 566`_ or its older specifications.
  18. By default, package metadata can live on the file system or in zip archives on
  19. ``sys.path``. Through an extension mechanism, the metadata can live almost
  20. anywhere.
  21. Overview
  22. ========
  23. Let's say you wanted to get the version string for a package you've installed
  24. using ``pip``. We start by creating a virtual environment and installing
  25. something into it::
  26. $ python3 -m venv example
  27. $ source example/bin/activate
  28. (example) $ pip install importlib_metadata
  29. (example) $ pip install wheel
  30. You can get the version string for ``wheel`` by running the following::
  31. (example) $ python
  32. >>> from importlib_metadata import version
  33. >>> version('wheel')
  34. '0.32.3'
  35. You can also get the set of entry points keyed by group, such as
  36. ``console_scripts``, ``distutils.commands`` and others. Each group contains a
  37. sequence of :ref:`EntryPoint <entry-points>` objects.
  38. You can get the :ref:`metadata for a distribution <metadata>`::
  39. >>> list(metadata('wheel'))
  40. ['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']
  41. You can also get a :ref:`distribution's version number <version>`, list its
  42. :ref:`constituent files <files>`, and get a list of the distribution's
  43. :ref:`requirements`.
  44. Functional API
  45. ==============
  46. This package provides the following functionality via its public API.
  47. .. _entry-points:
  48. Entry points
  49. ------------
  50. The ``entry_points()`` function returns a dictionary of all entry points,
  51. keyed by group. Entry points are represented by ``EntryPoint`` instances;
  52. each ``EntryPoint`` has a ``.name``, ``.group``, and ``.value`` attributes and
  53. a ``.load()`` method to resolve the value::
  54. >>> eps = entry_points()
  55. >>> list(eps)
  56. ['console_scripts', 'distutils.commands', 'distutils.setup_keywords', 'egg_info.writers', 'setuptools.installation']
  57. >>> scripts = eps['console_scripts']
  58. >>> wheel = [ep for ep in scripts if ep.name == 'wheel'][0]
  59. >>> wheel
  60. EntryPoint(name='wheel', value='wheel.cli:main', group='console_scripts')
  61. >>> main = wheel.load()
  62. >>> main
  63. <function main at 0x103528488>
  64. The ``group`` and ``name`` are arbitrary values defined by the package author
  65. and usually a client will wish to resolve all entry points for a particular
  66. group. Read `the setuptools docs
  67. <https://setuptools.readthedocs.io/en/latest/setuptools.html#dynamic-discovery-of-services-and-plugins>`_
  68. for more information on entrypoints, their definition, and usage.
  69. .. _metadata:
  70. Distribution metadata
  71. ---------------------
  72. Every distribution includes some metadata, which you can extract using the
  73. ``metadata()`` function::
  74. >>> wheel_metadata = metadata('wheel')
  75. The keys of the returned data structure [#f1]_ name the metadata keywords, and
  76. their values are returned unparsed from the distribution metadata::
  77. >>> wheel_metadata['Requires-Python']
  78. '>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'
  79. .. _version:
  80. Distribution versions
  81. ---------------------
  82. The ``version()`` function is the quickest way to get a distribution's version
  83. number, as a string::
  84. >>> version('wheel')
  85. '0.32.3'
  86. .. _files:
  87. Distribution files
  88. ------------------
  89. You can also get the full set of files contained within a distribution. The
  90. ``files()`` function takes a distribution package name and returns all of the
  91. files installed by this distribution. Each file object returned is a
  92. ``PackagePath``, a `pathlib.Path`_ derived object with additional ``dist``,
  93. ``size``, and ``hash`` properties as indicated by the metadata. For example::
  94. >>> util = [p for p in files('wheel') if 'util.py' in str(p)][0]
  95. >>> util
  96. PackagePath('wheel/util.py')
  97. >>> util.size
  98. 859
  99. >>> util.dist
  100. <importlib_metadata._hooks.PathDistribution object at 0x101e0cef0>
  101. >>> util.hash
  102. <FileHash mode: sha256 value: bYkw5oMccfazVCoYQwKkkemoVyMAFoR34mmKBx8R1NI>
  103. Once you have the file, you can also read its contents::
  104. >>> print(util.read_text())
  105. import base64
  106. import sys
  107. ...
  108. def as_bytes(s):
  109. if isinstance(s, text_type):
  110. return s.encode('utf-8')
  111. return s
  112. .. _requirements:
  113. Distribution requirements
  114. -------------------------
  115. To get the full set of requirements for a distribution, use the ``requires()``
  116. function. Note that this returns an iterator::
  117. >>> list(requires('wheel'))
  118. ["pytest (>=3.0.0) ; extra == 'test'"]
  119. Distributions
  120. =============
  121. While the above API is the most common and convenient usage, you can get all
  122. of that information from the ``Distribution`` class. A ``Distribution`` is an
  123. abstract object that represents the metadata for a Python package. You can
  124. get the ``Distribution`` instance::
  125. >>> from importlib_metadata import distribution
  126. >>> dist = distribution('wheel')
  127. Thus, an alternative way to get the version number is through the
  128. ``Distribution`` instance::
  129. >>> dist.version
  130. '0.32.3'
  131. There are all kinds of additional metadata available on the ``Distribution``
  132. instance::
  133. >>> d.metadata['Requires-Python']
  134. '>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'
  135. >>> d.metadata['License']
  136. 'MIT'
  137. The full set of available metadata is not described here. See `PEP 566
  138. <https://www.python.org/dev/peps/pep-0566/>`_ for additional details.
  139. Extending the search algorithm
  140. ==============================
  141. Because package metadata is not available through ``sys.path`` searches, or
  142. package loaders directly, the metadata for a package is found through import
  143. system `finders`_. To find a distribution package's metadata,
  144. ``importlib_metadata`` queries the list of `meta path finders`_ on
  145. `sys.meta_path`_.
  146. By default ``importlib_metadata`` installs a finder for distribution packages
  147. found on the file system. This finder doesn't actually find any *packages*,
  148. but it can find the packages' metadata.
  149. The abstract class :py:class:`importlib.abc.MetaPathFinder` defines the
  150. interface expected of finders by Python's import system.
  151. ``importlib_metadata`` extends this protocol by looking for an optional
  152. ``find_distributions`` callable on the finders from
  153. ``sys.meta_path``. If the finder has this method, it must return
  154. an iterator over instances of the ``Distribution`` abstract class. This
  155. method must have the signature::
  156. def find_distributions(name=None, path=None):
  157. """Return an iterable of all Distribution instances capable of
  158. loading the metadata for packages matching the name
  159. (or all names if not supplied) along the paths in the list
  160. of directories ``path`` (defaults to sys.path).
  161. """
  162. What this means in practice is that to support finding distribution package
  163. metadata in locations other than the file system, you should derive from
  164. ``Distribution`` and implement the ``load_metadata()`` method. This takes a
  165. single argument which is the name of the package whose metadata is being
  166. found. This instance of the ``Distribution`` base abstract class is what your
  167. finder's ``find_distributions()`` method should return.
  168. .. _`entry point API`: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points
  169. .. _`metadata API`: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#metadata-api
  170. .. _`Python 3.7 and newer`: https://docs.python.org/3/library/importlib.html#module-importlib.resources
  171. .. _`importlib_resources`: https://importlib-resources.readthedocs.io/en/latest/index.html
  172. .. _`PEP 566`: https://www.python.org/dev/peps/pep-0566/
  173. .. _`finders`: https://docs.python.org/3/reference/import.html#finders-and-loaders
  174. .. _`meta path finders`: https://docs.python.org/3/glossary.html#term-meta-path-finder
  175. .. _`sys.meta_path`: https://docs.python.org/3/library/sys.html#sys.meta_path
  176. .. _`pathlib.Path`: https://docs.python.org/3/library/pathlib.html#pathlib.Path
  177. .. rubric:: Footnotes
  178. .. [#f1] Technically, the returned distribution metadata object is an
  179. `email.message.Message
  180. <https://docs.python.org/3/library/email.message.html#email.message.EmailMessage>`_
  181. instance, but this is an implementation detail, and not part of the
  182. stable API. You should only use dictionary-like methods and syntax
  183. to access the metadata contents.