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

Guido van Rossum guido@python.org
Mon, 16 Dec 2002 12:18:35 -0500


> 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.

I think Zope3 ought to defines its own API for this, then we can
decide later on how to do this.

I've seen several places where what you propose isn't enough: when I
tried putting everything in a zipfile, I ended up giving up when I
realized that there are places that use os.stat() on filenames thus
constructed.

Another issue is that, at least for zipfiles, and probably in most
realistic cases, the underlying implementation makes it easy to give
you the entire blob of data, and trying to pretend it's a file just
require wrapping it in a StringIO instance -- maybe it makes more
sense to change your fileNearModule() API to return the blob of data
too.

--Guido van Rossum (home page: http://www.python.org/~guido/)