Re: where do I put resources (images, audio files) when I wrote Python program?

2009-07-28 Thread Marco Mariani

Piotrek wrote:


that? I think about puting these files in /usr/share/myprogram and then
reading it the normal way (so the path /usr/share/myprogram would be just
hardwired in my program). Is it the way one usually does it in Python
program or is there any more sofisticated way?


Just keep them in your sources, and create an empty __init__.py file in 
the images directory.
Then install setuptools and use the pkg_resources module. It will work 
even if your application is installed as an egg through easy_install, 
possibly zipped.



To open a resource file:

f = pkg_resources.resource_stream('path.to.package', 'resource.png')
f.read()



Return a readable file-like object for the specified resource; it 
may be an actual file, a StringIO, or some similar object. The stream is 
in binary mode, in the sense that whatever bytes are in the resource 
will be read as-is.


--
http://mail.python.org/mailman/listinfo/python-list


Re: Re: where do I put resources (images, audio files) when I wrote Python program?

2009-07-28 Thread Dave Angel

Gabriel Genellina wrote:

For those read-only resources I'd use pkgutil.get_data (instead of
manually parsing __file__):

http://docs.python.org/library/pkgutil.html#pkgutil.get_data

It's easier to manage when someone later decide to install the package as
an egg file, or distribute it using py2exe. Quoting the documentation:

The function returns a binary string that is the contents of the
specified resource.

For packages located in the filesystem, which have already been imported,
this is the rough equivalent of:

d = os.path.dirname(sys.modules[package].__file__)
data = open(os.path.join(d, resource), 'rb').read()
return data



Thanks Gabriel, I hadn't come across get_data() yet.

DaveA
--
http://mail.python.org/mailman/listinfo/python-list


where do I put resources (images, audio files) when I wrote Python program?

2009-07-27 Thread Piotrek
Hello,

I write a Python program. It will contain some images (in .png format), some
audio files (as .ogg) etc. Now I think where should my installer put these
files and how should I access them. What is the normal Python way of doing
that? I think about puting these files in /usr/share/myprogram and then
reading it the normal way (so the path /usr/share/myprogram would be just
hardwired in my program). Is it the way one usually does it in Python
program or is there any more sofisticated way?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: where do I put resources (images, audio files) when I wrote Python program?

2009-07-27 Thread mhearne808
On Jul 27, 12:43 pm, Piotrek niedzi...@gazeta.pl wrote:
 Hello,

 I write a Python program. It will contain some images (in .png format), some
 audio files (as .ogg) etc. Now I think where should my installer put these
 files and how should I access them. What is the normal Python way of doing
 that? I think about puting these files in /usr/share/myprogram and then
 reading it the normal way (so the path /usr/share/myprogram would be just
 hardwired in my program). Is it the way one usually does it in Python
 program or is there any more sofisticated way?

Usually the preferred method is either distutils (http://
docs.python.org/library/distutils.html#module-distutils) or setuptools
(http://peak.telecommunity.com/DevCenter/setuptools).  Either of these
will allow you to create source (.tar.gz) or binary distributions
that can be installed relatively easily on a target machine.

Good luck,
Mike
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: where do I put resources (images, audio files) when I wrote Python program?

2009-07-27 Thread Dave Angel

Piotrek wrote:

Hello,

I write a Python program. It will contain some images (in .png format), some
audio files (as .ogg) etc. Now I think where should my installer put these
files and how should I access them. What is the normal Python way of doing
that? I think about puting these files in /usr/share/myprogram and then
reading it the normal way (so the path /usr/share/myprogram would be just
hardwired in my program). Is it the way one usually does it in Python
program or is there any more sofisticated way?


  
I think this question is mostly independent of the issue of what's used 
to actually install the files.


My answer is to put read-only files right with the py* files, and 
writable files in the user's space.  In the former case, you can find 
the files by parsing __file__ for one of your modules.  In the latter 
case, it's system dependent.  But I'd start with a single file which is 
put in a conventional place (for your target OS).  That will be a 
configuration file (preferences files), which specifies, among other 
things, where everything else will be written.  Somebody else will have 
to tell you what the Unix convention would be.


In the case of Windows, the first file would just be a registry 
entry.  And I wouldn't write any of the actual files till the user had 
configured things, either during installation or during the first run 
(with a File-Preferences menu setup).



--
http://mail.python.org/mailman/listinfo/python-list


Re: where do I put resources (images, audio files) when I wrote Python program?

2009-07-27 Thread Gabriel Genellina

En Mon, 27 Jul 2009 17:53:12 -0300, Dave Angel da...@ieee.org escribió:

Piotrek wrote:


I write a Python program. It will contain some images (in .png format),  
some
audio files (as .ogg) etc. Now I think where should my installer put  
these
files and how should I access them. What is the normal Python way of  
doing

that? I think about puting these files in /usr/share/myprogram and then
reading it the normal way (so the path /usr/share/myprogram would be  
just

hardwired in my program). Is it the way one usually does it in Python
program or is there any more sofisticated way?


My answer is to put read-only files right with the py* files, and  
writable files in the user's space.  In the former case, you can find  
the files by parsing __file__ for one of your modules.  In the latter  
case, it's system dependent.


For those read-only resources I'd use pkgutil.get_data (instead of
manually parsing __file__):

http://docs.python.org/library/pkgutil.html#pkgutil.get_data

It's easier to manage when someone later decide to install the package as
an egg file, or distribute it using py2exe. Quoting the documentation:

The function returns a binary string that is the contents of the
specified resource.

For packages located in the filesystem, which have already been imported,
this is the rough equivalent of:

d = os.path.dirname(sys.modules[package].__file__)
data = open(os.path.join(d, resource), 'rb').read()
return data


--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list