pyproject.toml
- Python course
- Advanced
- Lesson 73/83
- 5 min
Learn project configuration with pyproject.toml.
pyproject.toml
pyproject.toml is the configuration file of a modern Python project. The project name, version and dependencies are declared here. The format is TOML; in Python 3.12 it is read from the standard library with the tomllib module.
[project]
name = "demo"
version = "0.1.0"The file contains at least a [project] table. This table holds the package identity and requirements; pip and build tools read from it.
The [project] table
The required fields of the [project] table are name and version. The dependencies field lists the packages the project needs to run.
[project]
name = "demo"
version = "0.1.0"
dependencies = ["requests>=2.28"]name is the package name shown on PyPI; version is the semantic version. Together these three fields form the package identity used by pip.
Practice Read the project name with tomllib
Start with import tomllib. Convert the TOML string below into structured data with tomllib.loads(...) and assign it to data:
[project]
name = "demo"
version = "0.1.0"Then print the project name with print(data["project"]["name"]).
Starter code
# bring in the tomllib module
# load the pyproject content as structured data
# print the name field of the project tableSample solution
import tomllib
data = tomllib.loads('''
[project]
name = "demo"
version = "0.1.0"
''')
print(data["project"]["name"])Expected output
demo
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which table holds the project identity in pyproject.toml?
- [package]
- [build-system]
- [tool]
- [project] Correct answer
Explanation: The [project] table holds name, version and dependencies.
2 Complete the name line
[project]
____ = "demo"
[project]
name = "demo"
Explanation: The name field holds the package name.
3 Match the pyproject.toml tables
| Term | Meaning |
|---|---|
[project] |
Package identity |
[build-system] |
Build tool |
name |
Package name |
version |
Version |