🎯 Objective
Fix a broken or outdated Python dependency specification and generate a valid, reproducible requirements.txt file using uv.
The main command is:
uv pip compile requirements.in -o requirements.txtThe dependency flow is:
requirements.in
│
│ uv pip compile
▼
Dependency Resolution
│
▼
requirements.txt🧠 Concept
Python projects often separate their dependencies into two files:
requirements.in
requirements.txtThey have different purposes.
requirements.in
Contains the dependencies we directly want:
numpy
pandas
scikit-learn
matplotlibIt is the input dependency specification.
requirements.txt
Contains the fully resolved dependency versions:
numpy==...
pandas==...
scikit-learn==...
matplotlib==...
...It may also contain indirect dependencies required by those packages.
The idea is:
High-Level Dependencies
requirements.in
↓
Dependency Resolver
↓
Exact Dependencies
requirements.txt# ⚡ What Is uv?
uv is a Python package and project management tool that can handle operations such as dependency resolution and package installation.
For this task, we use:
uv pip compileto resolve the dependency specification.
# 🔍 Step 1: Check the Project Files
Check the current directory:
pwdList files:
ls -lahLook for:
requirements.inVerify its contents:
cat requirements.in# 🐍 Step 2: Inspect the Dependency Specification
The requirements.in file contains the project's required packages.
For example:
numpy
pandas
scikit-learn
matplotlibA dependency can also contain a version constraint:
numpy>=2.0
pandas>=2.0or a fixed version:
numpy==2.3.0If the specification is invalid or conflicting, uv may fail to resolve the dependencies.
# 🛠️ Step 3: Fix the Broken Specification
Edit:
vi requirements.inCorrect any invalid dependency names, versions, or incompatible constraints.
For example, a broken specification might request versions that cannot exist together:
package-a==1.0
package-b==2.0where package-b requires a different version of package-a.
The goal is to produce a valid input specification:
requirements.in
↓
Valid package constraints
↓
uv can resolve dependencies# ⚙️ Step 4: Compile the Requirements
Once the dependency specification is corrected, run:
uv pip compile requirements.in -o requirements.txtCommand Breakdown
uvRuns the uv tool.
pip compileResolves the Python dependency specification.
requirements.inThe input file.
-o requirements.txtWrites the resolved dependencies to:
requirements.txt# 🔄 How Compilation Works
Suppose:
requirements.incontains:
pandasPandas itself requires other packages.
So the dependency tree might look conceptually like:
pandas
│
├── numpy
├── python-dateutil
└── pytzRunning:
uv pip compile requirements.in -o requirements.txtresolves the complete dependency graph and writes compatible versions into the output file.
requirements.in
│
│ Direct Dependencies
▼
┌──────────────────┐
│ uv pip compile │
└────────┬─────────┘
│
│ Resolve
▼
Dependency Graph
│
▼
requirements.txt# 📄 Step 5: Verify requirements.txt
Check that the file was created:
ls -lah requirements.txtThen inspect it:
cat requirements.txtYou should see resolved package versions.
The exact contents depend on the packages and constraints defined in requirements.in.
# 📦 Step 6: Install the Compiled Dependencies
Once the lock-style requirements file is valid, dependencies can be installed using:
uv pip install -r requirements.txtIf using a Python virtual environment, activate it first when appropriate:
source ml-env/bin/activateThen:
uv pip install -r requirements.txtThe workflow becomes:
requirements.in
│
│ compile
▼
requirements.txt
│
│ install
▼
Python Environment# 🆚 requirements.in vs requirements.txt
Think of it as:
Developer Intent
↓
requirements.in
↓ uv pip compile
Resolved Environment
↓
requirements.txt# 🆚 pip freeze vs uv pip compile
These commands solve different problems.
pip freeze
pip freeze > requirements.txtRecords packages that are already installed in the current environment.
Existing Environment
↓
pip freeze
↓
requirements.txtuv pip compile
uv pip compile requirements.in -o requirements.txtStarts from a dependency specification and resolves compatible dependencies.
requirements.in
↓
uv resolver
↓
requirements.txtFor dependency management, compilation provides a more intentional workflow than simply capturing everything currently installed.
# ⚠️ Common Problem: Resolution Failure
If uv pip compile fails, inspect the error carefully.
A common cause is incompatible version requirements.
Conceptually:
Package A
└── requires X < 2
Package B
└── requires X >= 2
↓
Dependency Conflict ❌The solution is to modify the dependency constraints in:
requirements.inuntil a compatible dependency graph can be resolved.
Then run the compile command again.
# ⚠️ File Naming
Use consistent filenames:
requirements.in
requirements.txtThe common naming convention uses requirements with an s.
Therefore, prefer:
uv pip compile requirements.in -o requirements.txtrather than:
requirement.in
requirement.txtunless the task specifically provides those singular filenames.
# ⚙️ Complete Workflow
Check the files:
pwd
ls -lahInspect:
cat requirements.inEdit the broken specification:
vi requirements.inCompile:
uv pip compile requirements.in -o requirements.txtVerify:
ls -lah requirements.txt
cat requirements.txtInstall if required:
uv pip install -r requirements.txt# 🧪 Validation Checklist
# 📌 Summary
The main command is:
uv pip compile requirements.in -o requirements.txtThe complete dependency workflow is:
requirements.in
│
│ Direct Dependencies
▼
uv pip compile
│
│ Dependency Resolution
▼
requirements.txt
│
│ Resolved Dependencies
▼
uv pip install -r requirements.txt
│
▼
Python Environment ✅The two important files are:
requirements.in
→ What packages the project needs
requirements.txt
→ Exact resolved dependency set> uv pip compile resolves the dependency specification from requirements.in and generates a reproducible requirements.txt containing compatible package versions.