{"id":1812,"library":"configupdater","title":"ConfigUpdater","description":"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.","status":"active","version":"3.2","language":"en","source_language":"en","source_url":"https://github.com/pyscaffold/configupdater/","tags":["configuration","ini","parser","updater","configparser-alternative"],"install":[{"cmd":"pip install configupdater","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"ConfigUpdater","correct":"from configupdater import ConfigUpdater"}],"quickstart":{"code":"import os\nfrom configupdater import ConfigUpdater\n\n# Create a dummy config file for demonstration\nconfig_content = '''\n[metadata]\nauthor = Ada Lovelace\nsummary = The Analytical Engine\nversion = 1.0\n\n[options]\nverbose = yes\npath = /tmp/data\n'''\n\nconfig_file_path = \"example.ini\"\nwith open(config_file_path, \"w\") as f:\n    f.write(config_content)\n\nupdater = ConfigUpdater()\nupdater.read(config_file_path)\n\n# Change an existing value\nupdater[\"metadata\"][\"author\"].value = \"Grace Hopper\"\n\n# Add a new option\nupdater[\"metadata\"][\"license\"] = \"MIT\"\n\n# Add a new option with a comment before it\n(updater[\"options\"][\"path\"].add_before\n .comment(\"  # Path to store temporary files\")\n .option(\"temp_dir\", \"/var/temp\"))\n\n# Remove an option\ndel updater[\"metadata\"][\"version\"]\n\n# Print the current state (optional)\n# print(updater)\n\n# Write the changes back to the original file\nupdater.update_file()\n\nprint(f\"Updated configuration written to {config_file_path}\")\nwith open(config_file_path, 'r') as f:\n    print(f.read())\n\n# Clean up the dummy file\nos.remove(config_file_path)","lang":"python","description":"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."},"warnings":[{"fix":"Always use `copy.deepcopy()` from the standard library if you need to duplicate a configuration block and modify the copy independently.","message":"Shallow copies of configuration blocks (sections, options, comments) are highly discouraged. Each block maintains a reference to its container, and using shallow copies for modifications can lead to unreliable and unexpected results.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"ConfigUpdater does *not* implement all features found in Python's standard `ConfigParser`. Specifically, it does not support value interpolation, propagation of parameters from the default section, value conversions, passing key/value pairs with default arguments, or a non-strict mode allowing duplicate sections and keys. Its focus is solely on minimal invasive updates.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For setting multi-line values, use the `set_values()` or `append()` methods provided by `ConfigUpdater` to handle them correctly.","message":"Direct assignment to an option's `.value` property (`updater['section']['key'].value = 'multi\\nline'`) will fail if the value contains multiple lines. Multi-line values are explicitly disallowed for direct assignment.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}