473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2279

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Ströder | last post by:
HI! Is it possible to probe for installed libs with DistUtils? I'd like to automatically search for optional libs and adjust C compiler switchers before starting the build of a the extension...
1
by: Tristan Miller | last post by:
Greetings. I'm trying to install a Python program, rawdog, on a system running SuSE 9.0. I have Python 2.3 and the development package installed via apt4rpm, so distutils should be there. But...
3
by: Mike Meyer | last post by:
I've got a package that includes an extension that has a number of header files in the directory with the extension. They are specified as "depends = " in the Extension class. However, Distutils...
2
by: Rajarshi Guha | last post by:
Hi, I've been trying to package a python project and I'm a little confused about how I distribute some PNG's that the program uses as icons. Using distutils I can set the data_files argument of...
2
by: RickMuller | last post by:
I really appreciate the ease that the distutils make distributing Python modules. However, I have a question about using them to distribute non-Python (i.e. text) data files that support Python...
1
by: akbar | last post by:
Hi, I am creating not-so-important opensource application written in python for Linux. I have two files python source in src directory, named blabla1.py and blabla2.py. There are byte compiled...
0
by: Sam Peterson | last post by:
I've been googling for a while now and cannot find a good way to deal with this. I have a slightly messy python program I wrote that I've historically just run from the extracted source folder. ...
0
by: Joe Riopel | last post by:
Hi, I am using Distutils to build and distribute some packages. I do write unit tests, but I am confused as to where to put them and how to run them prior to the package being installed (The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.