Importing XML modules in py2exe or Installer?

Gordon McMillan gmcm at hypernet.com
Wed Jul 18 09:18:35 EDT 2001


"Gregory P. Smith" wrote:

> I hack that I don't really recommend, but that does work: rename the
> _xmlplus subdirectory from the pyxml installation to xml.  (move any
> old tiny xml directory that python came with out of the way).  We've
> been doing this to package mojo nation successfully with both gordons
> and py2exe.  [we did that for other historical reasons unrelated to
> packaging but it worked for packaging as a side effect]

I looked into this and discovered a problem with imputil (which
both Thomas & I rely on). Your hack neatly avoids the problem.

When you have _xmlplus installed, importing xml causes code in
xml/__init__.py to run which imports _xmlplus, then swaps itself
in sys.modules with _xmlplus.

Under the regular import mechanism, an import of, say, xml.sax 
will cause _xmlplus.sax to be imported, but it will show up as
xml.sax in sys.modules, and it will have the __name__ "xml.sax".

Under imputil, _xmlplus.sax will be imported (and become an
attribute of xml), but it will show in sys.modules as
_xmlplus.sax (and have a __name__ of _xmlplus.sax).

Normally, this is not a problem. Once imported, a reference
to xml.sax will be resolved by looking for the name "sax"
within xml, and this succeeds. But there are quite a few
places in _xmlplus where it searches sys.modules, and these
cases fail.

I've posted a patch to the import SIG for comment. If no one
complains, I'll send it on to Pythonlabs.

- Gordon



More information about the Python-list mailing list