[Python-Dev] Fix import errors to have data

Tim Peters tim.peters at gmail.com
Wed Jul 28 04:40:45 CEST 2004

[Delaney, Timothy C (Timothy)]
> OK - the problem as I see it is that a given module that exists, but
> raises ImportError, only raises ImportError *once*, whereas it really
> should raise ImportError every time

Jim's after something different, while the problem you're thinking
about is more general than just ImportError.  When an import of an
existing module fails for *any* reason, subsequent attempts to import
the same module succeed.  For example,

C:\Code>type a.py

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "a.py", line 1, in ?
ZeroDivisionError: integer division or modulo by zero
>>> import a

This is Bad, because an uncaught exception in module initialization
means the module probably can't fulfill its contract, yet subsequent
importers get no clue that the module is in a damaged state (until the
module fails to do its job, which may or may not be obvious when it
occurs).  A module failing to import because it suffers an ImportError
itself is once instance of this, but the same applies to a module
failing to import for any other reason:  in all these cases,
subsequent imports don't get the exception the initial importer saw,
they get a module object in an arbitrarily screwed-up state.

More information about the Python-Dev mailing list