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

Alex Martelli aleax at
Fri Nov 4 04:14:33 CET 2005

Noah <noah at> 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.

(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.


More information about the Python-list mailing list