Jonathan Livni
Jonathan Livni

Reputation: 107302

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?

Upvotes: 43

Views: 59506

Answers (5)

Danny Kip
Danny Kip

Reputation: 11

I'm sorry I am adding this as an answer, but the system here does not allow me to comment untill I have gained 50 rep. So this probably should just be a comment to the accepted answer, but alas. I'll try to make it worth your while.

Importing a config can be made better by specifying what you are importing.

Instead of just import config, you can do from config import foo, bar, listofthings, which makes you actually know (better) what you are importing, and way less susceptible to unexpected behaviour from someone "hacking" the config file.

In addition, this also makes it so you don't have to reference your variables with dot-notation as blinky = config.foo['blinky'], but just blinky = foo['blinky']

In addition you can also import classes and functions, which is way harder with another method.

So I see some benefit to doing it this way.

Upvotes: -1

Nathan
Nathan

Reputation: 4937

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.

Upvotes: 35

chown
chown

Reputation: 52798

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

Upvotes: 8

jathanism
jathanism

Reputation: 33724

IMO it comes down to a matter of personal style. Do you intend for 3rd parties to edit your config? If so, maybe it makes sense to have a more "natural" configuration style a la ConfigParser that is not as technical and that may not be too far over the heads of your target audience.

Many popular projects such as Fabric and Django use the "native" configuration style which is essentially just a Python module. Fabry has fabfile.py and Django has settings.py.

Overall, you're going to have a lot more flexibility using a native approach of importing a module simply because you can do anything you want in that file, including defining functions, classes, etc. because it's just another Python module you're importing.

Upvotes: 4

dugres
dugres

Reputation: 13113

"import config" is very simple, flexible and powerfull but, since it can do anything, it might be dangerous if the config.py is not in a safe place.

Upvotes: 6

Related Questions