[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

distutils and data files

Samuel Karl Peterson

2/20/2008 1:25:00 AM

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. I have pictures and sound
files in this folder that this program uses. I've always just used
the relative path names of these files in my program.

Lately, I had the idea of cleaning up my program and packaging it with
distutils, but I've been stuck on a good way to deal with these
resource files. The package_data keyword seems to be the way to go,
but how can I locate and open my files once they've been moved? In
other words, what should I do about changing the relative path names?
I need something that could work from both the extracted source
folder, AND when the program gets installed via the python setup.py
install command.
--
Sam Peterson
skpeterson At nospam ucdavis.edu
"if programmers were paid to remove code instead of adding it,
software would be much better" -- unknown
4 Answers

Robert Bossy

2/20/2008 8:29:00 AM

0

Sam Peterson wrote:
> 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. I have pictures and sound
> files in this folder that this program uses. I've always just used
> the relative path names of these files in my program.
>
> Lately, I had the idea of cleaning up my program and packaging it with
> distutils, but I've been stuck on a good way to deal with these
> resource files. The package_data keyword seems to be the way to go,
> but how can I locate and open my files once they've been moved? In
> other words, what should I do about changing the relative path names?
> I need something that could work from both the extracted source
> folder, AND when the program gets installed via the python setup.py
> install command.
>
This seems to be a classic distutils question: how a python module can
access to data files *after* being installed?

The following thread addresses this issue:
http://www.gossamer-threads.com/lists/python/pyt...

Carl Banks' solution seems to overcome the problem: his trick is to
generate an additional configuration module with the relevant
informations from the distutil data structure. However it is quite an
old thread (2003) and I don't know if there has been progress made since
then, maybe the distutils module now incorporates a similar mechanism.

Hope it helps,
RB

Samuel Karl Peterson

2/22/2008 7:28:00 AM

0

Robert Bossy <Robert.Bossy@jouy.inra.fr> on Wed, 20 Feb 2008 09:29:12
+0100 didst step forth and proclaim thus:

> Sam Peterson wrote:
>> 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. I have pictures and sound
>> files in this folder that this program uses. I've always just used
>> the relative path names of these files in my program.
>>
>> Lately, I had the idea of cleaning up my program and packaging it with
>> distutils, but I've been stuck on a good way to deal with these
>> resource files. The package_data keyword seems to be the way to go,
>> but how can I locate and open my files once they've been moved? In
>> other words, what should I do about changing the relative path names?
>> I need something that could work from both the extracted source
>> folder, AND when the program gets installed via the python setup.py
>> install command.
>>
> This seems to be a classic distutils question: how a python module
> can access to data files *after* being installed?

Man, I was afraid of that. Python is an awesome language, but this is
yet another instance of seeing something in the standard library
sucking.

> The following thread addresses this issue:
> http://www.gossamer-threads.com/lists/python/pyt...
>
> Carl Banks' solution seems to overcome the problem: his trick is to
> generate an additional configuration module with the relevant
> informations from the distutil data structure. However it is quite an
> old thread (2003) and I don't know if there has been progress made
> since then, maybe the distutils module now incorporates a similar
> mechanism.

Not if the documentation for 2.5's got anything to say about it. If
it does, it's well hidden.

I think I'll kill the idea of using distutils for my program. It
seems like distutils was primarily designed for modules and
extensions.

--
Sam Peterson
skpeterson At nospam ucdavis.edu
"if programmers were paid to remove code instead of adding it,
software would be much better" -- unknown

Diez B. Roggisch

2/22/2008 9:45:00 AM

0

> Not if the documentation for 2.5's got anything to say about it. If
> it does, it's well hidden.
>
> I think I'll kill the idea of using distutils for my program. It
> seems like distutils was primarily designed for modules and
> extensions.

Start using setuptools and pkg_resources. Then you can use pkg_resources
to locate a module directory, and use that to fetch data contained in there.

Diez

Gabriel Genellina

2/26/2008 1:15:00 AM

0

En Fri, 22 Feb 2008 05:28:22 -0200, Sam Peterson
<skpeterson@nospam.please.ucdavis.edu> escribió:
> Robert Bossy <Robert.Bossy@jouy.inra.fr> on Wed, 20 Feb 2008 09:29:12
> +0100 didst step forth and proclaim thus:
>
>> Sam Peterson wrote:
>>> 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. I have pictures and sound
>>> files in this folder that this program uses. I've always just used
>>> the relative path names of these files in my program.
>>>
>>> Lately, I had the idea of cleaning up my program and packaging it with
>>> distutils, but I've been stuck on a good way to deal with these
>>> resource files. The package_data keyword seems to be the way to go,
>>> but how can I locate and open my files once they've been moved? In
>>> other words, what should I do about changing the relative path names?
>>> I need something that could work from both the extracted source
>>> folder, AND when the program gets installed via the python setup.py
>>> install command.
>>>
>> This seems to be a classic distutils question: how a python module
>> can access to data files *after* being installed?
>
>
> I think I'll kill the idea of using distutils for my program. It
> seems like distutils was primarily designed for modules and
> extensions.

And packages. The package_data directive specifies data subdirectories
that will be installed under the package directory; you can use a relative
path starting at the package dir. (the thread above is about the
install-data command-line option, but package_data isn't affected by that).

That is, if you have:

pkg/
__init__.py
images/
foo.jpg
sounds/
bar.snd

At the top of __init__.py, you can say:

pkg_path = os.path.dirname(__file__)
images_path = os.path.join(pkg_path, "images")
sounds_path = os.path.join(pkg_path, "sounds")

It doesn't matter *where* pkg is actually installed, you can always reach
its subdirectories.

--
Gabriel Genellina