How do I refer to a data file relative to a package or installation directory?

P

Parzival

I have a package that includes some data files. I am planning to use a
distutils setup script to install the package. How can I compute a path
name for such a data file that will always be relative to my package
root directory (i.e. both when installed, and in my development directory?)

Or to rephrase, say the data file is mypkg/data/foo.dat relative to the
package root "mypkg" (the directory that holds __init__.py). Is there an
enquiry that will return the absolute path prefix for mypkg?


-- Parzival
-- Reply-to is confuggled: parzp (@) shaw (.) ca
 
P

Peter L Hansen

Parzival said:
I have a package that includes some data files. I am planning to use a
distutils setup script to install the package. How can I compute a path
name for such a data file that will always be relative to my package
root directory (i.e. both when installed, and in my development directory?)

Or to rephrase, say the data file is mypkg/data/foo.dat relative to the
package root "mypkg" (the directory that holds __init__.py). Is there an
enquiry that will return the absolute path prefix for mypkg?

Presumably you meant that relative to the package root "mypkg",
the data file is actually at "data/foo.dat", or as some might
write it "./data/foo.dat"? If not, you've got an extra layer
there confusing things.

Basically, just use __file__ inside the __init__.py module to find
that file's location, then use os.path.split() to pull off the
path part and os.path.join() to add the "data/foo.dat" part.

If that's not enough, please post some kind of code snippet
to help explain things better.

-Peter
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top