Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigParser vs. import config

ConfigParser is the much debated vanilla configuration parser for Python.
However you can simply import config where config.py has python code which sets configuration parameters.

What are the pros\cons of these two approaches of configuration? When should I choose each?

like image 822
Jonathan Livni Avatar asked Nov 09 '11 15:11

Jonathan Livni


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.

Does Configparser come with Python?

configparser comes from Python 3 and as such it works well with Unicode.

What is config () in Python?

A Python configuration file is a pure Python file that populates a configuration object. This configuration object is a Config instance.


2 Answers

The biggest issue I see with import config is that you don't know what will happen when you import it. Yes, you will get a set of symbols that are naturally referenced using a . style interface. But the code in the configuration file can also do who-knows-what. Now, if you completely trust your users, then allowing them to do whatever they feel like in the config file is possibly a good thing. However, if you have unknown quantities, or you want to protect users from themselves, then having a configuration file in a more traditional format will be safer and more secure.

like image 50
Nathan Avatar answered Sep 29 '22 22:09

Nathan


This completley depends on your needs and goals for the script. One way really isnt "better", just different. For a very detailed discussion on most of pythons config parsers (including ConfigParser and config modules), see:

Python Wiki - ConfigParserShootout

like image 39
chown Avatar answered Sep 29 '22 22:09

chown