PythonMastery
Python programs · Useful programs · intermediate

Read a config file in Python

Load settings from a JSON config file on top of default values, keep working when the file is missing, and read an INI file with configparser.

A config file keeps settings such as a port number or a debug switch outside your code, so you can change them without editing the program. This reader starts from default values and lets the file replace only the settings it lists.

The program

python
import json

DEFAULTS = {"host": "localhost", "port": 8000, "debug": False, "log_level": "INFO"}

# create a sample config file so this example has something to read
with open("config.json", "w", encoding="utf-8") as f:
    json.dump({"port": 8080, "debug": True}, f)


def load_config(path, defaults):
    config = dict(defaults)
    try:
        with open(path, encoding="utf-8") as f:
            config.update(json.load(f))
    except FileNotFoundError:
        print(f"{path} not found, using the defaults")
    return config


config = load_config("config.json", DEFAULTS)
for key, value in config.items():
    print(f"{key} = {value!r}")

print(load_config("missing.json", DEFAULTS)["port"])
output
host = 'localhost'
port = 8080
debug = True
log_level = 'INFO'
missing.json not found, using the defaults
8000

How it works

Another way: an INI file

INI files are plain text with [sections] and key = value lines. The standard library reads them with configparser:

python
import configparser

with open("settings.ini", "w", encoding="utf-8") as f:
    f.write("[server]\nhost = example.com\nport = 8080\n\n[app]\ndebug = yes\n")

parser = configparser.ConfigParser()
parser.read("settings.ini", encoding="utf-8")

host = parser.get("server", "host")
port = parser.getint("server", "port")
debug = parser.getboolean("app", "debug")
timeout = parser.getint("server", "timeout", fallback=30)
print(host, port, debug, timeout)
output
example.com 8080 True 30

INI values are always read as text, so use getint() and getboolean() to convert them. getboolean() accepts yes/no, on/off, true/false and 1/0. fallback=30 is the value to use when the setting is missing.

Learn the idea: CSV, JSON, and JSONL: Structured Text Done Right, Dictionaries, Errors & Exceptions: When Things Go Wrong