Import from zip file?

Paul Moore gustav at morpheus.demon.co.uk
Mon Jan 20 16:32:26 EST 2003


bokr at oz.net (Bengt Richter) writes:

> On 20 Jan 2003 16:35:45 +0100, martin at v.loewis.de (Martin v. =?iso-8859-15?q?L=F6wis?=) wrote:
>
>>k.robert at gmx.de (Robert) 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?

Not very, in some sense, given that Python 2.3 implements import hooks
allowing imports to come from *anywhere* (and specifically, supplies a
built in zip import module).

On the other hand, the changes were in low-level C code, so in some
sense Python 2.2's import does depend at a rather fundamental level on
the filesystem.

Of course, there are also existing import hook mechanisms such as
ihooks, imputil, and Gordon McMillan's iu, all of which work with
Python pre-2.3.

> 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.,

You don't need new syntax. In Python 2.3, you just do
sys.path.append("bar.zip") and away you go.

>     import foo from 'bar.zip' as baz  # note the quotes -- it's a file spec, not dotted module

Where the import is satisfied from is a function of sys.path (or a
couple of other hook-related places in 2.3), not of the import
statement itself.

See PEPs 273 and 302 for more details...

Paul Moore.
-- 
This signature intentionally left blank




More information about the Python-list mailing list