Python Glossary Русский

Python 3.11+tomllibterm

tomllib

module

Reads TOML configuration files, such as pyproject.toml, into plain Python dictionaries.

Syntax

import tomllib

tomllib only reads TOML: it cannot write, which needs a third-party package such as tomli-w. The file must be opened in binary mode — tomllib.load(open(path, "rb")); text mode raises TypeError. Unlike configparser, TOML keeps real types — numbers, lists, dates — rather than strings only, and unlike json it allows comments.

Examples

import tomllib
data = tomllib.loads('port = 8080\ntags = ["a", "b"]\n[db]\nhost = "localhost"\n')
print(data["port"] + 1, data["tags"], data["db"]["host"])  # → 8081 ['a', 'b'] localhost

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy