__init__.py and import dependencies

Good idea or good programming practice for checking module dependencies in a file __init__.pyfor a module?

So, if I have something in the file __init__.pyas follows:

import imp
modules = set(["numpy", "scipy.spatial.distance"])
for m in modules:
    try:
        imp.find_module(m)
    except ImportError:
        print("Missing dependency: " + str(m))

Is this a good practice or is there a more pythonic way to handle this?

+4
source share
1 answer

It’s good practice to keep the dependencies in which they belong - just put them in __init__.pyif you use them there. Otherwise, any check belongs to the module in which they are used.

- , . - , /, . , ; foo, import foo ImportError, , . , , logging warnings.

0

All Articles