Skip to content
Latchkey

Python "configparser.NoSectionError" in CI

configparser raises NoSectionError when you request a section that is not present. A frequent cause in CI is that read() silently succeeded on a missing path, leaving an empty parser with no sections.

What this error means

A step fails with "configparser.NoSectionError: No section: 'database'" when reading a value from an .ini or .cfg file.

python
configparser.NoSectionError: No section: 'database'

Common causes

The config file was not found

ConfigParser.read(path) returns the list of files it parsed and does not raise on a missing path, so a wrong CWD leaves the parser empty.

The section name differs from what the code expects

A case or spelling mismatch between the file and the lookup.

How to fix it

Verify the file was read and the section exists

  1. Capture the return value of read() and fail if the file was not parsed.
  2. Use an absolute path or resolve relative to the module, not the CWD.
  3. Check parser.has_section(name) before accessing it.
Python
import configparser, pathlib

cfg = configparser.ConfigParser()
path = pathlib.Path(__file__).parent / "settings.ini"
if not cfg.read(path):
    raise FileNotFoundError(f"config not found: {path}")

How to prevent it

  • Always check the read() return value for the expected file.
  • Resolve config paths relative to the module, not the working directory.
  • Guard section access with has_section.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →