Generally, you just try to import the dependency, and handle the ImportError
exception gracefully:
try:
import dependency
except ImportError:
# dependency missing, issue a warning
import warnings
warnings.warn('dependency not found, please install to enable xyz feature')
You can list such dependencies in the extras_require
entry of your setuptools-based setup.py
script. pip
, easy_install
and zc.buildout
all can handle installing such extras. See Declaring “Extras” (optional features with their own dependencies).
You can use the extras_require
entry to list minimal version requirements if you have those. Yes, there is a possibility that the user already has an older version of the dependency installed; I'd just document the requirements clearly. Really, test for features, not versions. If you need a newer version because a certain API method has been added? Test for that method rather than for the version.
However, it sounds as if you may want to package the plugins as separate packages instead, then list those in extras_require
. I'd use entry points to register and enumerate such plugins. That way you do not need to test for imports or for packages, you just enumerate over registered entry points instead. Each plugin lists their own dependencies and has it's own unit tests.