Python 3.14+configparserexception
configparser.UnnamedSectionDisabledError
exceptions
Raised when code uses configparser.UNNAMED_SECTION on a parser created without allow_unnamed_section=True.
Syntax
except configparser.UnnamedSectionDisabledError: ...
The unnamed section — keys before the first header — is off by default so that old configs parse as before. Enable it in the constructor: the flag cannot be set later, and add_section(UNNAMED_SECTION) on a regular parser raises this exception.
Examples
import configparser
try:
configparser.ConfigParser().add_section(configparser.UNNAMED_SECTION)
except configparser.UnnamedSectionDisabledError as e:
print(e) # → Support for UNNAMED_SECTION is disabled.
cfg = configparser.ConfigParser(allow_unnamed_section=True)
cfg.read_string("x = 1\n[a]\ny = 2\n")
print(cfg.get(configparser.UNNAMED_SECTION, "x")) # → 1
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy