Registry / serialization / libconf

libconf

JSON →
library2.0.1pypypi✓ verified 22d ago

libconf is a pure-Python reader/writer for configuration files in libconfig format, commonly used in C/C++ projects. Its interface is designed to be similar to Python's standard `json` module, providing `load()`, `loads()`, `dump()`, and `dumps()` methods. The library is currently at version 2.0.1 and appears to be actively maintained.

pip install libconf
INSTALL
IMPORT
SIG · LIBCONF
L
libconf
serializationpythonv2.0.1
Install
1.6s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.0.1 · 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.103.95 runs
installs and imports cleanly · install 0.0s · import 0.006s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

libconf
import libconf
LibconfInt64
from libconf import LibconfInt64
Used for fine-grained control over integer output formatting.
LibconfList
from libconf import LibconfList
Used for fine-grained control over list output formatting.
LibconfArray
from libconf import LibconfArray
Used for fine-grained control over array output formatting.

This quickstart demonstrates how to load a libconfig string into a Python object, access its elements using attribute or dictionary-style access, modify values, and then dump the modified configuration back into a libconfig formatted string. It also shows the use of `LibconfInt64` for explicit control over integer suffixes. For file operations, `libconf.load()` and `libconf.dump()` are available, similar to the `json` module.

import libconf import io # Example libconfig string config_string = """ version = 2; window : { title = "My Application"; position = { x = 10; y = 20; w = 800; h = 600; }; }; capabilities = ( true, 1234567890123456789L, [1, 2, 3] ); """ # Load configuration from a string config = libconf.loads(config_string) print("Loaded Config:", config) print("Window Title:", config.window.title) # Modify and dump configuration back to a string config.window.title = "Updated App Title" config.capabilities = ('new_list_item', 42) # Demonstrate LibconfInt64 for explicit long suffix from libconf import LibconfInt64 config['large_number_forced_L'] = LibconfInt64(123) dumped_string = libconf.dumps(config, indent=2) print("\nDumped Config:\n", dumped_string)
Debug
Known issues
breakingStarting with version 2.0.0, `libconf` introduced output validation for `dump()` and `dumps()`. These functions will now raise exceptions if attempting to dump data that cannot be correctly read by a standard C libconfig implementation.
fix
Review data structures being dumped for compatibility with the libconfig specification. Ensure that values and structures adhere to libconfig's rules to avoid `libconf.ConfigError` or similar exceptions.
affects: >=2.0.0
gotchaPython integer values are dumped without an 'L' suffix if they fit within a C/C++ 32-bit integer range. For larger Python integers, an 'L' suffix is automatically appended. If you need to force an 'L' suffix for a number within the 32-bit range (e.g., for explicit 64-bit representation in libconfig), wrap the integer with `libconf.LibconfInt64`.
fix
To explicitly dump an integer as a 64-bit value with an 'L' suffix, use `config_data['key'] = LibconfInt64(your_int_value)` before dumping.
affects: All
gotchalibconfig distinguishes between 'lists' (enclosed by parentheses `()`, arbitrary values) and 'arrays' (enclosed by brackets `[]`, scalar values of the same type). `libconf` maps libconfig `()`-lists to Python `tuples` and libconfig `[]`-arrays to Python `lists`. This distinction is important when round-tripping data or ensuring compatibility with C libconfig.
fix
When reading, be aware that libconfig lists will become Python tuples. When writing, use Python tuples for libconfig lists and Python lists for libconfig arrays, keeping in mind the type homogeneity requirement for arrays.
affects: All
gotchaIt is strongly recommended to use Unicode objects (Python 3 `str`) for all input strings or streams passed to `load()` and `loads()`, and for any strings within data structures passed to `dump()` and `dumps()`. `libconf` handles standard escape sequences (`\n`, `\r`, `\t`, `\xNN`).
fix
Always ensure strings are Unicode/`str` type in Python 3. If dealing with byte strings or specific encodings, decode them to Unicode before passing to `libconf` functions.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'libconf'
The 'libconf' package is not installed in the current Python environment.
fix
Install the library using pip: `pip install libconf`
libconf.exceptions.ConfigParseError: Syntax error
The input configuration file or string contains syntax errors and does not conform to the libconfig format specification.
fix
Review the configuration file or string for syntax errors, such as missing semicolons, incorrect grouping, or invalid characters, and correct them according to the libconfig format.
TypeError: libconfig arrays must contain scalars of the same type
Attempting to dump a Python list (which `libconf` maps to a `libconfig` array by default) that contains non-scalar values (e.g., dictionaries, other lists/tuples) or elements of mixed data types, which violates `libconfig` array limitations.
fix
If the list contains mixed types or non-scalar elements, wrap it with `libconf.LibconfList` to ensure it's dumped as a `libconfig` list (enclosed in parentheses). If it should be a `libconfig` array, ensure all elements are scalar and of the same type. For explicitly forcing `int64`, use `libconf.LibconfInt64`.
Upgrade
Version history
2.0.1latest on PyPI · released Nov 21, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
6
Amazon
1
Resources
libconf — pip install libconf · libregistry