Python configuration file: Any file format recommendation? INI format still appropriate? Seems quite old school

Consider using plain Python files as configuration files.

An example (config.py):

# use normal python comments

value1 = 32
value2 = "A string value"

value3 = ["lists", "are", "handy"]
value4 = {"and": "so", "are": "dictionaries"}

In your program, load the config file using exec (docs):

from pathlib import Path

if __name__ == "__main__":
    config = {}
    exec(Path("config.py").read_text(encoding="utf8"), {}, config)
    
    print config["value1"]
    print config["value4"]

I like this approach, for the following reasons:

  • In the simple case, the format is as easy to author as an INI-style config file. It also shares an important characteristic with INI files: it is very suitable for version control (this is less true for XML and maybe also for JSON)
  • I like the flexibility that comes with having the config file in an actual programming language.

The approach is widely used, a few examples:

  • A Django site's settings lives inside settings.py. Django does not use execfile, it uses import to read/execute settings.py AFAIK, but the end result is the same: the code inside the settings file is executed.
  • The bash shell reads and executes ~/.bashrc on startup.
  • The Python interpreter imports site.py on startup.

INI is till totally OK and as other said, the format of your config file really depends from how you are going to use it.

Personally I am a fan of YAML: concise, readable, flexible.

Google seems to share my enthusiasm, as they use it too in the Google App Engine. The python parser is here.


Dictionaries are pretty popular as well. Basically a hash table.

{"one": 1, "two": 2} is an example, kind of looks like json.

Then you can call it up like mydict["one"], which would return 1.

Then you can use shelve to save the dictionary to a file:

mydict = shelve.open(filename)
# then you can call it from there, like
mydict["one"]

So, it's somewhat easier then an ini file. You can add stuff just like a list or change options pretty easily and then once you close it, it will write it back out.

Heres a simple example of what i mean:

import shelve

def main():
    mydict = shelve.open("testfile")
    mydict["newKey"] = value("some comment", 5)
    print(mydict["newKey"].value)
    print(mydict["newKey"].comment)
    mydict.close()


class value():
    def __init__(self, comment, value):
        self.comment = comment
        self.value = value



if __name__ == '__main__':
    main()

Tags:

Python