Zip imports on python 2.2?

Jacob Smullyan smulloni at bracknell.smullyan.org
Sat Jun 14 23:20:43 EDT 2003


In article <GuXFa.127539$g92.2947285 at news2.tin.it>, Alex Martelli wrote:
><posted & mailed>
> 
> Beni Cherniavsky wrote:
> 
>> I have a university shell account with 10MB quota (most of it filled
>> by mail).  At last they've installed python 2.2 (they forgot curses,
>> sockets and some other nice modules but never mind ;).  Now I'm
>> installing some modules, notably docutils, under my home directory and
>> it takes even more of my quota...
>> 
>> Is there some pure-python way to use zip imports in Python 2.2 so I
>> can save space by compressing the installed modules?
> 
> You might kludge something up by using your site.py (or the like) to
> substitute a customized __import__ wrapping the normal built-in one --
> your customized __import__ might look into the relevant zipfiles'
> directories, extract the needed modules into some /tmp subdirectory
> suitably created and inserted in your sys.path, then delegate to the
> real-builtin __import__.  There are cleaner ways but you may not need
> generality enough -- perhaps this kludge, perfected experimentally
> for your specific and rather peculiar needs, might suffice.

To save yourself some coding, you could use the vfs importer in
SkunkWeb.  To use it you need the vfs package available at

  http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/skunkweb/skunkweb/pylibs/vfs/

or in the skunkweb tarball (actually, sourceforge's viewcvs seems to
be borked at the moment) and Gordon MacMillan's iu.py, which is
available at ../iu.py relative to the above, among other places.

Then you need to do something along these lines:

  import vfs.importer
  # install the import hook
  vfs.importer.install()
  # create a virtual filesystem that is backed with a zip file.
  # if you have multiple archives, you could create a MultiFS
  # and mount various other fses at mount points on it.
  vfs.VFSRegistry['foo']=vfs.ZipFS('/home/me/myzip.zip')
  import sys
  sys.path.append('vfs://<foo>/path/to/my/libs')
  # you should now be able to use the import hook
  import MyZippyModule

I haven't tested this particular example, but I doubt that it contains
more than two or three fundamental errors!  

You can also import from various other sources, of course, by means of this hook.

Cheers,

Jacob Smullyan




More information about the Python-list mailing list