By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

modifiable config files in compiled code?

P: n/a
Hi All,

I've been trying to come up with an elegant solution to this problem,
but can't seem to think of anything better than my solution below.

I have a Python program that needs to be converted into an executable.
The problem is that I have a "config" template file that I've been
using to modify initialization constants such as paths, global
variables, structures, etc. Obviously, once I convert my code into an
executable I can no longer read/write to my configuration file.

My solution was to convert this config file into a standard ASCII text
document and have Python parse it for user set variables. This doesn't
seem like the most elegant of solutions; however, and I thought others
must have implemented a better way than this.

Anyone have a better solution for this problem?

Jay

Jul 18 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
10 Mar 2005 06:02:22 -0800, ga********@gmail.com <ga********@gmail.com> wrote:
Hi All,

I've been trying to come up with an elegant solution to this problem,
but can't seem to think of anything better than my solution below.

I have a Python program that needs to be converted into an executable.
The problem is that I have a "config" template file that I've been
using to modify initialization constants such as paths, global
variables, structures, etc. Obviously, once I convert my code into an
executable I can no longer read/write to my configuration file.

My solution was to convert this config file into a standard ASCII text
document and have Python parse it for user set variables. This doesn't
seem like the most elegant of solutions; however, and I thought others
must have implemented a better way than this.

Anyone have a better solution for this problem?

Jay

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


Don't know if you knew about this.....
#example
from ConfigParser import ConfigParser


def getconfig():
"""
initialize configuration settings
"""
result = ConfigParser()
result.read("config.ini")
return result

def main():
"""
runs the app
"""
config = getconfig()
....
dbsettings = {}

for key,value in config.items("db"):
print "%s: %s" % (key,value)

#etc...
#-------------------config.ini--------------
;db settings
[db]
server = 127.0.0.1
db = db
user = user
password = password
#--------------------end config.ini------------

There are other projects for dealing with config information but
ConfigParser is included wth python I think.

Hope that helps...


--
Thomas G. Willis
http://paperbackmusic.net
Jul 18 '05 #2

P: n/a
Note: my comments assume Windows distribution.

Why do you think you can't you have a config file after you convert
your program to an executable? I do it all the time and so do many
other programs. The .INI config file is just a separate file that
provides a good way to pass client supplied information into an
executable (I'm assuming when you say executable you mean something
that goes through a program like py2exe). You can also pass in
information as arguments to your program or as I normally do some
combination of the two.

Steps I take:

1) Run program through py2exe
2) Build an Inno Installer script to gather all my program parts
and my .INI and README.TEXT, etc. files together into a single
setup.exe. This may include things like data files or other
"extra" files that the program requires.
3) Insert Inno Installer commands to make any install-time
changes that are required to registry or my .INI file.
4) Have Inno Installer compile everything together into setup.exe
viola' you have a single file that can be installed on any computer
that will run without Python installation.

-Larry Bates
ga********@gmail.com wrote:
Hi All,

I've been trying to come up with an elegant solution to this problem,
but can't seem to think of anything better than my solution below.

I have a Python program that needs to be converted into an executable.
The problem is that I have a "config" template file that I've been
using to modify initialization constants such as paths, global
variables, structures, etc. Obviously, once I convert my code into an
executable I can no longer read/write to my configuration file.

My solution was to convert this config file into a standard ASCII text
document and have Python parse it for user set variables. This doesn't
seem like the most elegant of solutions; however, and I thought others
must have implemented a better way than this.

Anyone have a better solution for this problem?

Jay

Jul 18 '05 #3

P: n/a
Larry Bates wrote:
Note: my comments assume Windows distribution.

Why do you think you can't you have a config file after you convert
your program to an executable? I do it all the time and so do many


I suspect the OP's config file is a Python module.

regards
Steve

Jul 18 '05 #4

P: n/a
On Thu, 10 Mar 2005 13:01:28 -0500, Steve Holden <st***@holdenweb.com> wrote:
Larry Bates wrote:
Note: my comments assume Windows distribution.

Why do you think you can't you have a config file after you convert
your program to an executable? I do it all the time and so do many


I suspect the OP's config file is a Python module.

regards
Steve

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


That's what was thinking when I posted my response.
--
Thomas G. Willis
http://paperbackmusic.net
Jul 18 '05 #5

P: n/a
You can also do:

settings = {}
execfile('/path/to/file/myconfig.conf', settings)

myconfig.conf is a Python file. After this all variables from
myconfig.conf are stored in settings dictionary.
--
Ksenia
Jul 18 '05 #6

P: n/a
Larry,

I am using py2exe to package my application into an executable, but did
not have the need for an installer such as Inno Installer. This is
definately good info for future use, though. Thank you!

Tom,

No, I didn't know about the ConfigParser module and was exactly what I
was trying to find. Py2exe will convert all *.py modules in my program
and this was the reason I couldn't simply use an import command.

I'm curious to know if the built-in execfile() command is supported by
the compiled Python code. I'll have to check this out later.

Thanks for your help, everyone!

Jay

Jul 18 '05 #7

P: n/a
Since this utility will also be ported to the linux world, does anyone
know what the linux/unix counterpart of a Windows .INI configuration
file is?

I suppose I could get away with using XML for my config files and avoid
having two different tools altogether.

Jul 18 '05 #8

P: n/a
ConfigParser works on linux I'm pretty sure. I just ran Ipython
imported it and loaded a config file.

I don't remember anything in the docs that said otherwise.
I would prefer an xml style config file myself. But I can get by with
and ini right now. The logging framework seems to me to be the
hairiest configurations and it's able to work in an ini format. If I
need anything fancier than that, I might consider doing it for a
living and getting someone to pay me to compe up with it. :)


On 10 Mar 2005 19:34:46 -0800, ga********@gmail.com
<ga********@gmail.com> wrote:
Since this utility will also be ported to the linux world, does anyone
know what the linux/unix counterpart of a Windows .INI configuration
file is?

I suppose I could get away with using XML for my config files and avoid
having two different tools altogether.

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

--
Thomas G. Willis
http://paperbackmusic.net
Jul 18 '05 #9

P: n/a
On 10 Mar 2005 06:02:22 -0800, ga********@gmail.com
<ga********@gmail.com> wrote:
Hi All,

I've been trying to come up with an elegant solution to this problem,
but can't seem to think of anything better than my solution below.

I have a Python program that needs to be converted into an executable.
The problem is that I have a "config" template file that I've been
using to modify initialization constants such as paths, global
variables, structures, etc. Obviously, once I convert my code into an
executable I can no longer read/write to my configuration file.

My solution was to convert this config file into a standard ASCII text
document and have Python parse it for user set variables. This doesn't
seem like the most elegant of solutions; however, and I thought others
must have implemented a better way than this.

Anyone have a better solution for this problem?


Package config.py outside the zipfile containing all the python bytecode.

1) Remove config.pyc from dist\Library.zip as the last thing you do in
your setup.py.

2) As the first thing you do in main.py (or whatever your main is), do:

import sys, os
try:
import config
except ImportError:
# We're in a py2exe, so we'll append an element to the (one element)
# sys.path which points to Library.zip, to the directory that contains
# Library.zip, allowing us to import config.py
sys.path.append(os.path.split(sys.path[0]))
import config

3) Put in your setup.py

setug( ....
data=[('.', ['config.py'])]# Package config.py seperately.
)
Regards,
Stephen Thorne
Jul 18 '05 #10

P: n/a
Stephen Thorne <st************@gmail.com> writes:
On 10 Mar 2005 06:02:22 -0800, ga********@gmail.com
<ga********@gmail.com> wrote:
Hi All,

I've been trying to come up with an elegant solution to this problem,
but can't seem to think of anything better than my solution below.

I have a Python program that needs to be converted into an executable.
The problem is that I have a "config" template file that I've been
using to modify initialization constants such as paths, global
variables, structures, etc. Obviously, once I convert my code into an
executable I can no longer read/write to my configuration file.

My solution was to convert this config file into a standard ASCII text
document and have Python parse it for user set variables. This doesn't
seem like the most elegant of solutions; however, and I thought others
must have implemented a better way than this.

Anyone have a better solution for this problem?
Package config.py outside the zipfile containing all the python bytecode.

1) Remove config.pyc from dist\Library.zip as the last thing you do in
your setup.py.


You can save this step by using the exclude module option of py2exe.
2) As the first thing you do in main.py (or whatever your main is), do:

import sys, os
try:
import config
except ImportError:
# We're in a py2exe, so we'll append an element to the (one element)
# sys.path which points to Library.zip, to the directory that contains
# Library.zip, allowing us to import config.py
sys.path.append(os.path.split(sys.path[0]))
import config

3) Put in your setup.py

setug( ....
data=[('.', ['config.py'])]# Package config.py seperately.
)
Regards,
Stephen Thorne

Jul 18 '05 #11

P: n/a
In article <11**********************@f14g2000cwb.googlegroups .com>,
"ga********@gmail.com" <ga********@gmail.com> wrote:
Since this utility will also be ported to the linux world, does anyone
know what the linux/unix counterpart of a Windows .INI configuration
file is?
ConfigParser works on Linux and Mac as well. Configuration files on
Linux/Unix have a high 'roll your own' value: the format basically
depends on the needs of the program.
I suppose I could get away with using XML for my config files and avoid
having two different tools altogether.


I think you could do worse than adopt the Apple .plist format. There
is already a pure python module for these:

http://sarwat.net/opensource/

The advantage of the plist module is that the type of the variables is
stored as well, and that you can store complex variables (lists,
dictionaries) without mangling.

Maarten
Jul 18 '05 #12

P: n/a

Tom Willis wrote:
ConfigParser works on linux I'm pretty sure. I just ran Ipython
imported it and loaded a config file.

I don't remember anything in the docs that said otherwise.


ConfigParser is pure python - so it's cross platform.
ConfigObj is nicer though :-)
http://www.voidspace.org.uk/python/configobj.html

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

[snip..]

Jul 18 '05 #13

P: n/a
There are a lot of good options here. Much more than I thought I had.
Since my program's configuration file will need to be read/writeable
from an existing Java Netbeans GUI, I'll most likely move the Python
module to an INI or XML document. The deciding factor will be
whichever the other programmer is most comfortable with, of course.

Thanks, everyone.

Jul 18 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.