modifiable config files in compiled code?

Stephen Thorne stephen.thorne at gmail.com
Fri Mar 11 05:41:06 CET 2005


On 10 Mar 2005 06:02:22 -0800, gaudetteje at gmail.com
<gaudetteje at 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



More information about the Python-list mailing list