{"id":4069,"library":"kconfiglib","title":"Kconfiglib","description":"Kconfiglib is a Python library (v14.1.0) for scripting and extracting information from Kconfig configuration systems. It can load, analyze, and output kernel-style configurations, programmatically get and set symbol values, read and write .config and defconfig files, and generate C headers. Highly compatible with the C Kconfig tools, it is a single-file library primarily used in projects like Zephyr and ACRN, running on Python 2.7 and 3.2+ with releases following semantic versioning.","status":"active","version":"14.1.0","language":"en","source_language":"en","source_url":"https://github.com/ulfalizer/Kconfiglib","tags":["configuration","kconfig","embedded","build-tools","linux-kernel","devops"],"install":[{"cmd":"pip install kconfiglib","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"The library is written in Python.","package":"python","optional":false},{"reason":"Required for the terminal menuconfig interface on Windows.","package":"windows-curses","optional":true}],"imports":[{"symbol":"Kconfig","correct":"from kconfiglib import Kconfig"},{"symbol":"Symbol","correct":"from kconfiglib import Symbol"},{"note":"Kconfiglib 2.x (current) replaced functions with properties for accessing symbol values and other object attributes. The Kconfiglib 1.x API is not backwards-compatible.","wrong":"kconfig.sym_value(\"FOO\")","symbol":"Kconfiglib 1.x API"}],"quickstart":{"code":"import os\nfrom kconfiglib import Kconfig\n\n# Create a dummy Kconfig file for demonstration\nkconfig_content = \"\"\"\nmenu \"My Application Configuration\"\n\nconfig MY_FEATURE_ENABLED\n    bool \"Enable My Feature\"\n    default y\n\nconfig MY_STRING_SETTING\n    string \"My String Setting\"\n    default \"default_value\"\n    depends on MY_FEATURE_ENABLED\n\nendmenu\n\"\"\"\nwith open(\"Kconfig_example\", \"w\") as f:\n    f.write(kconfig_content)\n\n# Load the Kconfig file\nkconf = Kconfig(\"Kconfig_example\")\n\n# Access a symbol by its name\nfeature_symbol = kconf.syms[\"MY_FEATURE_ENABLED\"]\nstring_symbol = kconf.syms[\"MY_STRING_SETTING\"]\n\n# Print symbol information\nprint(f\"Symbol: {feature_symbol.name}, Prompt: '{feature_symbol.prompt}', Value: {feature_symbol.str_value}\")\nprint(f\"Symbol: {string_symbol.name}, Prompt: '{string_symbol.prompt}', Value: {string_symbol.str_value}\")\n\n# Set a symbol value\nfeature_symbol.set_value(0) # Set to 'n'\nprint(f\"Updated {feature_symbol.name} value: {feature_symbol.str_value}\")\n\n# Try to set string_symbol, which now depends on MY_FEATURE_ENABLED being 'y'\n# This will fail silently or yield an 'n' if the dependency is not met\nstring_symbol.set_value(\"new_value\")\nprint(f\"Updated {string_symbol.name} value: {string_symbol.str_value} (expected empty if feature disabled)\")\n\n# Re-enable MY_FEATURE_ENABLED and set string_symbol\nfeature_symbol.set_value(2) # Set to 'y'\nstring_symbol.set_value(\"new_value_2\")\nprint(f\"Updated {string_symbol.name} value: {string_symbol.str_value} (expected 'new_value_2')\")\n\n# Clean up the dummy Kconfig file\nos.remove(\"Kconfig_example\")\n","lang":"python","description":"This quickstart demonstrates how to load a Kconfig file, access symbols, query their properties, and programmatically set their values. It showcases how symbol values are affected by their dependencies."},"warnings":[{"fix":"Refer to the Kconfiglib 2.x documentation and `kconfiglib-2-changes.txt` for migration details. Update code to use properties (e.g., `symbol.str_value` instead of `symbol.get_value()`).","message":"Kconfiglib 2.x is not backwards-compatible with Kconfiglib 1.x. The API changed significantly, replacing functions with properties for accessing symbol values and menu structures. Code written for Kconfiglib 1.x will break.","severity":"breaking","affected_versions":"< 2.0.0"},{"fix":"Manually install `windows-curses` on Windows if you intend to use the terminal `menuconfig`: `pip install windows-curses`.","message":"Starting with Kconfiglib 13.0.0, the `windows-curses` package is no longer automatically installed on Windows. This dependency is required for the terminal `menuconfig` interface to function.","severity":"breaking","affected_versions":">= 13.0.0"},{"fix":"Update Kconfig files to use `$(FOO)` for environment variable references.","message":"The old syntax for referencing environment variables as `$FOO` is deprecated. The new syntax is `$(FOO)`. While the old syntax is currently supported for compatibility with older Linux kernels, it might be removed in a future major version.","severity":"deprecated","affected_versions":"All versions"},{"fix":"Avoid direct assignments to hidden symbols in `.config` files. Configure their controlling symbols instead.","message":"Assignments to hidden (promptless) symbols in configuration files are ignored. These symbols derive their values indirectly from other symbols via `default` or `select`. It's a common mistake to assume such assignments in a `.config` file will be respected when read back by Kconfiglib.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consider using `depends on` instead of `select` where appropriate to make dependencies explicit and local. Avoid long chains of `select` statements.","message":"Overusing `select` statements in Kconfig can lead to complex and hard-to-debug dependency issues. Changes to a selected symbol's dependencies often require propagating those changes to all symbols that select it, which is easily overlooked.","severity":"gotcha","affected_versions":"All versions"},{"fix":"This is an active issue (as of the last check). Check Kconfiglib's GitHub issues for potential patches or workarounds. Ensure your Kconfiglib version is up-to-date, as this might be resolved in newer releases.","message":"Kconfiglib may fail to correctly parse `Kconfig.include` files from Linux kernel versions 5.12.1 and newer, leading to `ValueError: invalid literal for int() with base 10: 'error-if'` during parsing.","severity":"gotcha","affected_versions":"All versions (when parsing kernel >= 5.12.1 Kconfig)"},{"fix":"Ensure all hex values in Kconfig files adhere to the `0x...` or `0X...` format.","message":"Within Kconfig files, all hexadecimal literals must be prefixed with `0x` or `0X` to be correctly distinguished from symbol references by the parser.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}