Skip to content

Instantly share code, notes, and snippets.

@romuald
Created September 26, 2013 17:35
Show Gist options
  • Save romuald/6717687 to your computer and use it in GitHub Desktop.
Save romuald/6717687 to your computer and use it in GitHub Desktop.
Try to gracefully handle a missing module, but raise if that module itself tries to import something that isn't available
try:
module = __import__(name, **specs)
except ImportError:
# If tracback "length" is greater than 1, there is most
# probably a deeper ImportError that we don't want to
# mask (example: missing library used by the base import)
_, _, tb = sys.exc_info()
if len(traceback.extract_tb(tb)) > 1:
raise
# else, handle the error graciously
print('WARN: module %s is missing' % name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment