Note that there are some explanatory texts on larger screens.

plurals
  1. POpython: How to check extra requirements at runtime?
    primarykey
    data
    text
    <p>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 <code>requires.txt</code> file there. At runtime, we would like to let people know, if the feature will break. I am currently considering something like this:</p> <pre><code>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 </code></pre> <p>Is this the right, pythonic way of doing things? Does this make sense?</p> <p>Small update: Why so complex and not just <code>try: import ... except ImportError: ...</code> like suggested in one answer:</p> <ol> <li>Our plugins might have a bunch of dependencies. Creating actual code like the one below is quite verbose.</li> <li>Some plugins might need a specific version of a package. Testing that requires either a pakcage specific test or using <code>pkg_resources</code> anyway. So that's why my idea above uses pkg_resources.</li> <li>We want to run unit tests for plugins that can be run. Handling the ImportError in the unit tests is not nice. Having a <code>can_we_unit_test_this_plugin(plugin)</code> function makes things easier.</li> </ol> <p>Second update: What about <code>extra_require</code> in <code>setup.py</code>?</p> <ol> <li>people miss to install those often. Okay, bad excuse.</li> <li>My vision is, that <code>setup.py</code> loads the <code>extra_require</code> straight from the above mentioned <code>requires.txt</code> in the individual subdirs for the individual features. But that's really the next step.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload