Import from zip file?

Martin v. Löwis martin at v.loewis.de
Mon Jan 20 16:23:03 EST 2003


bokr at oz.net (Bengt Richter) writes:

> >> What is the easyiest way to import modules/module-trees from zip
> >> files (Python 2.2)?
> >
> >Unpack the zip file, then add the directory to sys.path.
> >
> How deep is the dependence of import on the file system?

Unless you redefine __import__, modules *are* files in Python 2.2.

> I.e., what is the role of the file name ('.' name)* after the file data
> has been found and loaded? 

The file name is actually ('/' name)* '.py'; ('.' name)* is the module
name.

> It seems like it's mainly to be recorded
> in sys.modules to prevent re-loading, so that sharing results.

Correct. However, more things rely on __path__ of a package being a
list of directories, e.g. the submodule importing.

> Is there a reason you couldn't have import recognize a zip or tgz file
> as a virtual file directory tree to search and import from there,
> given a little hint via alternate syntax, e.g.,

There is no reason you couldn't do such a thing, and it is actually
possible to do so. However, the OP was asking for the easiest way, and
about Python 2.2. In Python 2.2, it is more difficult to implement
__import__ than to unpack the zip file. In Python 2.3, the easiest way
is to add the zipfile to sys.path.

Regards,
Martin




More information about the Python-list mailing list