Python configuration file: any recommendation for file format? Is the INI format still suitable? Seems pretty old school

I need to save the configuration (key / value) for a Python application, and I'm looking for the best way to save these configurations in a file.

I came across Python ConfigParser and I wondered if the INI file format really fits right now? Is there a more modern format or is INI still the recommended way? (XML, JSON, ...)

Please share your opinions / recommendations ...

+59
python
Nov 22 '11 at 11:21
source share
7 answers

Consider using simple Python files as configuration files.

Example ( example.conf ):

 # use normal python comments value1 = 32 value2 = u"A unicode value" value3 = "A plain string value" value4 = ["lists", "are", "handy"] value5 = {"and": "so", "are": "dictionaries"} 

In your program, load the configuration file using execfile ( 2.7 documents ):

 if __name__ == "__main__": config = {} execfile("example.conf", config) # python 3: exec(open("example.conf").read(), config) print config["value1"] print config["value5"] 

I like this approach for the following reasons:

  • In a simple case, the format is as simple as an INI-style configuration file. It also has an important characteristic with INI files: it is very suitable for version control (this is less true for XML and possibly for JSON).
  • I like the flexibility associated with having a configuration file in a real programming language.

This approach is widely used, a few examples:

  • Django site settings are located in 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 runs ~/.bashrc at startup.
  • The Python interpreter imports (and executes) site.py at startup.
+68
Nov 22 '11 at 11:31
source share

INI is completely OK, and as another said, the format of your configuration file really depends on 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 also in the Google App Engine. The python parser is here .

+31
Nov 22 '11 at 11:35
source share

Dictionaries are quite popular. Basically a hash table.

{"one": 1, "two": 2} - an example, the view looks like json.

Then you can call it like mydict["one"] , which will 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, this is somewhat simpler than an ini file. You can easily add material in the same way as a list or change parameters, and then, as soon as you close it, it will write it back.

Here is 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() 
+9
Nov 22 '11 at 11:50
source share

It completely depends on your requirements. If (as you say) all you need is key / value pairs, ini files (or other "simple" configuration files) are perfect for you. No, they are not out of date as they are still in use.

XML / JSON is ideal if you have hierarchical structures and also want to use more complex methods (for example: checking XML files, namespaces, etc.).

+8
Nov 22 '11 at 11:24
source share

Abandon ConfigObj , this is the fastest method I've found so far, and is definitely more flexible than ConfigParser. Personally, I am not a fan of YAML because its “flexibility” makes it difficult to use tools like Augeas .

+6
Dec 31 '13 at 2:48
source share

It depends on how the configuration file is used.

One of the advantages of INI files is that they are really easy to read and understand. It is much easier to make a mistake in a JSON or XML file if you edit the configuration manually. PHP still uses INI files.

However, if you are not going to edit your configuration manually, go to any format you like because INI is not the easiest to parse.

+5
Nov 22 '11 at 11:26
source share

For completeness, you can also use a shell-style configuration format using the "shlex" module. If you have a fixed set of configuration parameters, you can combine it with the optparse module.

 from optparse import OptionParser _o = OptionParser("%prog [options] configfiles...") _o.add_option("--hostname", metavar="HOSTNAME", default="10.0.0.1") _o.add_option("--username", metavar="USERNAME", default="admin") _o.add_option("--password", metavar="PASSWORD", default="admin") import shlex def parse(filename, defaults): opt, args = _o.parse_args(shlex.split(open(filename).read()), defaults) return opt if __name__ == "__main__": import sys values, args = _o.parse_args() for arg in args: values = parse(arg, values) values, args = _o.parse_args(values = values) for name in _o.defaults: print name, "=", getattr(values, name) 

This example shows how you can associate ini files with a set of default values ​​and custom overrides. Suppose you have two files containing

file1.ini:

 --hostname 10.2.3.4 --password admin-sc 

file2.ini:

 --username "foo bar" --password "special key" 

Then you can run ./configtest.py file1.ini file2.ini --password other , and the resulting values ​​will have a host name of 10.2.3.4 and a username like "foo bar" and a password like "other". This change of configuration parameters may be useful if you already have an optparse definition for your program parameters → just reuse it and you can associate the values ​​from the command line with the values ​​from the configuration file and, possibly, with some global configuration settings.

As an incentive, configuration parameters are always documented, and configuration parameters with incorrect settings will appear at an early stage as an error, just as you can use the optparse instance to pre-check the default settings file (check the scheme). As a flaw, there are no comments in the INI, and configuration items are not easily structured. So far, your parser is essentially single-line.

0
Dec 24 '14 at 7:23
source share



All Articles