Library and real path name

M

Michele Petrazzo

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
 
R

Robert Kern

Michele said:
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),

http://peak.telecommunity.com/DevCenter/PkgResources

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
F

Fredrik Lundh

Michele said:
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/)

you can use the __file__ or __path__ variables to determine this.
e.g.

# get this module's directory
import os
prefix = os.path.dirname(__file__)

or, inside a package __init__.py file:

# get package's main directory
prefix = __path__[0]

</F>
 
M

Michele Petrazzo

Fredrik said:
Michele said:
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/)


you can use the __file__ or __path__ variables to determine this.
e.g.

# get this module's directory
import os
prefix = os.path.dirname(__file__)

or, inside a package __init__.py file:

# get package's main directory
prefix = __path__[0]

</F>

Thanks,
it work

Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top