Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python single configuration file

Tags:

I am developing a project that requires a single configuration file whose data is used by multiple modules.
My question is: what is the common approach to that? should i read the configuration file from each of my modules (files) or is there any other way to do it?

I was thinking to have a module named config.py that reads the configuration files and whenever I need a config I do import config and then do something like config.data['teamsdir'] get the 'teamsdir' property (for example).

response: opted for the conf.py approach then since it it is modular, flexible and simple

I can just put the configuration data directly in the file, latter if i want to read from a json file a xml file or multiple sources i just change the conf.py and make sure the data is accessed the same way.

accepted answer: chose "Alex Martelli" response because it was the most complete. voted up other answers because they where good and useful too.

like image 460
João Portela Avatar asked Feb 27 '10 20:02

João Portela


1 Answers

I like the approach of a single config.py module whose body (when first imported) parses one or more configuration-data files and sets its own "global variables" appropriately -- though I'd favor config.teamdata over the round-about config.data['teamdata'] approach.

This assumes configuration settings are read-only once loaded (except maybe in unit-testing scenarios, where the test-code will be doing its own artificial setting of config variables to properly exercise the code-under-test) -- it basically exploits the nature of a module as the simplest Pythonic form of "singleton" (when you don't need subclassing or other features supported only by classes and not by modules, of course).

"One or more" configuration files (e.g. first one somewhere in /etc for general default settings, then one under /usr/local for site-specific overrides thereof, then again possibly one in the user's home directory for user specific settings) is a common and useful pattern.

like image 125
Alex Martelli Avatar answered Sep 22 '22 11:09

Alex Martelli