@@ -31,17 +31,33 @@ TODO Optimistic vs pessimistic handling?
3131Handling failing imports
3232========================
3333
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).
3549
36- TODO mention it doesn't check versions, so a bit dangerous
3750
51+ Using ``pkg_resources `` (deprecated)
52+ ====================================
3853
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:
4158
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
4561
4662
4763 .. code-block :: python
@@ -55,11 +71,25 @@ given optional dependency of your package is installed or not:
5571 except VersionConflict:
5672 ... # handle version mismatches
5773
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 ``.
6188
6289
6390------------------
6491
92+ .. _packaging-problems-317 : https://github.com/pypa/packaging-problems/issues/317
93+
6594.. _packaging-problems-664 : https://github.com/pypa/packaging-problems/issues/664
95+
0 commit comments