Python Notes and Examples

Config files

Here are a couple of options.

Customary-style Config Files

See the docs for configparser.

from configparser import ConfigParser
config = ConfigParser()
config.read('foo.ini')

That gives you something like a map of maps. That is, you can index into it like a map, but you can’t just print the whole thing out.

Use a config.py

You can create a module containing a hashmap that can serve as a config file:

cd my-proj
touch main.py config.py
chmod +x main.py

where config.py contains:

config = {
    'name': 'ringo',
    'speed': 13,
    'radius': 5,
}

and main.py contains:

#!/usr/bin/env python3

import config as c

print('speed is', c.config['speed'])