Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Accessing files installed with distutils

Reply
Thread Tools

Accessing files installed with distutils

 
 
Frans Englich
Guest
Posts: n/a
 
      02-21-2005

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
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      02-22-2005

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.

 
Reply With Quote
 
 
 
 
Leif K-Brooks
Guest
Posts: n/a
 
      02-22-2005
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')
 
Reply With Quote
 
timothy.williams@nvl.army.mil
Guest
Posts: n/a
 
      03-08-2005
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.

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      03-08-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Thomas Heller
Guest
Posts: n/a
 
      03-08-2005
Steve Holden <(E-Mail Removed)> writes:

> (E-Mail Removed) 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
 
Reply With Quote
 
timothy.williams@nvl.army.mil
Guest
Posts: n/a
 
      03-08-2005
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.

 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      03-08-2005

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [Distutils] A new script which creates Python 3.3 venvs withDistribute and pip installed in them Philippe Ombredanne Python 0 01-31-2013 09:05 AM
distutils, No module named numpy.distutils.fcompiler.conv_template Luis Alberto Zarrabeitia Gomez Python 0 03-30-2009 03:56 PM
can distutils windows installer invoke another distutils windows installer timw.google Python 1 05-11-2006 10:07 PM
python-devel installed; can't find distutils.core Tristan Miller Python 1 07-23-2004 09:08 PM
autoconf-style checking for installed libs with DistUtils =?ISO-8859-1?Q?Michael_Str=F6der?= Python 2 07-28-2003 11:19 AM



Advertisments