Importing XML modules in py2exe or Installer?

Gordon McMillan gmcm at hypernet.com
Sun Jul 15 10:11:50 EDT 2001


[posted and mailed]

Gustaf Liljegren wrote: 

> Gordon McMillan <gmcm at hypernet.com> wrote:
> 
>>So put the line
>>packages=_xmlplus 
>>in the APPZLIB section.
> 
> Okay, now I used Standalone first. Got a .cfg file and changed it
> according to the above. Then I ran in again with Builder. Is this
> really the normal operation? I had expected to first make a config file
> and then make an exe, or that you would get an opportunity to save the
> modified config file and run Standalone on it again. But I guess this
> is a special case, and the second run is normally not needed.

All Standalone, Simple and Freeze do is create config files for you, 
and then run Builder on them. If the code does not contain import
hacks, that's all that's needs to be done.

There are 2 general approaches to getting around import hacks (which,
for some reason, many package authors seem to feel compelled to use, 
even when simpler solutions are possible). Both require figuring out
what the hack does.

The easiest is to do the import yourself in plain Python (using straight 
"import"s) before the hack gets executed. When the hack executes, it will 
find the module(s) already in sys.modules and complete successfully.

The second is to use the config file. Depending on the hack, that may
not always work. The config file is very handy for trimming the size
of your executable, though. By using Analyze, you can strip out all
unneeded modules.

 
> When I run Builder, I get the following two warnings (among many others
> that seem non-significant):
> 
> W: __import__ hack detected at line 54 of xml.dom.domreg
> W: __import__ hack detected at line 100 of xml.sax
> 
> Does this mean that the import of the XML modules wasn't successful?

No, it means you'd better look at that code and see what the author
is doing with his import hacks.
 
> My script needs a special config file, written in XML, in the same 
> directory as the script itself. I was happy to see that before I had
> added the XML file to this directory, jane.exe complained in the right
> way: 
> 
> E:\python\gustaf\dist_jane>jane
> No profiles file found.
> 
> However, when I add the XML file to the directory, it fails:
> 
> E:\python\gustaf\dist_jane>jane

[snip]

>   File "e:\python\_xmlplus\sax\saxexts.py", line 75, in make_parser
>     sys.modules[parser_name].create_parser = _create_parser
> KeyError: xml.sax.drivers2.drv_pyexpat

I'd guess one of those __import__s is responsible for importing
xml.sax.drivers2.drv_pyexpat. If you do that yourself (before this
code executes), you'll at least get past the KeyError.
 
<editorial>It's not a good sign for Python's maturity that
developers ignore (subvert!)[1] the distribution of packaged 
apps. Assuming that end users have a Python installation (of
the correct version) is just silly. Python is not just for
Python developers.</editorial>

[1] One notable exception being Greg MacFarlane, who has included
a bundlepmw script with his Pmw package for years.

- Gordon



More information about the Python-list mailing list