[Python-Dev] Fix import errors to have data

Jim Fulton jim at zope.com
Tue Jul 27 17:36:00 CEST 2004

Guido van Rossum wrote:
>>A common idiom to optionally support some module if it is
>>present is to use ImportError handlers:
>>   try:
>>       import foo
>>   except ImportError:
>>       # Configure for absense of foo
>>       ...
>>   else:
>>       # Configure for presense of foo
>>       ...
>>Unfortunately, this is a bug trap.  The module foo
>>might be present and yet it's import could fail with an import error.
>>This can happen if one of *its* imports fails.  Code like that
>>above will hide such bugs.
> Why is it important to catch the bug at this point? 

Because the masked import failure could leave modules in a
a broken state that causes very hard to diagnose problems

 > If the module
> imported by foo has a bug, importing it directly will reveal it. 

No, it won't.  For example, suppose foo imports B. B tries to import
C, but fails.  B is now broken, but it is still importable.  Actually,
both foo and B can be imported without errors, even though they are

 > The
> code importing foo will work without foo.  In some very real sense,
> foo is unusable, and its importer is making the right choice.

The intent is to ignore the absense of foo, not the brokenness
of foo.  If foo is around, but broken, I want to know about it.

As a bonus, if I ignore the error in importing foo, I may leave a
broken, but importable foo around.  If any other code tries
to conditionally import foo, it will think that foo is imported
and usable.


> If you provide a working patch, I have no objection against its
> introduction.  I'm just not going out of my way for it.

Cool.  I expected to do this myself anyway.


Jim Fulton           mailto:jim at zope.com       Python Powered!
CTO                  (540) 361-1714            http://www.python.org
Zope Corporation     http://www.zope.com       http://www.zope.org

More information about the Python-Dev mailing list