all Python 3configparserexception
configparser.NoSectionError
exceptions
Raised when the requested section does not exist: get(), set(), options() and similar parser methods throw it.
Syntax
except configparser.NoSectionError as e: e.section
Accessing cfg['section'] raises KeyError instead. Section names are case-sensitive, unlike keys: [DB] and [db] are different sections. get(..., fallback=...) covers a missing section as well as a missing option; has_section() checks in advance. [DEFAULT] is not counted as a section and is absent from sections().
Examples
import configparser
cfg = configparser.ConfigParser()
cfg.read_string("[DB]\nport = 5432\n")
try:
cfg.get("db", "port")
except configparser.NoSectionError as e:
print(e) # → No section: 'db' — регистр секции важен
print(cfg.get("db", "port", fallback="5432")) # → 5432
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy