Import from zip file?

Bengt Richter bokr at oz.net
Mon Jan 20 12:51:02 EST 2003


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?
I.e., what is the role of the file name ('.' name)* after the file data
has been found and loaded? It seems like it's mainly to be recorded
in sys.modules to prevent re-loading, so that sharing results.

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

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

where foo could go in sys.modules and baz could be the local binding.
baz.__name__ would be foo, and
    sys.modules['foo'] is baz => True.

Seems like any byte stream ought to be able to be made to work too, even

    import spam from sys.stdin

with no local as-alias, it would be spam, and spam.__name__ would be spam,
resulting in sys.modules['spam'] is spam => True.

and it could recognize a string after the from and open that as a normal file
path spec without dotted notation, though you might even be able to introduce
that specifically too, e.g.,

    import hum.haw from '~/stuff/ho/hawfile' as foo
and get
    sys.modules['hum.haw'] is foo => True.
(but leaving out sys.modules['hum'] I suppose).

Regards,
Bengt Richter




More information about the Python-list mailing list