How can I package a python script and modules into a single script?

Alex Martelli aleax at mail.comcast.net
Fri Nov 4 04:14:33 CET 2005


Noah <noah at noah.org> wrote:

> This is interesting, but requires two separate files -- the ZIP file
> and the boot script.

No, it doesn't.

> This is because zipimport can only import from file paths.

It can import from a file, and the file (like all zipfiles) can have a
prefix.  That prefix is where you put the "boot" script.

See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/215301
(though I believe I did some minor enhancements, as well as adding
useful discussion, in the 2nd edition of the O'Reilly printed version of
the cookbook, Raedler's basic idea is presented here; you can get all
the sources as they appear in the printed version as an archive, a
zipfile I believe, on the O'Reilly site for the book).

> Anders Hammarquist has a Python Cookbook example that seems to do what
> I want.
> "Importing a Dynamically Generated Module". So far so good. I have to
> check it out a bit more.

I know Anders (known to his friends as "Iko") quite well, worked at his
side in Sweden in past years, and I selected his recipe for the first
edition of the O'Reilly printed version of the Cookbook -- but it does
not address the specific problem you desire.  Raedler's recipe does.


Alex



More information about the Python-list mailing list