How to use configuration files in Python
I’ve been doing quite a bit of Python lately and the application I’m working on right now is actually a rewrite of scripts that we wrote over a year ago. The scripts started out to be pretty simple but they have now grown to a point that to be able to maintain and extend them quickly they’ll need to be completely rewritten with a new design.
One of the things we’re adding is a configuration file. It might seem a little weird to use config files in Python but there are good reasons for it:
- Easier for non-programmers to customize and understand. If your support team has little or no knowledge of Python and they need to make some changes, such as file locations when a drive fails, for example, they may have a hard time figuring out what to change or accidentally change other portions of the code that could cause failure. Having those options in the config file will reduce the chances of unintended changes happening because you’re limiting them on what they can change. It also makes training easier because the config file will be in a format that’s easy to understand and if you put good comments in your config file it should pretty much be self-explanatory.
- Easier deployments. If you’re deploying your application to multiple environments, you can basically just create a config file for each environment and write a script where you can pass in the environment as an argument. Based on that argument, you can then build the application using the proper config file (similar to ant).
Here’s a quick example on how to do this using the ConfigParser module, which is already part of the standard library.
Sample configuration file (settings.ini):
[DEFAULT] application_base_folder: D:\MySuperCoolApp input_files_base_folder: \\server\inputs output_files_base_folder: \\server\outputs [log] # Below is an example on how to use interpolation. location: %(application_base_folder)s\logs [database] server: VMDB01 database: MySuperCoolApp max_connections: 100 [report] run: True
The portions in brackets (e.g. [report]) is called a section. Inside the section you have the options (e.g. run) and the value (e.g. True). You can add a comment in your config file by putting a ‘#’ or ‘;’ before your comment.
You can also add a special section called the DEFAULT section. This is very useful when using interpolation. Normally, you can only use interpolation to reference values in the same section, but by putting the options/values in the DEFAULT section you will be able to reference them anywhere in the config file. This is a good place to define your base paths. I guess you can think of this as a “global” section. See the [log] section above for an example on how to use interpolation.
Sample Python code using the configuration file:
import ConfigParser config = ConfigParser.ConfigParser() config.read('settings.ini') print config.get('DEFAULT', 'application_base_folder') print config.get('log', 'location') print config.getint('database', 'max_connections') print config.getboolean('report', 'run')
Output:
D:\MySuperCoolApp D:\MySuperCoolApp\logs 100 True
As you can see, using the ConfigParser module is very easy. It also has methods that already converts the value to the desired type.