try except ImportError failing to catch error!
Alex Martelli
aleax at aleax.it
Wed Sep 18 09:57:08 EDT 2002
Peter Hansen wrote:
...
> catch it. If you want to catch *any* error in the imported
> module, you'll have to do "except Exception, e" instead.
> Unfortunately, then you'll also have ImportErrors in the mix,
> so you won't know if the module could be found but had its
> own error, or if the call to __import__ failed...
That's trivially easy to remedy, thanks to the fact that you
may have multiple except clauses in a try statement, and they're
conceptually tried in order:
try:
themod = __import__(whatever)
except ImportError, blah:
print "Can't import (%s): %s" % (whatever, blah)
except Exception, bluh:
print "Some other problem: %s" % bluh
The only problem is, the ImportError might not be due to
the __import__ call itself -- it COULD be propagating
from some errant import that the module 'whatever' names
is trying to perform! Discriminating between the two
cases does need using the traceback module, as you
suggest for other purposes in another part of this post.
Alex
More information about the Python-list
mailing list