[Python-ideas] class ModuleNotFoundError(ImportError)
Michael Foord
fuzzyman at gmail.com
Mon Feb 28 22:45:55 CET 2011
On 28 February 2011 21:28, Guido van Rossum <guido at python.org> wrote:
> On Mon, Feb 28, 2011 at 1:21 PM, cool-RR <cool-rr at cool-rr.com> wrote:
> > 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?
>
> The most problematic issue is actually that the imported module
> (above, simplejson) itself imports a non-existent module. Since that
> would still raise ModuleNotFoundError, your proposal doesn't really
> fix the problem.
>
> I think modules raising ImportError for some other reason is rare.
>
> What might perhaps help is if ImportError had the name of the module
> that could not be imported as an attribute. Then the code could be
> rewritten as:
>
> try:
> import simplejson
> except ImportError, err:
> if e.module_name != 'simplejson':
> raise
> <backup plan>
>
>
+1
Michael
> --
> --Guido van Rossum (python.org/~guido)
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
--
http://www.voidspace.org.uk/
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110228/8ea22d5b/attachment.html>
More information about the Python-ideas
mailing list