iniconfig

2.3.0 · active · verified Fri Mar 27

iniconfig is a minimal, read-only INI-file parser maintained under the pytest-dev umbrella. It preserves section and key order, supports multi-line values, strips `#` comments from structure (not inline values in <2.3), raises `ParseError` with accurate line numbers, and rejects duplicate section names. Current stable version is 2.3.0, released 2024. Release cadence is irregular but healthy, driven by pytest ecosystem needs.

Warnings

Install

Imports

Quickstart

Parse an INI config from a string, access values, use .get() for safe access with a default and optional converter.

import iniconfig

INI_TEXT = """
[database]
host = localhost
port = 5432
names = foo,bar

[app]
debug = true
"""

# Parse from a string (no file needed)
ini = iniconfig.IniConfig("-", data=INI_TEXT)

# Direct access — raises KeyError if key/section missing
host = ini["database"]["host"]          # 'localhost'

# Safe access with default + optional converter
port = ini.get("database", "port", 5432, int)          # 5432 (int)
names = ini.get("database", "names", [], lambda x: x.split(","))  # ['foo', 'bar']
missing = ini.get("database", "user", "root")           # 'root'

# Membership check
assert "database" in ini
assert "ghost" not in ini

# Iterate sections
for section in ini:
    print(section.name, list(section.items()))

# Catch parse errors
try:
    bad = iniconfig.IniConfig("-", data="[dup]\n[dup]\n")
except iniconfig.ParseError as exc:
    print(f"Parse failed: {exc}")

view raw JSON →