Skip to main content

tomlite

check PyPI Python License

English | 한국어

Change values in a TOML config file from Python, keeping the comments and layout the user wrote. tomlite rewrites only the lines that change, so hand-written comments, blank lines, and aligned = survive the edit.

1. Install

pip install tomlite

No dependencies. Python 3.11+. Works on Windows, macOS, and Linux.

2. Quickstart

from tomlite import TOMLEditor

doc = TOMLEditor.load("config.toml")                      # read the file, or an empty document if it is new

# add a [[user]] block only if no user named "bob" is there yet
if not doc.has_in_array("user", match_field="name", match_value="bob"):
    doc.append_to_array("user", [("name", "bob"), ("role", "guest")])

doc.set_root_key("title", "My App", only_if_absent=True)  # set title only if it is absent
doc.set_table_key("server", "debug", False)               # change [server].debug to false
doc.save("config.toml")                                   # write it back, keeping the comments and layout

Read the values back with tomllib:

import tomllib

with open("config.toml", "rb") as f:
    config = tomllib.load(f)

3. Supported formats

tomlite edits three shapes — the ones a machine-managed config file uses:

title = "My App"                 # top-level key

[server]                         # table
host = "localhost"
port = 8080
debug = true
tags = ["web", "prod"]           # values: str, int, float, bool, string array

[[user]]                         # table array, entries matched by a field
name = "alice"
role = "admin"

A construct its line scanner can't edit safely — a multi-line string or a dotted key (a.b = 1) — raises UnsupportedTOMLError instead of risking corruption; reading and dumps still work.

4. Round-trip

Any value is stored safely and reads back unchanged, even one with quotes, brackets, or backslashes. Changing a value keeps the comment on its line, an array you wrote across several lines stays multi-line, and the file's newline style (LF or CRLF) is preserved. An edit that would make the file invalid TOML is refused and the file left untouched.

5. API

Method Description
TOMLEditor.load(path) Read a file; an empty document if it does not exist.
TOMLEditor.loads(text) A document from a TOML string.
.dumps() → str The document as text.
.save(path) Write atomically, keeping an existing file's mode.
.set_root_key(key, value, *, only_if_absent=False, multiline=False) Set a top-level key. only_if_absent skips an existing value.
.unset_root_key(key) → bool Remove a top-level key.
.has_in_array(array, *, match_field, match_value) → bool Whether a [[array]] block has match_field = match_value.
.append_to_array(array, fields, *, before_table=None, multiline=False) Add a [[array]] block, optionally before a named table.
.update_in_array(array, *, match_field, match_value, field, value, multiline=False) → bool Set field in the matched block.
.remove_from_array(array, *, match_field, match_value) → bool Remove the matched block.
.set_table_key(table, key, value, *, multiline=False) Set key in [table], creating the table if needed.
.unset_table_key(table, key) → bool Remove key from [table].

Values are str, int, float, bool, or a sequence of str. Match arguments are keyword-only. multiline=True writes an array value one item per line; replacing a multi-line array with a non-empty one keeps it multi-line.

6. License

MIT

Metadata

Release files for tomlite 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tomlite 0.1.1
File Size Uploaded
tomlite-0.1.1.tar.gz 33.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tomlite 0.1.1
File Interpreter ABI Platform
tomlite-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 52.4 kB

Release files / tomlite-0.1.1.tar.gz

Download URL tomlite-0.1.1.tar.gz
Size 33.1 kB
Tags Source
SHA-256 checksum
How to use checksums
92c0f11728d09da2c53e779e2c6e2862af565b9c47e4335139641634e93ab27d
BLAKE2b-256 checksum
How to use checksums
9500129e008d2fe2801ab4f26fd7ce699810c8e21336066d1130302e2f31ddff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.

Transparency log

Release files / tomlite-0.1.1-py3-none-any.whl

Download URL tomlite-0.1.1-py3-none-any.whl
Size 19.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
46f8b13358594df846e4fdce4be8bad9132ea5ea962ca1ee6f6ad8641b6335da
BLAKE2b-256 checksum
How to use checksums
58f10a6e2ae0f9c60adf22ee05490e0d18b95f7269fbcc27c361dac26a7d9f69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page