Configparser
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.
Warnings
- breaking The `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`.
- breaking As 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.
- gotcha ConfigParser 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.
- gotcha Option names (keys) within sections are case-insensitive and are normalized to lowercase when accessed via dictionary-like syntax. However, section names *are* case-sensitive.
- gotcha The special `[DEFAULT]` section, while providing default values for other sections, is not returned by `config.sections()` or `config.has_section()`.
- gotcha The `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.
Install
-
pip install configparser
Imports
- ConfigParser
from configparser import ConfigParser
Quickstart
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}")