Our main application has some extra features, that users can enable. Those features are in their own directory. Those features might need extra dependencies. I am considering to put those in a requires.txt
file there. At runtime, we would like to let people know, if the feature will break. I am currently considering something like this:
def checkfeature(feature):
everything_okay = True
f = pkg_resources.resource_stream(feature, "requires.txt")
with f:
for r in pkg_resources.parse_requirements(f):
if pkg_resources.working_set.find(r) is None:
print "%r not found, please install, otherwise this feature does not work" % (r,)
everything_okay = False
return everything_okay
Is this the right, pythonic way of doing things? Does this make sense?
Small update:
Why so complex and not just try: import ... except ImportError: ...
like suggested in one answer:
- Our plugins might have a bunch of dependencies. Creating actual code like the one below is quite verbose.
- Some plugins might need a specific version of a package. Testing that requires either a pakcage specific test or using
pkg_resources
anyway. So that's why my idea above uses pkg_resources. - We want to run unit tests for plugins that can be run. Handling the ImportError in the unit tests is not nice. Having a
can_we_unit_test_this_plugin(plugin)
function makes things easier.
Second update: What about extra_require
in setup.py
?
- people miss to install those often. Okay, bad excuse.
- My vision is, that
setup.py
loads theextra_require
straight from the above mentionedrequires.txt
in the individual subdirs for the individual features. But that's really the next step.