Declare Project Metadata in pyproject.toml
Core idea
The [project] table declares distribution metadata: name, version, requires-python and dependencies. [build-system] declares build requirements separately. Dependency strings describe requirements, not necessarily the exact resolved environment. tomllib parses TOML on Python 3.11+ but does not validate packaging semantics.
import tomllibtext = """[project]name = "example-parcel"version = "0.1.0"requires-python = ">=3.11"dependencies = []"""p = tomllib.loads(text)['project']print(p['name'], p['version'])The project name and version describe the distribution.
Expected output:
example-parcel 0.1.0Separate optional dependencies
import tomllibp = tomllib.loads("""[project]name = "example-parcel"version = "0.1.0"dependencies = [][project.optional-dependencies]cli = ["rich>=13"]""")['project']print(p['dependencies'])print(p['optional-dependencies']['cli'])The optional cli extra does not add a mandatory dependency. Nothing is installed by parsing.
Expected output:
[]['rich>=13']Common mistake
Do not confuse distribution names with import names. A valid TOML document can still contain invalid project metadata.
Practice
Draft a pyproject.toml for a local project; add a Python version requirement and an optional test extra. Review with a packaging frontend.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Pyproject metadata
Check your understanding of pyproject metadata.
What is printed?
import tomllibprint(tomllib.loads("[project]\nversion=\"1.0\"")["project"]["version"])What is printed?
import tomllibprint(tomllib.loads("[project]\ndependencies=[]")["project"]["dependencies"])Which table declares runtime dependencies?
Does TOML parsing validate packaging rules?