Registry / serialization / configobj

configobj

JSON →
library5.0.9pypypi✓ verified 30d ago

ConfigObj is a Python library for reading, writing, and validating configuration files. It supports a syntax similar to .ini files but adds features like nested sections, list handling, and comprehensive validation. It is currently at version 5.0.9 and is actively maintained with infrequent but important updates, often addressing security concerns or Python version compatibility.

pip install configobj
INSTALL
IMPORT
SIG · CONFIGOBJ
C
configobj
serializationpythonv5.0.9
Install
1.5s avg
Import
13ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v5.0.9 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 18MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.014s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ConfigObj
✓ from configobj import ConfigObj
Validate
✓ from configobj import Validate
Used for performing validation against a configspec.

This example demonstrates how to create a ConfigObj instance, populate it with data (including nested sections and lists), write it to a file, and then read it back. It concludes with cleanup of the created file.

from configobj import ConfigObj import os # 1. Create and write a config file config = ConfigObj() config.filename = "example.ini" config['Section One'] = { 'key1': 'value1', 'key2': ['itemA', 'itemB', 'itemC'] } config['Section Two'] = {} config['Section Two']['nested_key'] = 'nested value' config.write() print(f"Config file 'example.ini' created.\n") # 2. Read the config file read_config = ConfigObj('example.ini') print(f"Read config content:\n{read_config.dict()}\n") print(f"Value from Section One, Key1: {read_config['Section One']['key1']}") print(f"List from Section One, Key2: {read_config['Section One']['key2']}") # 3. Clean up the created file os.remove("example.ini") print("example.ini removed.")
Debug
Known issues
breakingPython 2 support was entirely dropped in version 5.0.9, along with associated compatibility code. The minimum required Python version is now 3.7.
fix
Upgrade to `configobj>=5.0.9` and ensure your environment runs Python 3.7 or newer.
affects: <=5.0.8
breakingVersion 5.0.9 addresses CVE-2023-26112, a Regular Expression Denial of Service (ReDoS) vulnerability. Older versions are susceptible to this vulnerability.
fix
Upgrade to `configobj>=5.0.9` immediately to patch the ReDoS vulnerability.
affects: <=5.0.8
gotchaValidation is not automatic. To enable validation, you must explicitly pass a `configspec` (a file path or a `ConfigObj` instance representing the specification) and then use a `Validate` object with `config.validate(validator)`.
fix
Always define a `configspec` and an instance of `configobj.Validate` to use with the `config.validate()` method, e.g., `config = ConfigObj('my.ini', configspec='my.spec'); config.validate(Validate())`.
affects: All versions
gotchaWhen working with lists, ConfigObj automatically converts comma-separated strings to lists upon writing. However, reading them back without a `configspec` will treat elements as strings. For robust type handling, especially with non-string list items, a `configspec` is essential.
fix
Define a `configspec` with `list` types (e.g., `key = list`) for keys expected to hold lists. When reading, instantiate `ConfigObj` with the `configspec` to ensure correct type conversion.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'configobj'
The `configobj` library is not installed in the Python environment being used, or there is a Python version mismatch (e.g., attempting to use it with a Python 2 environment, which `configobj` 5.0.9 no longer supports).
fix
Install the library using pip: `pip install configobj`. If using a specific Python version or virtual environment, ensure `pip` for that environment is used (e.g., `python3 -m pip install configobj`).
ParseError
The configuration file contains a syntax error, such as a malformed `key = value` line, an invalid section marker, or using a colon (`:`) instead of an equals sign (`=`) as a key-value separator, which `configobj` does not support.
fix
Review and correct the syntax in the configuration file to adhere to `configobj`'s specification, ensuring proper `key = value` format and valid section headers.
KeyError
An attempt was made to access a section or an option within a section using dictionary-like access (`config['section']['key']`) that does not exist in the loaded configuration, or `restore_default(key)` was called for a key that has no default value defined in the `configspec`.
fix
Ensure the key or section path is correct and exists in your configuration. Use `config.get('section', {}).get('key', default_value)` for safe access with a fallback, or verify that the `configspec` properly defines default values for keys you intend to restore.
IOError: [Errno 2] No such file or directory: 'your_config_file.conf'
The file path provided to the `ConfigObj` constructor does not point to an existing file, and the `file_error` parameter is set to `True` (or implicitly `True` depending on other constructor arguments like `create_empty`).
fix
Provide the correct and absolute path to an existing configuration file. Alternatively, set `file_error=False` and `create_empty=True` in the `ConfigObj` constructor if you intend for `configobj` to create an empty file if it doesn't exist.
validate.ValidateError (or subclasses like VdtValueError, VdtTypeError)
During the validation process using `config.validate(validator)`, one or more values in the configuration file failed to meet the criteria (e.g., type, range, allowed options) specified in the associated `configspec`.
fix
Inspect the `configspec` and the corresponding values in your configuration file. Correct the data in the config file to match the validation rules, or adjust the `configspec` if the rules are unintentionally restrictive. Using `config.validate(validator, preserve_errors=True)` and `flatten_errors()` can help get detailed information about specific validation failures.
Upgrade
Version history
5.0.9latest on PyPI · released Nov 26, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
29 hits · last 30 days
node
26
OpenAI (training)
2
Resources
configobj — pip install configobj · libregistry