ConfigObj/ConfigParser vs. using YAML for Python settings file

It depends on what you want to store in your config files and how you use them

  • If you do round tripping (yaml→code→yaml) and want comments preserved you cannot use PyYAML or ConfigParser.

  • If you want to preserve the order of your keys (e.g. when you check in your config files), PyYAML doesn’t do that unless you specify !!omap (which makes it less easy to update than a normal mapping)

  • If you want to have complex structures with lists of unnamed elements containing mappings/dictionaries, then ConfigParser and ConfigObj won’t help you as the INI files key-value pairs have to go into sections and lists can only be values.

The ruamel.yaml implementation of the YAML reader supports all of the above ¹. I have used fuzzyman’s excellent ConfigObj for round trip comment preservation for a long time, as well as PyYAML for more complex structures and this combines best of both worlds. ruamel.yaml includes the yaml utility that can convert ConfigObj INI files to YAML


¹ ruamel.yaml is a YAML library that supports YAML 1.2 (I recommend using that, but then I am the author of the package). PyYAML only supports (most of) YAML 1.1.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)