would be nice: import from archive

Anthony Baxter anthonybaxter at gmail.com
Fri Aug 27 13:40:28 EDT 2004


On Fri, 27 Aug 2004 17:15:49 GMT, Dan Perl <dperl at rogers.com> wrote:
> My bad.  Right after posting the message I found the 'Index of Python
> Enhancement Proposals (PEPs)'
> and there is PEP 273, 'Import Modules from Zip Archives', submitted by
> James C. Ahlstrom.  Thank you, Jim!  And there are even two implementations
> already.

Note that the PEP is not up-to-date. zipimport "just works" in Python
2.3 and Python 2.4. In the following example, we use the -v flag to
show where imports are coming from.

bonanza% cat hello.py
def hello():
    print 'hello world'
bonanza% zip hello.zip hello.py 
  adding: hello.py (deflated 8%)
bonanza% rm hello.py
bonanza% python2.4 -v
Python 2.4a2 (#3, Aug 24 2004, 01:25:51) 
[GCC 3.4.0 20040613 (Red Hat Linux 3.4.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
   ... lots and lots of lines showing default imports snipped ...
>>> import hello
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: No module named hello
>>> import sys
>>> sys.path.append('hello.zip')
>>> import hello
# zipimport: found 1 names in hello.zip
dlopen("/usr/local/lib/python2.4/lib-dynload/zlib.so", 2);
import zlib # dynamically loaded from
/usr/local/lib/python2.4/lib-dynload/zlib.so
# zipimport: zlib available
import hello # loaded from Zip hello.zip/hello.py
>>> hello.hello()
hello world

At least on this box (linux) there's even a zip file on the default sys.path!

Anthony



More information about the Python-list mailing list