Sub topic 18
Migrating a requirements.txt project
This chapter assumes you know basic Python, can use a terminal, and understand what pip and virtual environments do. It also assumes you have read the earlier chapters on pyproject.toml, uv add, dependency groups, and uv.lock. Here you will move an existing project that uses requirements.txt over to uv's project workflow.
What you are moving
A typical older project has these files:
requirements.txtlists runtime packages, often pinned with==.requirements-dev.txtlists test and lint tools, and often starts with-r requirements.txt.- A hand-made
.venvdirectory, created withpython -m venv. - Sometimes a
setup.pyorsetup.cfg.
The goal is a pyproject.toml that holds your direct dependencies and a uv.lock that holds the full resolved set. After the move, uv sync and uv run replace pip install -r and manual activation.
There are two kinds of migration. The first keeps your pip-style workflow and only swaps the tool, using uv pip. The second moves to uv's project model. This chapter focuses on the second, but starts with the safe first step.
Step 0: Check what you have
Before you change anything, write down how the project is installed today. Run these commands in the old environment:
python --version
pip freeze > frozen-before.txtfrozen-before.txt records every installed package, including transitive ones. A transitive dependency is a package that one of your dependencies needs. You will compare against this file at the end.
Commit your work first, so you can undo any change with git.
Step 1: Create the project metadata
Run uv init inside the existing directory. Because the directory already has files, use --bare so uv writes only a pyproject.toml and does not add sample code:
uv init --bare --name inventory-apiThe result is a minimal file with a [project] table:
[project]
name = "inventory-api"
version = "0.1.0"
requires-python = ">=3.12"Set requires-python to the oldest Python version you actually support. If you are unsure, use the version you recorded in step 0. A value that is too high blocks users on older Pythons. A value that is too low lets the resolver pick package versions that do not work for you.
If the project is an application that you run but never publish, you do not need a build backend. uv then treats it as a virtual project: it installs the dependencies but not your own code as a package. If you want your code installed and a console script available, add a [build-system] table as covered in the packaging chapters.
Step 2: Import the dependencies
The key command is uv add -r, which reads a requirements file and adds each entry to pyproject.toml.
uv add -r requirements.txt
uv add --dev -r requirements-dev.txtThe second command puts the development tools into the dev dependency group. Both commands also resolve everything and write uv.lock.
Pins versus ranges
This is where most migrations go wrong, so read this section carefully. A file produced by pip freeze looks like this:
certifi==2024.8.30
httpx==0.27.2
idna==3.10
sniffio==1.3.1Only httpx is a direct dependency. The other three are transitive. If you import the whole file, uv copies all four into [project].dependencies as exact pins. That works, but it defeats the point of a lockfile. uv.lock already records exact versions, so pyproject.toml should list only what your code imports, with ranges that state what you need.
Compare the two outcomes:
| Approach | `pyproject.toml` contains | Effect |
|---|---|---|
| Import a frozen file as is | Every package, pinned with `==` | Upgrading one package means editing several lines and fighting conflicting pins |
| Import only direct dependencies | A few packages with lower bounds such as `httpx>=0.27` | `uv lock --upgrade` can move versions forward within your stated limits |
So clean the list before you import it. Keep a line only if your code imports that package, or if it provides a command you run. Then loosen the pins. If you had httpx==0.27.2, write httpx>=0.27. Use an upper bound only when you know a later release breaks you.
If you have no hand-written list and only a frozen file, a quick way to find the direct dependencies is to search your imports:
grep -rhE "^(import|from) " src tests | sort -uMatch the module names to package names by hand. A module name is not always the package name. For example, import yaml comes from the package PyYAML, and import PIL comes from Pillow.
Keeping the old pins as a starting point
You may want the migration to change no versions at first. In that case, import the frozen file once, then run uv lock to confirm it resolves. Afterwards, remove the transitive packages one at a time with uv remove, and run your tests after each removal. Because the package is still pulled in by its parent, the locked version stays the same.
Step 3: Create the environment and run the tests
Delete the old .venv only after your commit is safe, then let uv rebuild it:
rm -rf .venv
uv sync
uv run pytestuv sync creates .venv from uv.lock and installs your dependency groups that are enabled by default. uv run checks that the lockfile and environment are current before it runs the command, so you never need to activate the environment.
If the tests pass, compare against your earlier record:
uv pip freeze > frozen-after.txt
diff frozen-before.txt frozen-after.txtSmall differences are normal. Packages you pinned loosely may now resolve to newer releases. Look for packages that vanished or changed by a major version.
Step 4: Handle things requirements.txt allowed
Requirements files can express several things that need a different home in pyproject.toml.
| In `requirements.txt` | In the uv project |
|---|---|
| `-r other.txt` | A dependency group, or a combined list |
| `-e ./libs/shared` | `[tool.uv.sources]` with a `path` entry, possibly `editable = true` |
| `pkg @ git+https://...` | `[tool.uv.sources]` with a `git` entry |
| `--extra-index-url ...` | `[[tool.uv.index]]` and a per-package `index` source |
| `pkg==1.0 ; python_version < "3.12"` | The same environment marker inside the dependency string |
| `--hash=sha256:...` | Not needed. `uv.lock` stores hashes already |
For example, a private index entry looks like this:
[project]
name = "inventory-api"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["httpx>=0.27", "internal-utils"]
[tool.uv.sources]
internal-utils = { index = "internal" }
[[tool.uv.index]]
name = "internal"
url = "https://packages.example.com/simple"Binding a package to a named index this way, instead of adding an extra index globally, avoids dependency confusion. That is an attack where a public package with the same name as your private one is installed by mistake.
Step 5: Update the surrounding tools
Moving the dependencies is only half the job. Search the repository for every place that still mentions pip:
- CI workflows that run
pip install -r requirements.txt. Replace them withuv sync --locked, so the build fails ifuv.lockis out of date. - A
Dockerfilethat copies and installs requirements files. Copypyproject.tomlanduv.lockinstead, and runuv sync --locked. - Documentation that tells contributors to create and activate a virtual environment.
- Tools that read
requirements.txtdirectly, such as some security scanners.
For the last case you can generate the file from the lockfile when needed:
uv export --no-dev --no-hashes -o requirements.txtTreat that file as build output. Do not edit it by hand, and do not use it as the source of truth.
Common errors
- No solution found. Old exact pins sometimes conflict once the resolver sees the whole graph. Read the message, which names the clashing packages. Loosen the pin of the package it names.
requires-pythonconflict. A dependency that needs a newer Python than yourrequires-pythonallows makes resolution fail. Either raise the lower bound or pick an older release of that dependency.- Missing dev tools. If
uv run pytestsays pytest is not found, the tool was imported as a runtime dependency under another file, or the group was not synced. Check[dependency-groups]and runuv syncagain. - Import works locally but not in CI. A package was installed in the old
.venvby hand and never listed. The clean rebuild in step 3 exposes this. Add it withuv add. - Editable install of your own code is missing. An application without a build backend is not installed as a package. Add
[build-system]if other code needs toimportit by name.
The alternative: stay with uv pip
If a full move is too large right now, uv pip accepts the same commands as pip and runs much faster:
uv venv
uv pip install -r requirements.txt
uv pip compile requirements.in -o requirements.txtThis gives faster installs without changing your files. The cost is that you keep the old workflow, with no cross-platform lockfile and no uv run. Treat it as a stepping stone, not the end state.
Exercise
Your requirements.txt was made with pip freeze and holds 40 pinned packages, but your code imports only 6 of them. Which packages should go into [project].dependencies, why, and which command checks that the migrated project still installs from the lockfile?
Show answer
List only the packages your code imports directly, written as ranges such as httpx>=0.27. The other 34 are transitive, and uv.lock already records their exact versions and hashes. Pinning them in pyproject.toml makes upgrades hard. Run uv sync --locked to confirm the environment installs from uv.lock and that the lockfile matches pyproject.toml. It fails if the lockfile is out of date.
Exercise
A line in your old file reads mylib @ git+https://github.com/example/[email protected]. Where does this go in the uv project, and what do you write?
Show answer
Add mylib to [project].dependencies, then add a source entry in [tool.uv.sources], for example mylib = { git = "https://github.com/example/mylib", tag = "v2.1" }. Run uv lock to record the resolved commit.