Distinguish between ImportError because of not found module or faulty import in module itself in python? -


i have few modules in python, imported dynamicly , have same structur (plugin.py, models.py, tests.py, ...). in managing code want import submodules, example models.py or tests.py not mandatory. (so have plugin_a.plugin , plugin_a.tests plugin_b.plugin).

i can check if submodule exists by

try:     __import__(module_name + ".tests") except importerror:     pass 

that fail, if module_name+".tests" not found, fail if tests-module try import something, not found, example because of typo. there way check if module exists, without importing or make sure, importerror raised 1 specific import-action?

you know import error message if module doesn't exist check that:

try:     module = module_name + '.tests'     __import__(module) except importerror, e:     if e.args , e.args[0] == 'no module named ' + module:         print(module, 'does not exist')     else:         print(module, 'failed import') 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -