configparser2
raw JSON → 4.0.0 verified Fri May 01 auth: no python deprecated
Backport of Python 3.5's configparser to older Python versions (2.6-3.5). Version 4.0.0 is the final release. Provides the updated ConfigParser with features like mapping protocol support, ordered dicts, and interpolation depth checking. Consider using Python 3's standard library configparser instead.
pip install configparser2==4.0.0 Common errors
error ImportError: No module named configparser2 ↓
cause Package not installed or pip installed in wrong environment.
fix
Run
pip install configparser2==4.0.0 in the same Python environment where your script runs. error AttributeError: 'ConfigParser' object has no attribute 'read_file' ↓
cause Accessing method only available in Python 3's configparser; configparser2 may not have all modern methods.
fix
Use
read() or read_string() instead, or check docs for available methods. error ConfigParser.DuplicateSectionError: Key '...' with value '...' already exists ↓
cause The library version 4.0.0 defaults to `strict=True`, which raises on duplicate keys.
fix
Initialize with
ConfigParser(strict=False) to allow duplicate keys (though not recommended). Warnings
deprecated configparser2 is no longer maintained. Use Python 3's standard library configparser instead, or the backport 'configparser' package if stuck on Python 2. ↓
fix Switch to `import configparser` (stdlib backport) or upgrade to Python 3.
breaking In version 4.0.0, the `ConfigParser` class may have different default behavior for interpolation compared to older versions. Specifically, `strict` parameter defaults may break existing config files with duplicate keys. ↓
fix Set `strict=False` when creating ConfigParser if you rely on duplicate key behavior: `ConfigParser(strict=False)`.
gotcha Installing configparser2 will uninstall the standard library backport `configparser` due to package conflicts. This can break other tools that depend on the stdlib backport. ↓
fix Use a virtual environment to isolate projects, or avoid installing both packages simultaneously.
Imports
- ConfigParser wrong
from configparser import ConfigParsercorrectfrom configparser2 import ConfigParser - SafeConfigParser wrong
from ConfigParser import SafeConfigParsercorrectfrom configparser2 import SafeConfigParser
Quickstart
from configparser2 import ConfigParser
import os
config = ConfigParser()
config.read_string(os.environ.get('CONFIG_STRING', '[DEFAULT]\nkey=value\n'))
print(dict(config['DEFAULT']))