ConfigUpdater is a Python library designed to update INI configuration files while preserving their original formatting, comments, and structure. It offers complementary functionality to Python's standard `ConfigParser`, which primarily focuses on reading and writing new files. The library aims for minimal, targeted changes, ensuring that the ordering of sections and key-value pairs, as well as their original casing, remain intact. The current version is 3.2, and it is actively maintained as part of the PyScaffold project.
pip install configupdaterVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to read an INI file, modify an existing option, add new options (with and without comments), remove an option, and write the updated configuration back to the original file.
Always use `copy.deepcopy()` from the standard library if you need to duplicate a configuration block and modify the copy independently.
Be aware of the distinct functionality. If you require these `ConfigParser` features, process your configuration with `ConfigParser` first, then use `ConfigUpdater` for targeted structural or value changes, or handle interpolation/conversion manually.
For setting multi-line values, use the `set_values()` or `append()` methods provided by `ConfigUpdater` to handle them correctly.
Review the configuration file for any syntax issues, especially inline comments on option lines. Ensure that the INI file adheres to the expected format for `ConfigParser`. If comments within options are causing issues, try moving them to their own lines or using `ConfigUpdater`'s comment handling methods if appropriate.
Ensure that the section or option you are trying to access exists in your configuration file and that its name (including case) matches exactly. You can check for existence using `if 'section' in updater:` or `if 'option' in updater['section']:` before attempting to access it.
Before interacting with the `ConfigUpdater` object to get or set values, you must first load a configuration file or string using `updater.read('path/to/config.ini')` or `updater.read_string(ini_string)`.If you intend to use a block in multiple places or modify it independently, create a deep copy of the block using `import copy; new_block = copy.deepcopy(original_block)`. If moving a block, ensure it's detached from its current container first (though direct detachment methods are not typically exposed for end-users and `ConfigUpdater` aims to manage this internally with its `add_before`/`add_after` builders).
No dependency data recorded yet.