[Python-Dev] Fix import errors to have data

Charles Cazabon python at discworld.dyndns.org
Tue Jul 27 19:20:31 CEST 2004


Tim Peters <tim.peters at gmail.com> wrote:
> 
> Seems to me a module is present if and only if you try to import it,
> and the import succeeds, or the import fails and the module name is in
> sys.modules after.

This is a bit I've never been quite sure of.  If a module knows it imports
something not available everywhere, and can handle that bit missing, shouldn't
it already wrap its import of that module in a try: ... except ImportError:
block?

> >>> import pty  # exists, but is broken on Windows
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "C:\Program Files\Zope-2.7.2-0\bin\Lib\pty.py", line 19, in ?
>     import termios
> ImportError: No module named termios
> >>> 'pty' in sys.modules
> True

To me, it would seem more sensible (and would address the Windows brittleness
Tim mentions) if the above did /not/ leave pty in sys.modules -- i.e. the
import failed either completely, or not at all.  The above is a "partial
failure" which seems (to me) to be the root cause of the confusion.  Instead,
it should fail completely, and then the pty module can (if it wants) try to
import termios and handle it gracefully at that level if necessary.

Charles
-- 
-----------------------------------------------------------------------
Charles Cazabon                           <python at discworld.dyndns.org>
GPL'ed software available at:     http://www.qcc.ca/~charlesc/software/
-----------------------------------------------------------------------


More information about the Python-Dev mailing list