Accessing application data portably

T

Tom E H

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)
 
L

Larry Bates

Tom said:
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)
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
 
L

Larry Bates

Tom said:
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)
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
 
T

Tom E H

Larry said:
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
 
L

Larry Bates

Tom said:
Larry said:
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

From my original post:

Then I use ConfigParser in my application...

for help on ConfigParser you can do:

import ConfigParser
help(ConfigParser)

-Larry Bates
 
T

Tom E H

Larry said:
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
 
L

Larry Bates

Tom said:
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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,871
Messages
2,569,919
Members
46,171
Latest member
A.N.Omalum

Latest Threads

Top