[Python-Dev] Fix import errors to have data

Tim Peters tim.peters at gmail.com
Tue Jul 27 18:59:35 CEST 2004


[Jim Fulton]
> ...
> No.  I'm proposing a way for a Python developer to detect the
> presence or absence of a module.

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.

>>> import cupcake  # no such thing
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: No module named cupcake
>>> 'cupcake' in sys.modules
False
>>> 

>>> 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
>>>

Not that I like this -- it turns broken modules into "a feature".  A
function that answered the question directly would be mounds better.


More information about the Python-Dev mailing list