Converting pyproject.toml Back to requirements.txt
Extract a flat requirements.txt from a pyproject.toml file. Useful for Docker builds, CI pipelines, or tools that only support requirements.txt.
Detailed Explanation
While pyproject.toml is the modern standard, there are still valid reasons to generate a requirements.txt: Docker multi-stage builds, legacy CI systems, deployment platforms that only support requirements.txt, or simply sharing a dependency list without the full project structure.
pyproject.toml input:
[project]
name = "my-app"
version = "1.0.0"
requires-python = ">=3.10"
dependencies = [
"fastapi>=0.104",
"uvicorn[standard]>=0.24",
"sqlalchemy[asyncio]>=2.0",
"alembic>=1.13",
"pydantic>=2.5",
"httpx>=0.25",
]
[project.optional-dependencies]
dev = [
"pytest>=7.4",
"ruff>=0.1.6",
]
Generated requirements.txt:
# Main dependencies
fastapi>=0.104
uvicorn[standard]>=0.24
sqlalchemy[asyncio]>=2.0
alembic>=1.13
pydantic>=2.5
httpx>=0.25
# Optional: dev
pytest>=7.4
ruff>=0.1.6
When you need requirements.txt from pyproject.toml:
- Docker builds --
pip install -r requirements.txtin a Dockerfile is a common pattern for caching layers - Heroku / PaaS -- some platforms auto-detect
requirements.txtbut notpyproject.toml - pip-tools --
pip-compile pyproject.tomlgenerates a lockedrequirements.txt - Legacy CI -- older Jenkins or TeamCity setups may expect
requirements.txt - Sharing -- a flat text file is easier to share in chat or email
Note: For production deployments, consider using pip-compile (from pip-tools) to generate a fully resolved, pinned requirements.txt from your pyproject.toml. This gives you the best of both worlds: flexible constraints in pyproject.toml and reproducible installs from requirements.txt.
Use Case
Extracting a requirements.txt from a pyproject.toml for use in a Docker multi-stage build where pip install -r is used in the builder stage.