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
- Capture the return value of read() and fail if the file was not parsed.
- Use an absolute path or resolve relative to the module, not the CWD.
- 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
Python "KeyError" loading config in CIFix a "KeyError" raised while loading config in CI - code indexed a dict with a key that is present locally b…
Python "yaml.scanner.ScannerError" in CIFix "yaml.scanner.ScannerError" in CI - PyYAML could not tokenize a YAML file, usually a tab character, bad i…
Python "FileNotFoundError" opening a missing path in CIFix "FileNotFoundError: [Errno 2] No such file or directory" in CI - code opened a path that does not exist o…