Import conundrum

Steve Holden sholden at holdenweb.com
Tue Apr 23 07:52:38 EDT 2002


"Cliff" <pythonista at uk2.net> wrote ...
> If I put the following Python 2.1 script in a file called 'xml.py':
>
>    from xml.dom.minidom import parseString
>    dom = parseString('<?xml version="1.0" ?><test/>')
>    print dom.writexml(sys.stdout)
>
> and then try to run it on Windows NT I get the following:
>
>    Traceback (most recent call last):
>      File "C:\home\temp\xml.py", line 5, in ?
>        from xml.dom.minidom import parseString
>      File "C:\home\temp\xml.py", line 5, in ?
>        from xml.dom.minidom import parseString
>    ImportError: No module named dom.minidom
>
Its all to do with the import mechanism, and your confusion is
understandable when the documentation says """Details of the module
searching and loading process are implementation and platform specific. It
generally involves searching for a ``built-in'' module with the given name
and then searching a list of locations given as sys.path."""

> If I rename the file to something else, then everything is as expected.
> What I am interested to know is whether this is 'correct' behaviour, or am
> I doing something really stupid?
>
Well, it is quite normal for the import statement to look in the current
directory. If there's a file called xml.py, how is the interprpeter to know
that you don't want to use it as the basis of your import? I suspect you're
going to say "because xml is a module, not a package". Makes sense to me.

> I ask, because I would personally expect the Python interpreter to know
the
> difference between the file called 'xml.py' and the library path 'xml.dom'
> (which I would expect to be mapped to .\xml\dom on Windows).
>
When you get into the differences between modules and packages, which the
documentation is even more explicitly murky on: """The sequence of
identifiers up to the last dot is used to find a ``package'' ; the final
identifier is then searched inside the package. A package is generally a
subdirectory of a directory on sys.path that has a file __init__.py. [XXX
Can't be bothered to spell this out right now; see the URL
http://www.python.org/doc/essays/packages.html for more details, also about
how the module search works from inside a package.] """

> And there I was thinking I understood this Python stuff... ;-)
>
In this particular case you can be forgiven for not understanding it -- or,
at least, I have forgiven myself <wink> and feel you should do the same. It
doesn't help that the essay referenced in the previous paragraph is
currently offline, but that's just a net glitch.

If you've ever read any of the code that messes with import mechanisms, it's
funky in the extreme. The only reason we can't say whether it's broken is
because it's impossible to tell what "broken" means!

saying-'ni'-doesn't-always-get-you-there-ly y'rs  - steve
--

home: http://www.holdenweb.com/    book: http://pydish.holdenweb.com/pwp/








More information about the Python-list mailing list