modifiable config files in compiled code?

Discussion in 'Python' started by gaudetteje, Mar 10, 2005.

  1. gaudetteje

    gaudetteje Guest

    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
     
    gaudetteje, Mar 10, 2005
    #1
    1. Advertisements

  2. gaudetteje

    Tom Willis Guest

    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...
     
    Tom Willis, Mar 10, 2005
    #2
    1. Advertisements

  3. gaudetteje

    Larry Bates Guest

    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
     
    Larry Bates, Mar 10, 2005
    #3
  4. gaudetteje

    Steve Holden Guest

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

    regards
    Steve
     
    Steve Holden, Mar 10, 2005
    #4
  5. gaudetteje

    Tom Willis Guest

    That's what was thinking when I posted my response.
     
    Tom Willis, Mar 10, 2005
    #5
  6. 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 Marasanova, Mar 11, 2005
    #6
  7. gaudetteje

    gaudetteje Guest

    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
     
    gaudetteje, Mar 11, 2005
    #7
  8. gaudetteje

    gaudetteje Guest

    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.
     
    gaudetteje, Mar 11, 2005
    #8
  9. gaudetteje

    Tom Willis Guest

    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. :)
     
    Tom Willis, Mar 11, 2005
    #9
  10. 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
     
    Stephen Thorne, Mar 11, 2005
    #10
  11. You can save this step by using the exclude module option of py2exe.
     
    Thomas Heller, Mar 11, 2005
    #11
  12. 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 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
     
    Maarten Sneep, Mar 11, 2005
    #12
  13. gaudetteje

    Fuzzyman Guest

    Fuzzyman, Mar 11, 2005
    #13
  14. gaudetteje

    gaudetteje Guest

    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.
     
    gaudetteje, Mar 11, 2005
    #14
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.