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

Guido van Rossum guido@python.org
Fri, 26 Oct 2001 22:23:30 -0400


> >    Just as sys.path currently has default directory names, default
> >    zip archive names are added too.  Otherwise there is no way to
> >    import all Python library files from an archive.
> 
> A standard for this would be really cool.

Yup.

> >    Suppose import.c generates the path
> >    "/A/B/SubDir/Q/R/modfoo.pyc".  Then it will also generate the path
> >    "/C/D/E/Archive.zip/Q/R/modfoo.pyc".  Finding the SubDir path is
> >    exactly equivalent to finding "Q/R/modfoo.pyc" in the archive.
> 
> 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.

Maybe it's possible to allow "/path/to/zipfile.zip/subdirectory/etc/"
in sys.path?  That sounds better than picking a random new character
as delimiter.

> [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.]

I don't think Python has this problem, since we have control over the
zipfile reading code.

> Would entries in the static python dict be removed when a zipfile is
> removed from sys.path? 

It can be arranged that they are removed at some later point
(e.g. when sys.path is next searched).

> 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?

IMO these two questions are answered by the pathname semantics
that Jim proposes: __file__ = "/C/D/E/Archive.zip/Q/R/modfoo.pyc" and
__path__ = ["/C/D/E/Archive.zip/Q/R"].

--Guido van Rossum (home page: http://www.python.org/~guido/)