@@ -45,6 +45,37 @@ because another installed package depends on it with a wider version
45
45
requirement than specified by your extra).
46
46
47
47
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
+
48
79
Using ``pkg_resources `` (deprecated)
49
80
------------------------------------
50
81
@@ -53,12 +84,13 @@ Using ``pkg_resources`` (deprecated)
53
84
``pkg_resources `` is **deprecated ** and the PyPA **strongly discourages **
54
85
its use.
55
86
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
57
88
``importlib.metadata `` or ``packaging ``.
58
89
59
90
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:
62
94
63
95
.. code-block :: python
64
96
@@ -71,21 +103,6 @@ to check if a given optional dependency of your package is installed or not:
71
103
except VersionConflict:
72
104
... # handle version mismatches
73
105
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
-
89
106
90
107
Handling missing extras
91
108
=======================
0 commit comments