Monday, 15 September 2014

python - Detecting ImportError in imported module -



python - Detecting ImportError in imported module -

i want import python module if exists, ignore importerror if not. however, if imported module raises importerror, error ignored too, , that's not need (i.e. if module exists, has bug, want know).

for app in installed_apps: try: module = __import__('{}.mycustommodule'.format(app)) # except importerror: traceback = sys.exc_info()[2] if ( ? ): # if exception occurred in app.mycustommodel, raise raise pass # otherwise, ignore

i checked docs sys.exc_info , traceback didn't find useful info. how can done?

if exception occurred in same function catching it, stack trace have 1 frame. otherwise, have multiple frames. after looking @ related questions , making tests, found out it's plenty check if tb_next none:

except importerror: traceback = sys.exc_info()[2] if ( traceback.tb_next ): raise pass

note: although documentation on tb_next scarce (saw reference in docs inspect, nil specifying constitutes traceback "level"), behavior above consistent between python 2.7.3, python 3.2.3, jython 2.5.3, ironpython 2.7.3 , pypy 1.8.0.

python exception-handling stack-trace

No comments:

Post a Comment