Take Django for instance, in manage.py:
try:
import settings
except ImportError:
sys.stderr.write("Error: Can't find the file 'settings.py'...")
Seems legit, but what happens when settings imports non_existant_lib_foo?
Well, you're sent on a goose chase for all the possible things you could have done with PATH, etc.
Of course you can use except ImportError as e: and just print out the actual error message, but what if you want to catch only a specific error and give a really good bit of advice, like above?
You're left to using regexp more or less or, at best, guessing that the "right" module failed importing, and then display the message.
Is there a better way to handle this?
Basically the problem is that your error message is wrong - there are other reasons why import can fail, not just wrong paths. ImportError simply means "you cannot use this module, look at the traceback to find out why", you just jumped to a conclusion.
If you want to display "Can't find the file" then you should look for the file first. imp.find_module does that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With