473,383 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Accessing application data portably

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)

Aug 23 '06 #1
6 1648
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)
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
Aug 23 '06 #2
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)
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

Aug 23 '06 #3
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
Aug 23 '06 #4
Tom E H wrote:
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
From my original post:

Then I use ConfigParser in my application...

for help on ConfigParser you can do:

import ConfigParser
help(ConfigParser)

-Larry Bates

Aug 23 '06 #5
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

Aug 23 '06 #6
Tom E H wrote:
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
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 pathargument
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
Aug 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
4
by: Tom | last post by:
Let's say I've got a class defined in a header file. class foo { private: ... int bar; .... }; Now lets say I have a function that needs to access the private variable
22
by: Tony Houghton | last post by:
I'm using pygame to write a game called Bombz which needs to save some data in a directory associated with it. In Unix/Linux I'd probably use "~/.bombz", in Windows something like "C:\Documents...
3
by: prodirect | last post by:
Hi all, I hope someone can help me. I've recently created a database and wanted to put it up on an ftp sight so that multiple people could access the same tables at the same time from different...
4
by: Eugen Walcher | last post by:
Hello all, I've tried posting this same question on other newsgroups with no luck. This group seems to have a lot more activity so I apologize if you have seen it before. I'm trying to...
8
by: nkrisraj | last post by:
Hi, I have a following structure: typedef struct { RateData rdr; int RateID; char RateBalance; } RateInfo;
14
by: rsood | last post by:
Hi I'm developing a program, and naturally I want it to be as portable as possible. I need to be able to access specific numbers of bytes in it, but as far as I know, there is no keyword in the...
14
by: Kavya | last post by:
Here is the code int main(){ struct node{ int a; int b; int c; }; struct node s={3,5,6}; struct node *ptr=&s;
4
by: Noy B | last post by:
Hi, I have developed a small application that is using a MSAccess DB. the problem is that it was developed on a machine where the application and the DB are both located. now it needs to be...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.