14.10. TOML Use Case
14.10.1. SetUp
>>> DATA = b"""
... [project]
... name = "myproject"
... version = "1.0.0"
... requires-python = ">=3.15"
... authors = [{name = "Alice", email = "alice@example.com"}]
... readme = "README.md"
... license = {file = "LICENSE"}
... keywords = ["myproject", "myapp", "python"]
... urls.homepage = "https://github.com/myusername/myproject"
... urls.repository = "https://github.com/myusername/myproject.git"
... urls.documentation = "https://github.com/myusername/myproject"
... urls.changelog = "https://github.com/myusername/myproject/releases"
... urls.bugtracker = "https://github.com/myusername/myproject/issues"
... dependencies = [
... "pip==26.2.1",
... "tzdata==2026.5"]
... """
>>>
>>> with open('/tmp/myfile.toml', mode='wb') as file:
... file.write(DATA)
605
14.10.2. Load TOML File
>>> import tomllib
>>>
>>> with open('/tmp/myfile.toml', mode='rb') as file:
... data = tomllib.load(file)
14.10.3. Use
>>> data['project']
{'name': 'myproject',
'version': '1.0.0',
'requires-python': '>=3.15',
'authors': [{'name': 'Alice', 'email': 'alice@example.com'}],
'readme': 'README.md',
'license': {'file': 'LICENSE'},
'keywords': ['myproject', 'myapp', 'python'],
'urls': {'homepage': 'https://github.com/myusername/myproject',
'repository': 'https://github.com/myusername/myproject.git',
'documentation': 'https://github.com/myusername/myproject',
'changelog': 'https://github.com/myusername/myproject/releases',
'bugtracker': 'https://github.com/myusername/myproject/issues'},
'dependencies': ['pip==26.2.1', 'tzdata==2026.5']}
>>> data['project']['name']
'myproject'
>>> data['project']['version']
'1.0.0'
>>> data['project']['requires-python']
'>=3.15'
>>> data['project']['authors']
[{'name': 'Alice', 'email': 'alice@example.com'}]
>>> data['project']['dependencies']
['pip==26.2.1', 'tzdata==2026.5']
>>> data['project']['urls']
{'homepage': 'https://github.com/myusername/myproject',
'repository': 'https://github.com/myusername/myproject.git',
'documentation': 'https://github.com/myusername/myproject',
'changelog': 'https://github.com/myusername/myproject/releases',
'bugtracker': 'https://github.com/myusername/myproject/issues'}