Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's better, ConfigObj or ConfigParser?

Tags:

Which is better for creating a settings file for Python programs, the built-in module (ConfigParser), or the independent project (ConfigObj)?

like image 638
Apocryphon Avatar asked Aug 06 '10 00:08

Apocryphon


People also ask

What is the use of Configparser?

The configparser module from Python's standard library defines functionality for reading and writing configuration files as used by Microsoft Windows OS. Such files usually have . INI extension.

What is .ini file in Python?

An INI file is a configuration file for computer software. It contains key-value pairs that represent properties and their values. These key-value pairs are organized under sections.


2 Answers

I recently switched from configparser to configobj, and I'm thrilled to have done so.

For me, the big difference is configobj's validator. It lets me very easily/succinctly (1) define the type and acceptable values for each entry, and (2) set defaults.

Those two features save me a lot of code and prevent a lot from going wrong. Plus, there's really no extra overhead to using configobj in terms of the complexity of my code, and the library is quite small.

like image 140
rdchambers Avatar answered Oct 18 '22 21:10

rdchambers


Depending on your Python version, it may be contentious whether this answers your question, but after a short look at ConfigParser and ConfigObj, I settled for configparser, the Python 3 version of ConfigParser. There’s also a backported version on PyPI.

ConfigParser seemed cumbersome to me, maybe even—dare I say it—unpythonic, and with ConfigObj I encountered an esoteric problem with lists (I’d prefer to be able to align them vertically, since mine get really long), and it didn’t help that the latest version was published more than two years ago. The API of configparser, however, looked as spiffy as that of ConfigObj (albeit not as feature-rich), and when we move to Python 3, I can probably switch to the built-in version easily.

By the way, it works great so far.

like image 25
Denis Drescher Avatar answered Oct 18 '22 21:10

Denis Drescher