Sub topic 18
Migrating a requirements.txt project
This chapter assumes you know basic Python, virtual environments, and pip. You should also have read the earlier chapters on pyproject.toml, uv add, dependency groups, and uv.lock. The goal is to move an existing project that uses requirements.txt to a uv-managed pyproject.toml without changing what it installs.
What changes in a migration
A requirements.txt file lists packages to install. A pyproject.toml file describes the project, and uv.lock records the exact resolved versions. Migrating means turning the first into the other two.
Assume a project with these files:
requirements.txtholds runtime packages such ashttpxandtyper.requirements-dev.txtholds tools such aspytest.
Steps
- Create the project metadata without touching your code.
- Import the runtime requirements.
- Import the development requirements into a dependency group.
- Lock, sync, and test.
uv init --bare
uv python pin 3.12
uv add -r requirements.txt
uv add --dev -r requirements-dev.txt
uv sync
uv run pytestuv init --bare writes only a pyproject.toml. It does not create a sample main.py or a README. In an existing repository, that is what you want. uv add -r reads each requirement from the file and adds it to [project].dependencies. uv add --dev -r puts the requirements in the dev entry of [dependency-groups].
Pinned versions versus intent
Files made with pip freeze list every package, including transitive dependencies, each pinned with ==. If you import such a file, pyproject.toml will list far more than you chose and will pin it all.
Prefer to add only the packages your code imports directly, and let uv.lock record the rest. Use loose bounds when a pin is not needed.
[project]
name = "legacy-app"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"httpx>=0.27",
"typer>=0.12",
]
[dependency-groups]
dev = ["pytest>=8"]If you want the old pins to guide the first resolution, add the frozen file as a constraint. Use uv add -r requirements.txt -c constraints.txt, where the constraints file holds the old pinned versions. Constraints limit versions but do not add packages.
Common errors
- Imported a frozen file. Symptom: dozens of direct dependencies. Fix: remove the transitive ones with
uv remove, then re-runuv lock. - Missing
requires-python. Symptom: resolution picks versions that fail on your real interpreter. Fix: set it in[project]or runuv python pin. - Hashes or index options in the file. Lines such as
--index-urlor--hash=...are options, not packages. Move index settings to[[tool.uv.index]]and let uv record hashes inuv.lock. - Old file still in CI. The pipeline keeps running
pip install -r requirements.txt. Replace it withuv sync --locked.
Keeping a requirements file
Some deployment systems still need requirements.txt. Generate it from the lockfile instead of editing it by hand.
uv export --format requirements-txt --no-hashes -o requirements.txtExercise
Your requirements.txt came from pip freeze and has 60 pinned lines, but your code directly imports only 4 packages. What should [project].dependencies contain after migration, and where do the other 56 packages go?
Show answer
dependencies should list only the 4 packages you import directly, with version bounds where needed. The other 56 are transitive dependencies. uv resolves them and records exact versions in uv.lock, so you do not list them in pyproject.toml.
Exercise
Which command produces a requirements.txt from the lockfile for a deployment system that cannot read uv.lock?
Show answer
uv export --format requirements-txt --no-hashes -o requirements.txt