Codectionary / Developer documentation / Python

Reading TOML Configuration

tomllib reads TOML into Python dictionaries and typed scalar values. It is included from Python 3.11 and does not write TOML. Run as a Python 3.11+ script.

Syntax

tomllib.loads(text); tomllib.load(binary_file)

Examples

Reading TOML Configuration

Run as a Python 3.11+ script.

import tomllib
config = tomllib.loads('[server]\nport = 8080\nenabled = true\n')
print(config["server"]["port"] + 1)
print(config["server"]["enabled"])
  1. Expected output: 8081 True
  2. The parser produces an integer and a boolean directly. Real files passed to load must be opened in binary mode.

Best practices

  • Validate required keys and application-specific ranges after parsing.
  • Use a dedicated TOML writer if configuration must be edited.

Runtime & versions

Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Python language reference

Specifications & further reading