Created
September 26, 2013 17:35
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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