TOML Kit
raw JSON → 0.14.0 verified Tue May 12 auth: no python install: verified quickstart: verified
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.
pip install tomlkit Common errors
error ModuleNotFoundError: No module named 'tomlkit' ↓
cause The 'tomlkit' package is not installed in your Python environment or the environment where you are running your script is not active.
fix
Install the tomlkit library using pip:
pip install tomlkit error AttributeError: module 'tomlkit' has no attribute 'dump' ↓
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.
fix
Convert the TOMLDocument to a string using
tomlkit.dumps() or str() before writing it to a file:
import tomlkit
doc = tomlkit.document()
doc["example"] = "value"
# To write to a file
with open("config.toml", "w") as f:
f.write(tomlkit.dumps(doc))
# Or using str()
# with open("config.toml", "w") as f:
# f.write(str(doc)) error KeyError: 'section_name' ↓
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.
fix
Ensure that intermediate TOML tables (sections) are created as
tomlkit.table() objects before trying to set keys within them:
import tomlkit
doc = tomlkit.document()
# Correct way to create nested sections
doc["server"] = tomlkit.table()
doc["server"]["port"] = 8080
# If you want to add keys to an existing table or create a new one within it
doc["database"] = tomlkit.table()
doc["database"]["connection"] = tomlkit.table()
doc["database"]["connection"]["url"] = "localhost:5432" Warnings
breaking 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. ↓
fix Upgrade to TOML Kit version 0.13.0 or later.
deprecated The 'loads' function is deprecated in favor of 'parse' for parsing TOML strings. ↓
fix Use 'parse' instead of 'loads' for parsing TOML strings.
gotcha When modifying TOML documents, ensure that changes are made through the provided API to preserve formatting and comments. ↓
fix Use TOML Kit's API methods for modifications to maintain style preservation.
gotcha The test output indicates warnings from pip regarding running as the 'root' user and an available pip update. ↓
fix It is recommended to use a virtual environment for pip operations and consider upgrading pip as suggested in the notice.
Install compatibility verified last tested: 2026-05-12
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.05s 18.1M
3.10 slim (glibc) - - 0.03s 19M
3.11 alpine (musl) - - 0.09s 20.1M
3.11 slim (glibc) - - 0.07s 21M
3.12 alpine (musl) - - 0.07s 11.9M
3.12 slim (glibc) - - 0.07s 12M
3.13 alpine (musl) - - 0.07s 11.5M
3.13 slim (glibc) - - 0.07s 12M
3.9 alpine (musl) - - 0.04s 17.6M
3.9 slim (glibc) - - 0.04s 18M
Imports
- parse
from tomlkit import parse - dumps
from tomlkit import dumps - table
from tomlkit import table
Quickstart verified last tested: 2026-04-23
from tomlkit import parse, dumps, table
content = """[table]
foo = "bar" # String
"""
doc = parse(content)
doc['table']['baz'] = 13
tab = table()
tab.add('array', [1, 2, 3])
doc['table2'] = tab
dumps(doc)