Registry / serialization / configparser

configparser

JSON →
library7.2.0pypypi✓ verified 30d ago

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 configparser
INSTALL
IMPORT
SIG · CONFIGPARSER
C
configparser
serializationpythonv7.2.0
Install
1.5s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v7.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.004s · 17.9MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ConfigParser
✓ from configparser import ConfigParser
✗ import ConfigParser
The module was renamed from `ConfigParser` to `configparser` in Python 3. `import configparser` followed by `configparser.ConfigParser()` is also common and correct.

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.

import configparser import os # Create a configuration file programmatically config = configparser.ConfigParser() config['DEFAULT'] = { 'ServerAliveInterval': '45', 'Compression': 'yes', 'ForwardX11': 'yes' } config['production.server'] = { 'User': 'prod_user', 'Port': '50022', 'ForwardX11': 'no' } config_file_path = 'example.ini' with open(config_file_path, 'w') as configfile: config.write(configfile) print(f"Configuration written to {config_file_path}") # Read the configuration file read_config = configparser.ConfigParser() read_config.read(config_file_path) # Access values using dictionary-like syntax print(f"Compression for DEFAULT: {read_config['DEFAULT']['Compression']}") print(f"User for production.server: {read_config['production.server']['User']}") # Get a value with fallback port = read_config.get('development.server', 'Port', fallback='22') print(f"Port for development.server (with fallback): {port}") # Clean up the created file os.remove(config_file_path) print(f"Cleaned up {config_file_path}")
Debug
Known issues
breakingThe `SafeConfigParser` class and the `filename` parameter in `read()` were removed in version 6.0.0. The main `ConfigParser` class now incorporates the features previously found in `SafeConfigParser`.
fix
Use `configparser.ConfigParser()` directly. The `read()` method now takes a path-like object or a list of path-like objects.
affects: >=6.0.0
breakingAs of version 7.0.0, the `configparser` top-level name from the backport package is removed for Python versions where `configparser` is already in the standard library (Python 3.9+). This ensures consistency with the standard library module.
fix
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.
affects: >=7.0.0
gotchaConfigParser treats all values as strings by default. To retrieve values as specific data types (integers, floats, booleans), use the dedicated `getint()`, `getfloat()`, and `getboolean()` methods.
fix
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`.
affects: All
gotchaOption names (keys) within sections are case-insensitive and are normalized to lowercase when accessed via dictionary-like syntax. However, section names *are* case-sensitive.
fix
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).
affects: All
gotchaThe special `[DEFAULT]` section, while providing default values for other sections, is not returned by `config.sections()` or `config.has_section()`.
fix
Access `DEFAULT` section options directly via `config.get('section', 'option', fallback=config['DEFAULT']['option'])` or treat `config['DEFAULT']` as a separate dictionary.
affects: All
gotchaThe `configparser` package is a backport. While `pip install configparser` works on Python >= 3.9 (as per its `requires_python` metadata), the module `configparser` is built into the standard library for all Python 3.x versions. Installing the backport on Python 3.9+ is often redundant unless specific backport features or fixes are needed that aren't yet in the stdlib of that particular Python version.
fix
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.
affects: All, particularly Python >= 3.9
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'ConfigParser'
This error occurs when attempting to import the `configparser` module using its old Python 2 naming convention (`ConfigParser`) in a Python 3 environment.
fix
Change the import statement from `import ConfigParser` or `from ConfigParser import ConfigParser` to `import configparser` or `from configparser import ConfigParser` respectively.
configparser.NoSectionError: No section: 'your_section_name'
This error indicates that the requested section does not exist in the configuration file, or the configuration file itself was not successfully read by `configparser`.
fix
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).
configparser.NoOptionError: No option 'your_option'
This error means that the requested option (key) could not be found within the specified section in the configuration file.
fix
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`.
configparser.ParsingError: Source contains parsing errors: 'your_file.ini'
This error is raised when the configuration file has syntax issues, such as missing values for keys, incorrect delimiters, or malformed sections.
fix
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`.
Upgrade
Version history
7.2.0latest on PyPI · released Mar 8, 2025
Audit
Dependencies
PythonrequiredRequired by the latest version of the backport library.
Agent activity
30 hits · last 30 days
node
24
Bingbot
1
OpenAI (training)
1
Resources
configparser — pip install configparser · libregistry