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

James C. Ahlstrom jim@interet.com
Mon, 29 Oct 2001 10:34:30 -0500


Gordon McMillan wrote:
>
> > Currently, sys.path is a list of directory names as strings.
> 
> A nit: that's in practice, but not by definition (a couple std
> modules have been patched to allow other things on
> sys.path). After extensive use of imputil (which puts objects
> on sys.path), I think we might as well make it official that
> sys.path is a list of strings.

A nit on a nit: Non-strings can still be allowed,
because I can ignore them.

> [Subdirectory Equivalence]
> 
> This is a bit misleading - it can be read as implying that the
> importable modules are in a flat namespace. They're not. To
> get R.Q.modfoo imported, R.__init__ and R.Q.__init__ must
> be imported. The __init__ modules have the opportunity to
> play with __path__ to break the equivalence of R.Q.modfoo to
> R/Q/modfoo.py. Or (more likely), play games with attributes
> so that Q is, say, an instance, or maybe a module imported
> from someplace else.

I feel that this is an excellent point, but I don't understand
package import well enough to comment.
 
> Question: if Archive.zip/Q/__init__.pyc does
> "__path__.append('some/real/directory')", will it work?

My guess (and it really is a guess) is Yes.  The import.c code
which generates directory names is untouched.  My code simply
looks for ".zip" in the generated name and branches into the
zip archive at that point.  So if the directory version works,
the zip archive version should work too.
 
> [no *.py files]
> > You also can't import source files *.py from a zip archive.
> 
> Apparently some Linuxes / RPM distributions don't deliver
> .pyc's or .pyo's. Since they're installed as root and run as
> some-poor-user, I'm afraid there are quite a few installations
> running off .py's all the time. So while it's definitely sub-
> optimal, I'm not sure it should be outlawed.

For Linux/RPM's I think shipping a library directory is better
than a zip archive.  It is easier to hack on a directory.  I
think of zip archives as a way to distribute packages, and
as a replacement for freeze.

OTOH, maybe we should allow *.py to satisfy imports even if
it is slow and invisible.

> [Efficiency]
> > The key is the archive name from sys.path joined with the
> > file name (including any subdirectories) within the archive.
> 
> DIfferent spellings of the same path are possible in a
> filesystem, but not in a dictionary. A bit of "harmless"
> tweaking of sys.path could render an archive unreachable.

True, but I have little sympathy for case-insensitive file
systems.  Tweaking of sys.path will have to be done with care.
It helps that the '/' character is always used in zip, and
both backslash and colon ":" are illegal in zip archive names.
 
JimA