[SciPy-user] paths relative to a python package

Robert Kern robert.kern@gmail....
Tue Apr 1 18:40:23 CDT 2008

On Tue, Apr 1, 2008 at 6:37 PM, Zane Selvans <zane@ideotrope.org> wrote:
> I know this isn't scipy specific but...
>  If you have a data file that's distributed with a python package, how do
>  you refer to it relative to the location that the package ultimately
>  gets installed?  Is there a package_root variable or something?
>  e.g. in my package directory let's say I have:
>  __init__.py
>  MyModule.py
>  datadir
>  and within datadir:
>  data1.dat
>  data2.dat
>  ...
>  How do I, within MyModule.py tell the program to open one of the data files?

import os

dirname = os.path.dirname(os.path.abspath(__file__))
datadir = os.path.join(dirname, 'datadir')

f = open(os.path.join(datadir, 'data1.dat'))

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco

More information about the SciPy-user mailing list