Python Glossary Русский

all Python 3configparserexception

configparser.InterpolationError

exceptions

The base exception for %(name)s and ${section:option} substitution errors: bad syntax, a missing reference and a chain that is too deep.

Syntax

except configparser.InterpolationError as e: e.section, e.option

Timing matters: interpolation is lazy, so a broken config reads fine and fails only on get() of the affected key, sometimes far from where it was loaded. To validate a config up front, read every key right after loading. raw=True and interpolation=None skip substitution entirely.

Examples

import configparser
cfg = configparser.ConfigParser()
cfg.read_string("[a]\nok = 1\nbad = %(nope)s\nrate = 5%\n")
for key in cfg["a"]:
    try:
        cfg.get("a", key)
    except configparser.InterpolationError as e:
        print(key, type(e).__name__)  # → bad InterpolationMissingOptionError / rate InterpolationSyntaxError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy