[Python-Dev] imputil and modulefinder replacements

Thomas Heller thomas.heller@ion-tof.com
Fri, 5 Oct 2001 18:01:41 +0200


From: "James C. Ahlstrom" <jim@interet.com>
> Thomas Heller wrote:
> > 
> > From: "Gordon McMillan" <gmcm@hypernet.com>
> > > I would like to propose these (or something quite like them) as
> > > replacements for the official versions. The code is quite similar
> > > (in fact, the modulefinder code could have been written by
> > > subclassing the imputil stuff, but I wrote them the other way
> > > 'round).
> > >
> > It seems noone cares about this. imputil is in the distribution,
> > but is it really 'official'?
> 
> I care about it.  I am using imputil to implement "jar" files,
> zip files of Python libraries.  Greg worked hard to get imputil
> into the distribution, and I appreciate his effort.  But I always
> wondered if imputil was 'official'.
> 
> Users on c.l.p frequently want to ship a stand-alone
> Python.  The current answer to this is freeze, but this requires a
> C compiler and some work.  A standard jar file implementation would
> satisfy this need.  What I am thinking of is documentation which
> says something like: "Zip up the Python libraries into pylibs.zip
> and put that file in the same directory as python[.exe] and
> everything Just Works."
Do you know about py2exe and installer? This is a variation of what they do.
py2exe basically builds a zip-file containing the libary modules,
appends them to a special python.exe. This special exe 'boots' itself
by using imputil, and exposes a 'get_code' function into the __main__
namespace. This function is called by a module name contained in the
(embedded) zip, and returns the stored bytes. The installed imputil
code then creates a module from these bytes.
Gordon's installer works very similar, except that he uses a custom
archive format (IIRC).

> 
> My interest in imputil and ImportManager is to implement jar files,
> and neither does this well.  The problem is always how to get the
> SpecialImporter.py imported first, and how to hack the imports that
> the SpecialImporter.py may need to import.  In iu4.py we see the use
> of posix.stat to replace the os module, and the import of only
> built-in modules sys, imp, and marshal.  The same is true of imputil.py.
> 
> I admit I don't really understand package imports.  But I feel they
> are an unsolved problem, because package authors always seem to
> write custom ones.
> 
> A first-class solution would have these features IMHO:
> 1) Access to libraries in zip files is implemented in C (I volunteer).
You could take the code from py2exe...
> 2) All Python library modules can be imported by C code,
>    either from directories or zip files.
> 3) Package import is not implemented in C, it is implemented
>    by a Python module which is part of the standard library.
This could be imputil, or the new iu4.
> 4) If an import of A.B is attempted, and a function
>    A.MyCustomImporter() exists, then that is used instead of
>    the default.
> 5) More or better ideas from you all.

BTW: I remember I having seen a ZipFileImporter using imputil, but
cannot find it any more. Am I dreaming?

Thomas