[Python-Dev] zipfiles on sys.path

Jack Jansen jack@oratrix.nl
Wed, 25 Jul 2001 23:58:25 +0200

Recently, bckfnn@worldonline.dk (Finn Bock) said:
> Hi,
> We have recently added support for .zip files on sys.path to Jython.
> Now, after the fact, I wondered what prior art exists for such a feature
> and the semantic that is used.

MacPython uses a similar scheme, but slightly different. If there is a
file on sys.path it will be inspected for "PYC " resources with the
module name. (The main use for this feature is that you can put the
application itself in sys.path, compile all your modules into PYC
resources and you have a frozen Python program without having used a C
compiler. A boon on a platform where all C compilers cost money or are

I'll go thru the issues one by one:

> We came up with a solution where:
> - It is the name (as a string) of the zipfile that can be added to
>   sys.path.


> - The zipfile is opened on the next import that checks this sys.path
>   entry  and kept open until all references to the zipfile is gone
>   (including references from packages).

Different, MacPython opens it every time. With the  exception of the
application itself, which is already open (and this is checked).

What MacPython does do, and what speeds up imports immensely, is that
it interns all sys.path strings, and keeps a cache of the sys.path
entries that are known to be files, not directories. This forestalls
the import code testing many non-existing paths for existence
(/path/to/myfile.zip/mod.py, path/o/myfile.zip/mod.pyc, etc).

> - A side effect of the implementation is that the identity of a string
>   on sys.path or __path__ might change during import. The value of the
>   string stay the same.
> - The __path__ vrbl in a package 'foo.bar' loaded from zipfile.zip
>   will have the value ['zipfile.zip!foo/bar'] and this same syntax can
>   also be used when adding entries to sys.path and __path__.

__path__ is set to the package name. I'm not sure of the exact
rationale for this (Just did the package support) but it seems to work
Jack Jansen             | ++++ stop the execution of Mumia Abu-Jamal ++++
Jack.Jansen@oratrix.com | ++++ if you agree copy these lines to your sig ++++
www.oratrix.nl/~jack    | see http://www.xs4all.nl/~tank/spg-l/sigaction.htm