472,146 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

Accessing files installed with distutils


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
Jul 18 '05 #1
7 2174

Frans Englich wrote:
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.

Jul 18 '05 #2
Frans Englich wrote:
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')
Jul 18 '05 #3
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.

Jul 18 '05 #4
ti**************@nvl.army.mil wrote:
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

Jul 18 '05 #5
Steve Holden <st***@holdenweb.com> writes:
ti**************@nvl.army.mil wrote:
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
Jul 18 '05 #6
I ended up using the trick I found in the Disutils Cookbook.

http://www.python.org/moin/Distutils...lDataScattered

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.

Jul 18 '05 #7

Frans Englich wrote:
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
--
CARL BANKS

Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Michael Ströder | last post: by
1 post views Thread by Tristan Miller | last post: by
3 posts views Thread by Mike Meyer | last post: by
2 posts views Thread by Rajarshi Guha | last post: by
2 posts views Thread by RickMuller | last post: by
1 post views Thread by akbar | last post: by
reply views Thread by Sam Peterson | last post: by
reply views Thread by Joe Riopel | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.