[Python-ideas] class ModuleNotFoundError(ImportError)

cool-RR cool-rr at cool-rr.com
Mon Feb 28 22:21:52 CET 2011


There are many programs out there, including Django, that "carefully import"
a module by doing:

    try:
        import simplejson
    except ImportError:
        import whatever_instead as simplejson
        # or whatever

This is problematic because sometimes an `ImportError` is raised not because
the module is missing, but because there's some error in the module, or
because the module raises an `ImportError` itself. Then the exception gets
totally swallowed, resulting in delightful debugging sessions.

What do you think about having an exception `ModuleNotFoundError` which is a
subclass of `ImportError`? Then people could `except ModuleNotFoundError`
and be sure that it was caused by a module not existing. This will be a much
better way of "carefully importing" a module. Would this be
backwards-compatible?


Ram.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110228/260ef987/attachment.html>


More information about the Python-ideas mailing list