Accessing files installed with distutils

F

Frans Englich

This is silly. How do I access data files I've installed with distutils? In a
portable, generic way, I want to find out what is the following path on most
systems:

/usr/local/lib/python2.4/lib/site-packages/foo/bar.txt

How do I figure out the rest, if I know foo/bar.txt? sys.prefix doesn't get me
far.

I've googled, and looked in the python reference. I must be blind if the
distutils section[1] covers this.


Cheers,

Frans

1.
http://www.python.org/doc/current/dist/dist.html
 
J

John Machin

Frans said:
This is silly. How do I access data files I've installed with distutils? In a
portable, generic way, I want to find out what is the following path on most
systems:

/usr/local/lib/python2.4/lib/site-packages/foo/bar.txt

Most systems? A tad *nix-centric, yes/no?
How do I figure out the rest, if I know foo/bar.txt? sys.prefix doesn't get me
far.

This hint may be useful:
import sys
sys.modules['readline'].__file__
'c:\\Python24\\lib\\site-packages\\readline\\__init__.pyc'

but it may not work if the caller does "from yourmodule import ....."
or "import yourmodule as ym". Over to you.
 
L

Leif K-Brooks

Frans said:
This is silly. How do I access data files I've installed with distutils? In a
portable, generic way, I want to find out what is the following path on most
systems:

/usr/local/lib/python2.4/lib/site-packages/foo/bar.txt

Assuming your module is also in site-packages/foo, I would use:

import os
filename = os.path.join(os.path.dirname(__file__), 'bar.txt')
 
T

timothy.williams

I was wondering how to do this too. I'm trying to write a distutils
setup.py script that has some data I'd like to include. From the
distutils docs I get

data_files specifies a sequence of (directory, files) pairs in the
following way:

setup(...
data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
('config', ['cfg/data.cfg']),
('/etc/init.d', ['init-script'])]
)

I can use sys.prefix to find the top level where python is installed,
but this doesn't tell me specifically where site-packages is. On my
Linux box it is in

{sys.prefix}/lib/python2.3/site-packages

but on Windows, it's in

{sys.prefix}/Lib/site-packages.

Do I need to use sys.platform (along with sys.version) to check what
type of machine I'm on, or is there some better method to get the
location of site-packages?

Thanks.
 
S

Steve Holden

I was wondering how to do this too. I'm trying to write a distutils
setup.py script that has some data I'd like to include. From the
distutils docs I get

data_files specifies a sequence of (directory, files) pairs in the
following way:

setup(...
data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
('config', ['cfg/data.cfg']),
('/etc/init.d', ['init-script'])]
)

I can use sys.prefix to find the top level where python is installed,
but this doesn't tell me specifically where site-packages is. On my
Linux box it is in

{sys.prefix}/lib/python2.3/site-packages

but on Windows, it's in

{sys.prefix}/Lib/site-packages.

Do I need to use sys.platform (along with sys.version) to check what
type of machine I'm on, or is there some better method to get the
location of site-packages?
This is one of the areas where distutils could probably do with some
improvement. I don;t know whether it's on any developers priority list,
though.

regards
Steve
 
T

Thomas Heller

Steve Holden said:
I was wondering how to do this too. I'm trying to write a distutils
setup.py script that has some data I'd like to include. From the
distutils docs I get
data_files specifies a sequence of (directory, files) pairs in the
following way:
setup(...
data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
('config', ['cfg/data.cfg']),
('/etc/init.d', ['init-script'])]
)
I can use sys.prefix to find the top level where python is installed,
but this doesn't tell me specifically where site-packages is. On my
Linux box it is in
{sys.prefix}/lib/python2.3/site-packages
but on Windows, it's in
{sys.prefix}/Lib/site-packages.
Do I need to use sys.platform (along with sys.version) to check what
type of machine I'm on, or is there some better method to get the
location of site-packages?
This is one of the areas where distutils could probably do with some
improvement. I don;t know whether it's on any developers priority
list, though.

There are some functions in distutils.sysconfig which may help.

Thomas
 
T

timothy.williams

I ended up using the trick I found in the Disutils Cookbook.

http://www.python.org/moin/DistutilsInstallDataScattered

This works fine for me now, but I have another distutils question.

My package requires Pmw and another home grown package that has a
source dist and a Windows dist built with the bdist_wininst command.
Is there a way to include the tarballs and *.win32-py2.3.exe files to
install them if necessary?

What I have now is that I'm including these in my source dist for
Linux, but I don't know how to do some type of conditional install. I
tried adding the Pmw package in my setup.py, but the Pmw.def file
doesn't make it over. Only the *.py files are in the MANIFEST. I
suppose I could change my MANIFEST.in file, but it seems to me that
having "Pmw" in the packages list should be sufficient.

Does Python2.4 have more extensive distutils documentation? (I'm using
2.3.2 now.)

Thanks.
 
C

Carl Banks

Frans said:
This is silly. How do I access data files I've installed with distutils? In a
portable, generic way, I want to find out what is the following path on most
systems:

/usr/local/lib/python2.4/lib/site-packages/foo/bar.txt

How do I figure out the rest, if I know foo/bar.txt? sys.prefix doesn't get me
far.

I've googled, and looked in the python reference. I must be blind if the
distutils section[1] covers this.

It doesn't, because distutils doens't do this. The data_files option
to distutils is pretty limited, is buggy, and if you override the data
dir, distutils has absolutely no way to let the Python code know about
it. Once or twice I've thought about making a patch, but it's a
difficult problem since different parts of distutils need to
communicate to make it work, in a way distutils engine doesn't support.

Workarounds:

Probably the most straightforward way is to just assume the data files
are in the default place, but provide a way to override with an
environment variable. If the user overrides the data dir when
installing, then it's his or her responsibility to define the
environment variable declaring where the data is.

The second workaround is to inspect the distutils structure (that which
is returned by setup), find out where the data dir is, and create a
Python module that sets the data file location. I did this for a
certain package I wrote. I posted this example here in
comp.lang.python a while back:

http://tinyurl.com/5qgsw
 

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,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top