@@ -31,17 +31,33 @@ TODO Optimistic vs pessimistic handling?
31
31
Handling failing imports
32
32
========================
33
33
34
- TODO example
34
+ The perhaps simplest option, which is also in line with the :term: `EAFP `
35
+ principle, is to just import your optional dependency modules as normal and
36
+ handle the relevant exceptions if the import fails:
37
+
38
+ .. code-block :: python
39
+
40
+ try :
41
+ import your_optional_dependency
42
+ except ModuleNotFoundError :
43
+ ... # handle missing dependency
44
+
45
+ However, this can lead to difficult-to-debug errors when
46
+ ``your_optional_dependency `` *is * installed, but at the wrong version (e.g.
47
+ because another installed package depends on it with a wider version
48
+ requirement than specified by your extra).
35
49
36
- TODO mention it doesn't check versions, so a bit dangerous
37
50
51
+ Using ``pkg_resources `` (deprecated)
52
+ ====================================
38
53
39
- Using ``pkg_resources ``
40
- =======================
54
+ The now-deprecated :ref: `pkg_resources <ResourceManager API >` package (part of
55
+ the ``setuptools `` distribution) provides a ``require `` function that you can
56
+ use to check if a given optional dependency of your package is installed or
57
+ not:
41
58
42
- The now-deprecated ``pkg_resources `` package (part of the ``setuptools ``
43
- distribution) provides a ``require `` function that you can use to check if a
44
- given optional dependency of your package is installed or not:
59
+ .. :: TODO ask setuptools to add labels for pkg_resources & require, then link
60
+ properly
45
61
46
62
47
63
.. code-block :: python
@@ -55,11 +71,25 @@ given optional dependency of your package is installed or not:
55
71
except VersionConflict:
56
72
... # handle version mismatches
57
73
58
- Unfortunately, no replacement for this functionality exists in
59
- ``pkg_resources ``'s successor packages yet
60
- (`packaging-problems #664 <packaging-problems #664 >`_).
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 ``.
61
88
62
89
63
90
------------------
64
91
92
+ .. _packaging-problems-317 : https://github.com/pypa/packaging-problems/issues/317
93
+
65
94
.. _packaging-problems-664 : https://github.com/pypa/packaging-problems/issues/664
95
+
0 commit comments