Apophenia Overload Apophenia Overload - 4 years ago 659
YAML Question

ConfigObj/ConfigParser vs. using YAML for Python settings file

Which is better for creating a settings file for Python programs, the built-in module (ConfigParser) or the independent project (ConfigObj), or using the YAML data serialization format? I have heard that ConfigObj is easier to use than ConfigParser, even though it is not a built-in library. I have also read that PyYAML is easy to use, though YAML takes a bit of time to use. Ease of implementation aside, which is the best option for creating a settings/configuration file?

Answer Source

Using ConfigObj is at least very straightforward and ini files in general are much simpler (and more widely used) than YAML. For more complex cases, including validation, default values and types, ConfigObj provides a way to do this through configspec validation.

Simple code to read an ini file with ConfigObj:

from configobj import ConfigObj

conf = ConfigObj('filename.ini')
section = conf['section']
value = section['value']

It automatically handles list values for you and allows multiline values. If you modify the config file you can write it back out whilst preserving order and comments.

See these articles for more info, including examples of the type validation:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download