Basic requirements.txt to pyproject.toml
Learn how to convert a simple requirements.txt file with basic package names and version pins to the pyproject.toml dependencies format defined by PEP 621.
Detailed Explanation
Converting a basic requirements.txt to pyproject.toml is the most common migration task for Python projects adopting modern packaging standards. A simple requirements.txt lists one package per line, optionally with a version specifier.
Example requirements.txt:
requests==2.31.0
flask>=3.0
pydantic
Converted to pyproject.toml:
[project]
name = ""
version = "0.1.0"
requires-python = ">=3.8"
dependencies = [
"requests==2.31.0",
"flask>=3.0",
"pydantic",
]
Key differences in the format:
- String quoting. Each dependency in
pyproject.tomlmust be enclosed in double quotes and placed inside a TOML array. - Trailing commas. TOML allows trailing commas in arrays, making it easy to add or remove entries.
- Project metadata.
pyproject.tomlrequires additional fields likename,version, andrequires-pythonthat don't exist inrequirements.txt. - Section headers. Dependencies live under the
[project]table inpyproject.toml, not as a flat list.
The PEP 621 format is now supported by all major Python build tools including pip, setuptools, hatch, flit, pdm, and poetry (with adapter). This makes migration straightforward and future-proof.
Use Case
Migrating a small Python script or library from the traditional requirements.txt format to pyproject.toml when adopting modern packaging with pip, hatch, or flit.