How to read file during module import?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Apr 23 02:56:41 CEST 2010


En Sun, 18 Apr 2010 22:21:40 -0300, HigStar <adrian at higstar.com> escribió:
> On Apr 13, 4:03 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> wrote:
>> En Sun, 11 Apr 2010 19:43:03 -0300,HigStar<adr... at higstar.com> escribió:
>>
>> > I have had trouble with the __file__ attribute in the past, when using
>> > py2exe (i.e. on the windows platform) and using the bundle feature
>> > (which zips all files).
>> > Using os.path.realpath( __file__ ) resolves to something like .../
>> > library.zip/packageName/fileName
>> > Then when trying to perform an open on a file, say .../library.zip/
>> > packageName/supportingPackageName, the file can not be found.
>>
>> Use pkgutil.get_data  
>> then:http://docs.python.org/library/pkgutil.html#pkgutil.get_data
>>
> I should have been more specific with my example, the problem I had
> was when trying to reference a non python file, so something like .../
> library.zip/packageName/supportingDll.dll or .../library.zip/
> packageName/supportingConfigFile.ini
> For the DLL case, adding the directory that __file__ lives in to the
> os.environ["PATH"] doesn't work.

pkgutil.get_data let you extract such data from the .zip file, but you  
can't use it as a file name. For a config file it may be ok, but I don't  
think you can load a DLL that way. At least for us mere mortals, Windows  
requires the DLL to actually reside on a disk file by its own. Perhaps  
some black magic might help...

-- 
Gabriel Genellina




More information about the Python-list mailing list