# A python question

**URL:** <https://www.ruby-forum.com/t/a-python-question/91325>\
**Category:** Ruby\
**Created:** [April 6, 2007, 7:33pm UTC](https://www.ruby-forum.com/t/a-python-question/91325 "2007-04-06T19:33:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsuraan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@tsuraan](https://www.ruby-forum.com/u/tsuraan)\
**Post date:** [April 6, 2007, 7:33pm UTC](https://www.ruby-forum.com/t/a-python-question/91325/1 "2007-04-06T19:33:21Z")

</div>

So, I’m just going on the hope that the Ruby community, with its great  
number of other-language enthusiasts, might have a member or two who’s a  
Python guru. I asked this question about a week ago on the python list  
and  
got all of zero responses, so I’ll try here.

In python, each directory can be a library if it has a file in it called  
**init**.py. So, you can have the following directory structure:

a/ **init**.py  
/b/ **init**.py  
/c.py

And c.py could have some method in it, like say:

def d():  
return ‘d’

And, from the python command line, you can do

> > > from a.b.c import d  
> > > d()  
> > > ‘d’
> 
> > >

Now, you can also put this structure in a zipfile with the same  
structure,  
and load data from it similarly:

$ unzip -Z a.zip  
Archive: a.zip 1302 bytes 8 files  
drwxr-xr-x 2.3 unx 0 bx stor 1-Apr-07 10:41 a/  
drwxr-xr-x 2.3 unx 0 bx stor 1-Apr-07 10:41 a/b/  
-rw-r–r-- 2.3 unx 22 tx stor 1-Apr-07 10:41 a/b/c.py  
-rw-r–r-- 2.3 unx 0 bx stor 1-Apr-07 10:41 a/b/ **init**.py  
-rw-r–r-- 2.3 unx 95 bx defN 1-Apr-07 10:41 a/b/ **init**.pyc  
-rw-r–r-- 2.3 unx 186 bx defN 1-Apr-07 10:41 a/b/c.pyc  
-rw-r–r-- 2.3 unx 0 bx stor 1-Apr-07 10:41 a/ **init**.py  
-rw-r–r-- 2.3 unx 93 bx defN 1-Apr-07 10:41 a/ **init**.pyc  
8 files, 396 bytes uncompressed, 238 bytes compressed: 39.9%  
$ python  
Python 2.4.3 (#1, Oct 11 2006, 23:25:18)  
[GCC 4.1.1 (Gentoo 4.1.1)] on linux2  
Type “help”, “copyright”, “credits” or “license” for more information.

> > > import sys; sys.path.insert(0, ‘a.zip’)  
> > > import a; a  
> > > \<module ‘a’ from ‘a.zip/a/ **init**.pyc’\>
> 
> > > from a.b.c import d  
> > > d()  
> > > ‘d’

So, basically importing from a zip file looks just like importing from a  
standard directory. Now, what I can’t figure out how to do is import  
from a  
zipfile that’s nested in a directory. My layout is like this:

$ find a  
a  
a/ **init**.py  
a/ **init**.pyc  
a/b.zip  
$ unzip -Z a/b.zip  
Archive: a/b.zip 416 bytes 3 files  
drwxr-xr-x 2.3 unx 0 bx stor 6-Apr-07 12:29 b/  
-rw-r–r-- 2.3 unx 0 bx stor 31-Mar-07 19:58 b/ **init**.py  
-rw-r–r-- 2.3 unx 22 tx stor 6-Apr-07 12:29 b/c.py  
3 files, 22 bytes uncompressed, 22 bytes compressed: 0.0%  
$ python  
Python 2.4.3 (#1, Oct 11 2006, 23:25:18)  
[GCC 4.1.1 (Gentoo 4.1.1)] on linux2  
Type “help”, “copyright”, “credits” or “license” for more information.

> > > import a.b  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in ?  
> > > ImportError: No module named b
> 
> > > import a  
> > > dir(a)  
> > > [‘ **builtins** ’, ‘ **doc** ’, ‘ **file** ’, ‘ **name** ’, ‘ **path** ’, ‘b’,  
> > > ‘here’,  
> > > ‘mod’, ‘os’, ‘zip’, ‘zipimport’]
> 
> > > dir(a.b)  
> > > [‘ **builtins** ’, ‘ **doc** ’, ‘ **file** ’, ‘ **loader** ’, ‘ **name** ’,  
> > > ‘ **path** ’]

Does anyone here know how to import a zipfile library’s contents into a  
normal library? Any Python gurus around?

---

<div class="post-metadata">

**Author:** ![tsuraan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@tsuraan](https://www.ruby-forum.com/u/tsuraan)\
**Post date:** [April 6, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/a-python-question/91325/2 "2007-04-06T20:42:11Z")

</div>

Hello, I happen to enjoy Python and Ruby.

Importing modules from zip files was proposed in PEP-273 [1]

Here is how the spec of PEP-273 begins:

‘’’  
Currently, sys.path is a list of directory names as strings. If this  
PEP is implemented, an item of sys.path can be a string naming a zip  
file archive.  
‘’’

My interpretation of the above is that, to be importable, a zip file  
must be explicitly named in sys.path, so the mere fact that a zip file  
lies somewhere in a directory which is part of the sys.path does not  
make it importable.

Cheers,

Luciano

[1] [PEP 273 – Import Modules from Zip Archives | peps.python.org](http://www.python.org/dev/peps/pep-0273/)

---

<div class="post-metadata">

**Author:** ![tsuraan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@tsuraan](https://www.ruby-forum.com/u/tsuraan)\
**Post date:** [April 6, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/a-python-question/91325/3 "2007-04-06T20:52:14Z")

</div>

> My interpretation of the above is that, to be importable, a zip file  
> must be explicitly named in sys.path, so the mere fact that a zip file  
> lies somewhere in a directory which is part of the sys.path does not  
> make it importable.

I just realized that I left out the tricky part of what I’m doing.  
Within  
a/ **init**.py, I have the following:

import zipimport  
import os  
here = os.path.join(os.getcwd(), **path** [0])  
zip = os.path.join(here, ‘b.zip’)  
mod = os.path.split(zip)[-1][:-4]  
globals()[mod] = zipimport.zipimporter(zip).load\_module(mod)

So, this explicitly uses zipimport to manually load the b.zip module  
into  
a’s namespace. I sort of forgot that was important 🙂 Anyhow, b does  
exist  
within a as a module, but it has no contents, and there doesn’t seem to  
be  
any way to get them, unless anyone knows a cool trick. Thanks for the  
help  
though 🙂
