Python Notes and Examples
← prev | next →     Top-level ToC     /config-files.html     (printable version)

There are a number of options.

1 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'])

2 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.