Skip to content

Commit c8d32d9

Browse files
committed
Extend hbutils section & put before pkg_resources
1 parent 0cfb987 commit c8d32d9

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

source/guides/handling-missing-extras-at-runtime.rst

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,37 @@ because another installed package depends on it with a wider version
4545
requirement than specified by your extra).
4646

4747

48+
Using ``importlib.metadata`` and ``packaging``
49+
----------------------------------------------
50+
51+
As a safer alternative that does check whether the optional dependencies are
52+
installed at the correct versions, :py:mod:`importlib.metadata` and
53+
:ref:`packaging` can be used to iterate through the extra's requirements
54+
recursively and check whether all are installed in the current environment.
55+
56+
This process is currently quite involved. An implementation can be found in
57+
`packaging-problems #664 <packaging-problems-664_>`_, which is also made
58+
available in the `hbutils <https://pypi.org/project/hbutils/>`_ package as
59+
``hbutils.system.check_reqs``.
60+
The possibility of offering a similar helper function in ``importlib.metadata``
61+
or ``packaging`` themselves is still being discussed
62+
(`packaging-problems #317 <packaging-problems-317_>`_).
63+
64+
With ``check_reqs`` included in your codebase or imported from ``hbutils``,
65+
usage is as simple as:
66+
67+
.. code-block:: python
68+
69+
extra_installed = check_reqs(["your-package[your-extra]"])
70+
71+
In contrast to the method above, this is typically done in :term:`LBYL` style
72+
prior to importing the modules in question.
73+
In principle, it could also be done after the imports succeeded just to check
74+
the version, in which case the imports themselves would have to be wrapped in a
75+
``try``-``except`` block to handle the possibility of not being installed at
76+
all.
77+
78+
4879
Using ``pkg_resources`` (deprecated)
4980
------------------------------------
5081

@@ -53,12 +84,13 @@ Using ``pkg_resources`` (deprecated)
5384
``pkg_resources`` is **deprecated** and the PyPA **strongly discourages**
5485
its use.
5586
This method is included in this guide for completeness's sake and only until
56-
functionality with a similar level of comfort exists in
87+
functionality with a similar level of convenience exists in
5788
``importlib.metadata`` or ``packaging``.
5889

5990
The now-deprecated `pkg_resources <pkg_resources_>`_ package (part of the
60-
``setuptools`` distribution) provides a ``require`` function that you can use
61-
to check if a given optional dependency of your package is installed or not:
91+
``setuptools`` distribution) provides a ``require`` function, which was the
92+
inspiration for ``check_reqs`` from the previous section. Its usage is quite
93+
similar to ``check_reqs`` but not identical:
6294

6395
.. code-block:: python
6496
@@ -71,21 +103,6 @@ to check if a given optional dependency of your package is installed or not:
71103
except VersionConflict:
72104
... # handle version mismatches
73105
74-
Unfortunately, no drop-in replacement for this functionality exists in
75-
``pkg_resources``'s "official" successor packages yet
76-
(`packaging-problems #317 <packaging-problems-317_>`_).
77-
78-
79-
Using 3rd-party libraries
80-
-------------------------
81-
82-
In response to the aforementioned lack of a replacement for
83-
``pkg_resources.require``, at least one 3rd party implementation of this
84-
functionality using only the ``packaging`` and ``importlib.metadata`` modules
85-
has been created (`packaging-problems #664 <packaging-problems-664_>`_) and
86-
made available in the 3rd-party `hbutils <https://pypi.org/project/hbutils/>`_
87-
package as ``hbutils.system.check_reqs``.
88-
89106
90107
Handling missing extras
91108
=======================

0 commit comments

Comments
 (0)