[Python-Dev] Re: [Zope3-dev] Zip import and sys.path manipulation (was Re: directory hierarchy proposal)

Phillip J. Eby pje@telecommunity.com
Mon, 16 Dec 2002 12:02:28 -0500


At 11:54 AM 12/16/02 -0500, Guido van Rossum wrote:

>I tried putting Zope3 in a zipfile, and it's hopeless; lots and lots
>of code expects to find data files (at least page templates and zcml
>files; probably also style sheets and other stuff that I don't even
>know about) by taking a module's __file__, stripping the filename to
>get a directory, and concatenating the directory with the name of a
>known data file.  Plus there's the issue of extension modules.
>
>So I think the isdir() not working for zip files can be considered a
>feature. :-)

Alternately, now that there's a "pkgutils" to put it in, why not add a 
"fileNearModule()" function that returns a file handle opened for reading 
on the basis of a module name and a file name?  I currently use something 
roughly like:

def fileNearModule(moduleName,filename):
     basedir = os.path.dirname(sys.modules[moduleName].__file__)
     return open(os.path.join(basedir,filename), 'r')

Of course, a zipfile version would need some work, and clearly the zipfile 
version wouldn't be a candidate for alpha 1.

Anyway, if we could establish an API for some of these sorts of things in 
pkgutils, then they could be the official correct way to get at data files 
and make zipfiles work for a lot more things long term.