zip files as nested modules?

tsuraan tsuraan at
Sun Apr 1 17:45:54 CEST 2007

Supposing that I have a directory tree like so:


and has some method (let's call it d) within it.  I can, from python, do:

from a.b.c import d

And, that works.  Now, suppose I want to have a zipped module under a,
called  Is there any way that I can accomplish the same thing,
but using the zip file as the inner module?

My directory layout is then


And b is a zipfile laid out like


I tried populating a's __init__ with this:

import zipimport
import os
here = os.path.join(os.getcwd(), __path__[0])
zips = [f for f in os.listdir(here) if f.endswith('.zip')]
zips = [os.path.join(here, z) for z in zips]

for z in zips:
  print z
  mod = os.path.split(z)[-1][:-4]
  print mod
  globals()[mod] = zipimport.zipimporter(z).load_module(mod)

All the zip modules appear (I actually have a few zips, but that
shouldn't be important), but their contents do not seem to be
accessible in any way.  I could probably put import statements in all
the files to import everything in the level below, but I
am under the impression that relative imports are frowned upon, and it
seems pretty bug-prone anyhow.

Any pointers on how to accomplish zip modules being nested within normal ones?

More information about the Python-list mailing list