INIParse
iniparse is an INI parser for Python designed for API compatibility with the standard library's ConfigParser. It offers enhanced functionality, including preserving the structure of INI files (order of sections & options, indentation, comments, and blank lines) when data is updated, and more convenient dotted notation access to values. The current version is 0.5.1, with infrequent releases primarily focused on Python compatibility updates.
Common errors
-
AttributeError: 'ConfigParser' object has no attribute 'readfp'
cause This error occurs in Python 3.12+ because the `readfp` method was removed from the standard `configparser.ConfigParser` class, which `iniparse` internally extends or mimics. Older `iniparse` versions have not adapted to this change.fixUpgrade `iniparse` to version 0.5.1 or newer to resolve this compatibility issue: `pip install --upgrade iniparse`. The recommended replacement is `read_file`. -
TypeError: 'ConfigParser' object is not subscriptable (when using dotted notation)
cause This error typically indicates you've accidentally imported the standard `configparser.ConfigParser` instead of `iniparse.ConfigParser`. The standard library's parser does not support dotted notation for accessing sections/options.fixEnsure your import statement is `from iniparse import ConfigParser`. If you already have `iniparse` installed, verify the import path is correct and not shadowed by `configparser`.
Warnings
- breaking Older versions of `iniparse` may encounter compatibility issues with Python 3.11 due to internal changes in `ConfigParser` or deprecated features. Version 0.5.1 specifically addresses Python 3.11 compatibility.
- breaking When running on Python 3.12 or newer, `iniparse` versions older than 0.5.1 may break due to the removal of `ConfigParser.readfp` and certain `unittest` aliases. `readfp` has been deprecated since Python 3.2 and is removed in 3.12.
- gotcha While `iniparse` is API-compatible with `ConfigParser`, its core feature is preserving INI file structure (order, comments, blank lines) upon writing. This might lead to unexpected file modifications if a user expects the default `ConfigParser` behavior of stripping these elements during write operations, which `iniparse` is specifically designed to avoid.
Install
-
pip install iniparse
Imports
- ConfigParser
from configparser import ConfigParser
from iniparse import ConfigParser
- RawConfigParser
from iniparse import RawConfigParser
- SafeConfigParser
from iniparse import SafeConfigParser
Quickstart
import os
from iniparse import ConfigParser
# Create a dummy INI file
ini_content = """
[mysection]
key = value
another_key = another value
[database]
host = localhost
port = 5432
user = dbuser
"""
config_file_path = 'config.ini'
with open(config_file_path, 'w') as f:
f.write(ini_content)
# Initialize and read the INI file
cfg = ConfigParser()
cfg.read(config_file_path)
# Access values using dotted notation or dictionary-like syntax
print(f"Value for mysection.key: {cfg.mysection.key}")
print(f"Value for database.port: {cfg['database']['port']}")
# Modify a value
cfg.mysection.key = 'new_value'
print(f"Modified value for mysection.key: {cfg.mysection.key}")
# Add a new option
cfg.database.password = os.environ.get('DB_PASSWORD', 'default_password')
print(f"Added database.password: {cfg.database.password}")
# Write changes to a new INI file, preserving structure
new_config_file_path = 'new_config.ini'
with open(new_config_file_path, 'w') as f:
cfg.write(f)
print(f"Configuration written to {new_config_file_path}")
# Clean up dummy files
os.remove(config_file_path)
os.remove(new_config_file_path)