FAQ
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?

Search Discussions

  • Mhearne808 at Jul 27, 2009 at 6:57 pm

    On Jul 27, 12:43?pm, 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?
    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
  • Dave Angel at Jul 27, 2009 at 8:53 pm

    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).
  • Gabriel Genellina at Jul 28, 2009 at 2:17 am

    En Mon, 27 Jul 2009 17:53:12 -0300, Dave Angel <davea at 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
  • Dave Angel at Jul 28, 2009 at 10:43 am

    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
  • Marco Mariani at Jul 28, 2009 at 8:18 am

    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.
    """

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 27, '09 at 6:43p
activeJul 28, '09 at 10:43a
posts6
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase