[Tutor] How do you implement a config file?

Kent Johnson kent37 at tds.net
Sun Jul 16 14:29:08 CEST 2006


Tracy R Reed wrote:
> I am writing a small python application that needs a few variables to be
> end user configurable. Right now I just have the variables right up
> front where the user can tweak them in the program code and a big
> commented line that says "Nothing editable past this point." But I would
> like to be able to break this out into a separate config file. There are
> two ways I see to do this:
>
> 1. Use ConfigParser. But from all of the docs and examples I see
> ConfigParser only accepts name=value type pairs. How can I implement a
> list or a dictionary of configs in ConfigParser?
>
> Part of my config involves things like:
>
> foo = ['bar','baz','bah']
>
> How would I represent this cleanly in config parser? 

ConfigObj supports list data.
http://www.voidspace.org.uk/python/configobj.html

There is a list of alternative config file parsers here:
http://wiki.python.org/moin/ConfigParserShootout

To import a file in Python syntax whose name doesn't end in .py, I think 
you can use the imp module though I'm not certain of the details. This 
post might give you some clues:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/775dabe0a5e63f4f/f3bb17d8c7377aad?q=import+imp&rnum=3#f3bb17d8c7377aad

Kent



More information about the Tutor mailing list