473,406 Members | 2,745 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,406 software developers and data experts.

modifiable config files in compiled code?

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
13 1493
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
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
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
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
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
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
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
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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Graham | last post by:
I noticed a similar post awhile ago and in terms of my problem it wasnt a suitable answer so I will ask again. I have VS2005 running a on development machine in my office where I do all my...
5
by: Sridhar | last post by:
Hi, I have created a project which contains classes to read the data from the database. This project has an App.Config file which contains the SqlConnection String. when this code is called from...
4
by: aSoundMind | last post by:
Hi there, I recieve this error Server Error in '/integrate' Application. -------------------------------------------------------------------------------- Configuration Error Description: An...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.