On this page
tomllib
Prev: tokenize | Up: stdlib_module | Next: trace
Rel: csv, configparser, netrc, plistlib
Ref: docs.python.org — tomllib
Tags: #public #python3 #stdlib
Import
import tomllib
Overview
Read-only TOML 1.0 parser built into Python 3.11+ (loads() for strings, load() for binary files). Reach for tomllib to parse pyproject.toml and application config files with zero dependencies; it is strictly read-only (no TOML writer is included in the stdlib).
Quick Start
import tomllib
raw = '''
[project]
name = "demo"
version = "0.1.0"
tags = ["python", "toml"]
'''
data = tomllib.loads(raw)
assert data["project"]["name"] == "demo"
assert data["project"]["tags"] == ["python", "toml"]
Implementation Walkthrough
1. Loading a pyproject.toml File in Binary Mode
tomllib.load() requires a binary file handle ("rb") because the TOML 1.0 specification mandates UTF-8 decoding:
import tomllib
from pathlib import Path
def read_toml(path: Path) -> dict[str, object]:
try:
with path.open("rb") as fh:
return tomllib.load(fh)
except tomllib.TOMLDecodeError as exc:
raise ValueError(f"Invalid TOML in {path}: {exc}") from exc
2. Preserving Exact Decimal Precision with parse_float
from decimal import Decimal
import tomllib
cfg = tomllib.loads("rate = 0.075\n", parse_float=Decimal)
assert cfg["rate"] == Decimal("0.075")
Gotchas & Boundaries
tomllib.load()requires"rb", not"r": Passing a text-mode file raisesTypeError: File must be opened in binary mode.- Read-only by design:
tomllibhas nodump()/dumps(); generate simple TOML templates manually or use a dedicated TOML writer if you need round-trip editing. - Dates and times parse as
datetime/date/timeobjects: Unlike JSON, TOML has native temporal types that deserialize directly intodatetimeinstances.
Common Functions
- tomllib.load() —
load(fp: 'IO[bytes]', /, *, parse_float: 'ParseFloat' = <class 'float'>) -> 'dict[str, Any]'— Parse TOML from a binary file object. - tomllib.loads() —
loads(s: 'str', /, *, parse_float: 'ParseFloat' = <class 'float'>) -> 'dict[str, Any]'— Parse TOML from a string.
Classes & Objects
- tomllib.TOMLDecodeError — An error raised if a document is not valid TOML. (Methods:
.add_note(),.with_traceback())
Linked from
- tuts/python3
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/configparser
- tuts/python3/stdlib_module/csv
- tuts/python3/stdlib_module/json
- tuts/python3/stdlib_module/netrc
- tuts/python3/stdlib_module/plistlib
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/tokenize
- tuts/python3/stdlib_module/tomllib/load
- tuts/python3/stdlib_module/tomllib/loads
- tuts/python3/stdlib_module/tomllib/TOMLDecodeError
- tuts/python3/stdlib_module/trace
- tuts/python3/tutorials/popular_application_domains/automation_devops_and_web_scraping
Related notes
- tuts/python3/stdlib_module/configparser Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/csv Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/netrc Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/plistlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tokenize Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tomllib/load Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tomllib/loads Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tomllib/TOMLDecodeError Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/trace Linked here; links to this page; shared tags: #stdlib