|
| 1 | +.. _migration: |
| 2 | + |
| 3 | +================= |
| 4 | + Migration guide |
| 5 | +================= |
| 6 | + |
| 7 | +The following guide will help you migrate common ``pkg_resources`` |
| 8 | +APIs to ``importlib_metadata``. ``importlib_metadata`` aims to |
| 9 | +replace the following ``pkg_resources`` APIs: |
| 10 | + |
| 11 | +* ``pkg_resources.iter_entry_points()`` |
| 12 | +* ``pkg_resources.require()`` |
| 13 | +* convenience functions |
| 14 | +* ``pkg_resources.find_distributions()`` |
| 15 | +* ``pkg_resources.get_distribution()`` |
| 16 | + |
| 17 | +Other functionality from ``pkg_resources`` is replaced by other |
| 18 | +packages such as |
| 19 | +`importlib_resources <https://pypi.org/project/importlib_resources>`_ |
| 20 | +and `packaging <https://pypi.org/project/packaging>`_. |
| 21 | + |
| 22 | + |
| 23 | +pkg_resources.iter_entry_points() |
| 24 | +================================= |
| 25 | + |
| 26 | +``importlib_metadata`` provides :ref:`entry-points`. |
| 27 | + |
| 28 | +Compatibility note: entry points provided by importlib_metadata |
| 29 | +do not have the following implicit behaviors found in those |
| 30 | +from ``pkg_resources``: |
| 31 | + |
| 32 | +- Each EntryPoint is not automatically validated to match. To |
| 33 | + ensure each one is validated, invoke any property on the |
| 34 | + object (e.g. ``ep.name``). |
| 35 | + |
| 36 | +- When invoking ``EntryPoint.load()``, no checks are performed |
| 37 | + to ensure the declared extras are installed. If this behavior |
| 38 | + is desired/required, it is left to the user to perform the |
| 39 | + check and install any dependencies. See |
| 40 | + `importlib_metadata#368 <https://github.com/python/importlib_metadata/issues/368>`_ |
| 41 | + for more details. |
| 42 | + |
| 43 | +pkg_resources.require() |
| 44 | +======================= |
| 45 | + |
| 46 | +``importlib_metadata`` does not provide support for dynamically |
| 47 | +discovering or requiring distributions nor does it provide any |
| 48 | +support for managing the "working set". Furthermore, |
| 49 | +``importlib_metadata`` assumes that only one version of a given |
| 50 | +distribution is discoverable at any time (no support for multi-version |
| 51 | +installs). Any projects that require the above behavior needs to |
| 52 | +provide that behavior independently. |
| 53 | + |
| 54 | +``importlib_metadata`` does aim to resolve metadata concerns late |
| 55 | +such that any dynamic changes to package availability should be |
| 56 | +reflected immediately. |
| 57 | + |
| 58 | +Convenience functions |
| 59 | +===================== |
| 60 | + |
| 61 | +In addition to the support for direct access to ``Distribution`` |
| 62 | +objects (below), ``importlib_metadata`` presents some top-level |
| 63 | +functions for easy access to the most common metadata: |
| 64 | + |
| 65 | +- :ref:`metadata` queries the metadata fields from the distribution. |
| 66 | +- :ref:`version` provides quick access to the distribution version. |
| 67 | +- :ref:`requirements` presents the requirements of the distribution. |
| 68 | +- :ref:`files` provides file-like access to the data blobs backing |
| 69 | + the metadata. |
| 70 | + |
| 71 | +pkg_resources.find_distributions() |
| 72 | +================================== |
| 73 | + |
| 74 | +``importlib_metadata`` provides functionality |
| 75 | +similar to ``find_distributions()``. Both ``distributions(...)`` and |
| 76 | +``Distribution.discover(...)`` return an iterable of :ref:`distributions` |
| 77 | +matching the indicated parameters. |
| 78 | + |
| 79 | +pkg_resources.get_distribution() |
| 80 | +================================= |
| 81 | + |
| 82 | +Similar to ``distributions``, the ``distribution()`` function provides |
| 83 | +access to a single distribution by name. |
| 84 | + |
0 commit comments