Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Accessing application data portably

1 view
Skip to first unread message

Tom E H

unread,
Aug 23, 2006, 12:20:56 PM8/23/06
to pytho...@python.org
My Python application includes some data files that need to be accessed by
modules I distribute with it.

Where can I put them, and how should I arrange my code, so that it works
across platforms?

On Linux, I could install the data to "/usr/lib/myprogram/datafile", and
on Windows to "datafile" relative to where the executable (made by
py2exe) is installed. Then I could detect the operating system, and choose
appropriately.

To be that explicit seems undesirable. Any cleverer ideas?

Tom

(Please CC me on replies: I'm not subscribed. The From address is munged)

Larry Bates

unread,
Aug 23, 2006, 4:47:29 PM8/23/06
to Tom E H, pytho...@python.org
I almost always send along an application.ini file and put the location
of where my data is to be stored in that file instead of imbedding (or
worse, hard-coding) it in the application program itself. I also put
other parameters that the user might want to change that will change
the behavior of my program (debugging, logging, etc.) there also. Then
during installation I modify the option in this file with the install
script.

Something like:

[init]
debug=0
quiet=0
datafilepath=/usr/lib/myprogram/datafile

or

[init]
debug=0
quiet=0
datafilepath=C:\Program Files\myprogram\datafile

Then I use ConfigParser in my application to read this file and
extract the parameters. Makes it easy for more experienced users
(and me) to be able to easily relocate the datafile if they
desire.

On Windows I use Inno Installer and it can modify these options inside the
.ini file during the installation so that datafilepath points to where
my data actually will live. Works perfectly for me.

-Larry Bates

Larry Bates

unread,
Aug 23, 2006, 4:47:29 PM8/23/06
to Tom E H, pytho...@python.org
Message has been deleted

Tom E H

unread,
Aug 23, 2006, 5:40:20 PM8/23/06
to
Larry Bates wrote:
> Tom E H wrote:
>> My Python application includes some data files that need to be accessed
>> by modules I distribute with it.
>>
>> Where can I put them, and how should I arrange my code, so that it works
>> across platforms?
>>
> I almost always send along an application.ini file and put the location
> of where my data is to be stored in that file instead of imbedding (or
> worse, hard-coding) it in the application program itself.

> Something like:
>
> [init]
> debug=0
> quiet=0
> datafilepath=/usr/lib/myprogram/datafile

Well that's great, but how do you access the ini file portably?

Tom

Larry Bates

unread,
Aug 23, 2006, 6:55:54 PM8/23/06
to Tom E H

From my original post:

Then I use ConfigParser in my application...

for help on ConfigParser you can do:

import ConfigParser
help(ConfigParser)

-Larry Bates

Tom E H

unread,
Aug 23, 2006, 7:19:31 PM8/23/06
to
Larry Bates wrote:
>> Well that's great, but how do you access the ini file portably?
>
> From my original post:
>
> Then I use ConfigParser in my application...

Thanks, but where in the directory structure do you put the ini file on
different platforms? Presumably you have to hard-code that into the source
and then do operating system type detection?

i.e. if I make my config parser:

import ConfigParser
config = ConfigParser.ConfigParser()
config.read(filename)

What do you use for filename on Windows? What on Linux? OSX? etc. How do
you detect which operating system you are running on?

Tom

Steve Holden

unread,
Aug 23, 2006, 10:12:09 PM8/23/06
to pytho...@python.org
Tom E H wrote:
> My Python application includes some data files that need to be accessed by
> modules I distribute with it.
>
> Where can I put them, and how should I arrange my code, so that it works
> across platforms?
>
> On Linux, I could install the data to "/usr/lib/myprogram/datafile", and
> on Windows to "datafile" relative to where the executable (made by
> py2exe) is installed. Then I could detect the operating system, and choose
> appropriately.
>
> To be that explicit seems undesirable. Any cleverer ideas?
>
> Tom
>
> (Please CC me on replies: I'm not subscribed. The From address is munged)
>
If you aren't ubscribed then the only person who is going to copy you on
email is someone complaining about your presumptuousness in assuming
they'd happily want to spend time trimming your email address just so
you won't be bothered by the spam plagie.

Sorry, the question seems to have completely gone out of my head ...

regards
Steeve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Steve Holden

unread,
Aug 23, 2006, 10:12:40 PM8/23/06
to pytho...@python.org

Larry Bates

unread,
Aug 24, 2006, 9:46:11 AM8/24/06
to Tom E H
I almost always have the .ini configuration file live in the same
directory/folder as the program or sometimes in a subdirectory of
the install directory (which I reference relative to where the
program is run from). Typically I install a default .ini file
via program installer (I like Inno Installer on Windows). I also
make all my programs accept a -i <configuration file path> argument
when they are run so you can override the default .ini file on the
command line.

example:

myprog -i C:\aaa\bbb\myprog.ini

As a default I do config.read('myprog.ini') it always reads from
the current directory (which is where the program is installed).
To access a subdirectory of the current directory I do something
like:

p=os.path.join(os.getcwd(), 'configfiles')
config.read(p)

I haven't put anything on OSX but this works fine on Windows
and Linux and should work on OSX.

-Larry Bates

0 new messages