Library and real path name

Michele Petrazzo michele.petrazzo at TOGLIunipex.it
Tue Aug 23 05:56:33 EDT 2005


I want to redistribute the library that I create.
I create a project, its setup.py that when launched copy all files into
the "site-packages/library" directory. And here it's all ok.
When I call my library with:

import library
library.class()

I want that my library know where are its real path
(site-packages/library/), because it has to load a data file present
into a subpackage directory (like site-package/library/data/file.dat),
but if, inside the library code, I try to load the file with:

f = open("./data/file.dat")

python raise me an exception because it not found the file. I see that
the current path are the path where I execute python.
I think that I can solve this problem with something like:

if sys.platform == 'win32': prefix = os.path.join(sys.prefix,
"Lib/site-packages/library/data/")
else: prefix = os.path.join(sys.prefix, "site-packages/library/data/")

it work, but I think that is not a good solution (a real pythonic
solution).

Can someone help me?

Thanks
Michele



More information about the Python-list mailing list