Configparser is a Python standard library module (also available as a backport for earlier Python versions) that provides tools for handling configuration files in INI format. It allows you to read, write, and manage user-editable configuration files, supporting features like sections, key-value pairs, default values, and value interpolation. The current backport version is 7.2.0 and it's actively maintained, periodically syncing with upstream CPython changes, making its features largely consistent with recent Python standard library versions.
pip install configparserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a configuration, write it to an INI file, and then read values back using the `ConfigParser` class. It shows basic section and option access, as well as using fallback values for non-existent options.
Use `configparser.ConfigParser()` directly. The `read()` method now takes a path-like object or a list of path-like objects.
On Python versions 3.9+, always use the standard library's `configparser`. The `pip install configparser` package is primarily for older Python versions (e.g., 3.6-3.8) to get updated features, or for specific pinning scenarios on newer Pythons where the stdlib version is masked by design.
Instead of `config['section']['key']`, use `config.getint('section', 'key')`, `config.getfloat('section', 'key')`, or `config.getboolean('section', 'key')`. Boolean values are case-insensitive and recognize '1', 'yes', 'true', 'on' as `True` and '0', 'no', 'false', 'off' as `False`.Always refer to options using lowercase for consistency, or be aware that `config['Section']['Key']` and `config['Section']['key']` will access the same value (assuming 'Section' matches case-sensitively).
Access `DEFAULT` section options directly via `config.get('section', 'option', fallback=config['DEFAULT']['option'])` or treat `config['DEFAULT']` as a separate dictionary.For Python 3.9 and newer, rely on the standard library `configparser` module unless you have a specific reason to install the backport (e.g., to access features not yet in your Python version's stdlib or for version pinning). For Python versions 2.6-3.8, `pip install configparser` (an older version of the backport) is necessary to use modern `configparser` features.
Change the import statement from `import ConfigParser` or `from ConfigParser import ConfigParser` to `import configparser` or `from configparser import ConfigParser` respectively.
Ensure the section name in your code exactly matches a section in the INI file (case-sensitive by default for sections), and verify that the `config.read()` method successfully located and parsed the configuration file by checking its return value (a list of successfully read files).
Double-check the option name for typos or incorrect casing within the INI file and ensure it exists in the section you are trying to access. Options are case-insensitive by default in `configparser`.
Review the specified INI file (`your_file.ini`) for syntax errors. Common issues include missing '=' or ':' between keys and values, unquoted values with special characters, or invalid section headers. If options are intentionally without values, initialize `ConfigParser` with `allow_no_value=True`.