Unexpected import behavior

Chuck Esterbrook ChuckEsterbrook at yahoo.com
Fri Jun 22 20:20:27 CEST 2001


Under the old mxDateTime, I had:

import DateTime
from DateTime import now, RelativeDateTime


The new one is inside a package. No problem, I thought:

from mx import DateTime
from DateTime import now, RelativeDateTime


But that gives me this:

Traceback (most recent call last):
   File "./DailyLinks21.py", line 24, in ?
     from DateTime import now, RelativeDateTime
ImportError: No module named DateTime


Obviously there is a DateTime variable by the time we get to the second 
statement and it points to a module. I printed the repr() and type() to 
make sure.

An examination of sys.modules.keys() shows, however, that there is no 
'DateTime', but only 'mx' and 'mx.DateTime'.

Using a Python package containing a package containing a module, I created 
the same problem. I then created it with Python 2.0 and 1.5.2.

The behavior seems rather non-intuitive (although easily fixed in this 
case). I guess the explanation from those familiar with Python internals 
will be something along these lines:
   - import uses sys.modules.keys() not the symbols where it is located
   - "from pkg import module" does not "unqualify" the module for future 
imports even in the module where this statement appears

So at this point, I'm wondering if you think this behavior is 
intuitive/"correct" and whether or not it would make any sense to change 
it. For example, could import look at local symbols before sys.modules?

-Chuck





More information about the Python-list mailing list