[Python-Dev] Freeze hacks

Martin von Loewis loewis@informatik.hu-berlin.de
Wed, 18 Jul 2001 17:18:07 +0200 (MEST)

A number of modules in the standard library make use of dynamic
imports, or import modules through C code. In either case, no import
statement can be found.

Unfortunately, this means that tools like freeze or py2exe cannot
detect that those modules are used, so the frozen applications will
then fail at runtime. To make this work, I suggest to add explicit
import statements, which are put into a conditional 'if 0:'.

In particular, I found that the following modules need to be
referenced somewhere:
- xml.sax.expatreader, from xml.sax.__init__
- encodings.__init__, probably from codecs
- encodings.*, from encodings.__init__
- dbhash, gdbm, dbm, dumbdbm, from anydbm
- unixccompiler, msvccompiler, cygwinccompiler,
  bcppcompiler, mwerkscompiler, from distutils.ccompiler
- distutils.command.* from distutils.dist

What is the purpose of dumbdbm not importing os directly?

To give a specific example, I'd change xml.sax.__init__ to read

default_parser_list = ["xml.sax.expatreader"]
if 0:
    # freeze hack: the import relationship is not visible without this
    # statement
    import xml.sax.expatreader

Is that a desirable change? If so, I'll produce a patch.

The case of encodings is particularly troubling: I don't think there
is a way to tell freeze/py2exe/installer that

print u"Hallo".encode("iso8859-2")

will require additional modules. As a convention, I'd still recommend
to link all this to codecs, so that an application requiring any
codecs can do

if 0:
    import codecs

explicitly, or just tell the freeze tool to use codecs, and then will
get all codecs that are known statically.