{"id":334,"library":"tomlkit","title":"TOML Kit","description":"TOML Kit is a style-preserving TOML library for Python, currently at version 0.14.0. It offers a parser that maintains comments, indentations, whitespace, and internal element ordering, providing an intuitive API for accessing and editing TOML files. The library is actively maintained with regular updates, as seen in its recent releases.","status":"active","version":"0.14.0","language":"python","source_language":"en","source_url":"https://github.com/sdispater/tomlkit","tags":["TOML","Python","Configuration","Library"],"install":[{"cmd":"pip install tomlkit","lang":"bash","label":"Install TOML Kit"}],"dependencies":[{"reason":"Used for HTTP requests in documentation and examples","package":"requests","optional":true},{"reason":"Employed in documentation for templating","package":"jinja2","optional":true}],"imports":[{"note":"Correct import path for the 'parse' function.","symbol":"parse","correct":"from tomlkit import parse"},{"note":"Correct import path for the 'dumps' function.","symbol":"dumps","correct":"from tomlkit import dumps"},{"note":"Correct import path for the 'table' function.","symbol":"table","correct":"from tomlkit import table"}],"quickstart":{"code":"from tomlkit import parse, dumps, table\n\ncontent = \"\"\"[table]\nfoo = \"bar\"  # String\n\"\"\"\ndoc = parse(content)\ndoc['table']['baz'] = 13\ntab = table()\ntab.add('array', [1, 2, 3])\ndoc['table2'] = tab\ndumps(doc)","lang":"python","description":"A quickstart guide demonstrating parsing, modifying, and writing TOML documents using TOML Kit."},"warnings":[{"fix":"Upgrade to TOML Kit version 0.13.0 or later.","message":"In version 0.13.0, test failures with Python 3.13.0a4 were fixed, which may affect compatibility with earlier versions of Python 3.13.","severity":"breaking","affected_versions":"0.13.0"},{"fix":"Use 'parse' instead of 'loads' for parsing TOML strings.","message":"The 'loads' function is deprecated in favor of 'parse' for parsing TOML strings.","severity":"deprecated","affected_versions":"0.13.0"},{"fix":"Use TOML Kit's API methods for modifications to maintain style preservation.","message":"When modifying TOML documents, ensure that changes are made through the provided API to preserve formatting and comments.","severity":"gotcha","affected_versions":"All"},{"fix":"It is recommended to use a virtual environment for pip operations and consider upgrading pip as suggested in the notice.","message":"The test output indicates warnings from pip regarding running as the 'root' user and an available pip update.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-05-12T13:08:03.445Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Install the tomlkit library using pip: `pip install tomlkit`","cause":"The 'tomlkit' package is not installed in your Python environment or the environment where you are running your script is not active.","error":"ModuleNotFoundError: No module named 'tomlkit'"},{"fix":"Convert the TOMLDocument to a string using `tomlkit.dumps()` or `str()` before writing it to a file:\n```python\nimport tomlkit\n\ndoc = tomlkit.document()\ndoc[\"example\"] = \"value\"\n\n# To write to a file\nwith open(\"config.toml\", \"w\") as f:\n    f.write(tomlkit.dumps(doc))\n# Or using str()\n# with open(\"config.toml\", \"w\") as f:\n#     f.write(str(doc))\n```","cause":"Unlike some other configuration libraries (e.g., json, yaml, built-in toml), tomlkit does not provide a direct `dump()` function to write a TOMLDocument to a file-like object. You need to convert the document to a string first.","error":"AttributeError: module 'tomlkit' has no attribute 'dump'"},{"fix":"Ensure that intermediate TOML tables (sections) are created as `tomlkit.table()` objects before trying to set keys within them:\n```python\nimport tomlkit\n\ndoc = tomlkit.document()\n\n# Correct way to create nested sections\ndoc[\"server\"] = tomlkit.table()\ndoc[\"server\"][\"port\"] = 8080\n\n# If you want to add keys to an existing table or create a new one within it\ndoc[\"database\"] = tomlkit.table()\ndoc[\"database\"][\"connection\"] = tomlkit.table()\ndoc[\"database\"][\"connection\"][\"url\"] = \"localhost:5432\"\n```","cause":"You are attempting to access or assign a value within a nested TOML table (section) that has not yet been explicitly created in the TOMLDocument.","error":"KeyError: 'section_name'"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":2.2,"disk_size":"18.1M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":2.2,"disk_size":"19M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":2.5,"disk_size":"20.1M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.07,"mem_mb":2.5,"disk_size":"21M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.07,"mem_mb":2.3,"disk_size":"11.9M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.07,"mem_mb":2.3,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.07,"mem_mb":2.6,"disk_size":"11.5M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.07,"mem_mb":2.4,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":2.1,"disk_size":"17.6M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":2.1,"disk_size":"18M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}