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 bimport 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?