[Python-Dev] PEP 273: Import Modules from Zip Archives

James C. Ahlstrom jim@interet.com
Mon, 29 Oct 2001 09:35:07 -0500


Finn Bock wrote:

> Why can't .py be allowed? If a more recent .py[co] (or $py.class)
> exists, it is used. Otherwise the .py file is compiled and discarded
> when the process ends. Sure, it is slower, but a zip files with only
> .py[co] entries would be of little use with jython.

The *.py are allowed to be in the file, and jpython can use them.  In
fact, any files at all can be in the archive.

It is just that C-Python ignores them.  My reason was that if *.py[co] are
missing or out of date, zip importing will be slow and users won't figure
out what is wrong.  But I am open to changing this.

> >    Just as sys.path currently has default directory names, default
> A standard for this would be really cool.

Yes, lets make a standard now.

> So python packages and modules can exists *only* at the top level? That
> would conflict somewhat with jython where at would be common to put
> python modules into the same .zip file as java classes and java classes
> also wants to own the root of a zip file.
> 
> In the current implementaion in jython, we can put python modules under
> any path and add the zipfile!path to sys.path:
> 
>    sys.path.append("/path/to/zipfile.zip!Lib")
> 
> which will look for Lib/Q/R/modfoo.py (and Lib/Q/R/modfoo$py.class) in
> the archive.

I am confused.  Zip archives are equivalant to subdirectories, so there
is no requirement to have anything at the top level.  Your example seems
to imply a second search path besides sys.path.

BTW, the code uses ".zip" as the archive flag,
not a special character '!'.
 
> [I found efficiency hard to achieve in jython because opening a zipfile
> in java also cause the zip index to be read into a dictionary. So we
> did not want to reopen a zipfile if it can be avoided. Instead we hide a
> reference to the opened file in sys.path so that when removing a zipfile
> name from sys.path, the file is eventually closed.]
> 
> Would entries in the static python dict be removed when a zipfile is
> removed from sys.path?

No, entries would not be removed.  But they would not be found either,
because their names would not be generated from the new sys.path.

> What is the __path__ vrbl set to in a module imported from a zipfile?
> Can the module make changes to __path__ and will be changes to used when
> importing submodules?
> 
> What value should __file__ have?

The __file__ is /A/B/archive.zip/name.py.  There is no special code
for __file__ nor __path__, the path name just has a ".zip" in it.

JimA