uv is a relatively new Python package manager, designed to be faster and more efficient than pip. It's still evolving, but already gaining traction.
Generating a requirements.txt file with uv is straightforward. Here are the steps:
Prerequisites
Install uv: Run pip install uv (yes, you read that right – you install uv using pip for now). In the future, uv might become a standalone binary.
Create a virtual environment (optional but recommended): Run uv venv (or python -m uv venv if you have multiple Python versions). This creates a new virtual environment named .venv in your project directory. Activate it using source .venv/bin/activate (on Linux/Mac) or .venv\Scripts\activate (on Windows).
Generate requirements.txt
Now, let's get to the main event! You have two options:
Option 1: From an existing project with dependencies installed
If you've already installed dependencies using uv (or pip) in your project, run:
bash
uv export > requirements.txt
This command analyzes the dependencies installed in your current Python environment (i.e., the active virtual environment or your global Python environment) and generates a requirements.txt file based on the pinned dependencies (i.e., exact versions).
Option 2: From a pyproject.toml file (recommended for new projects)
uv supports pyproject.toml files, which are becoming the new standard for Python project configuration (introduced by PEP 518). If you have a pyproject.toml file in your project (or create one using uv init), you can generate requirements.txt like this:
bash
uv pip compile pyproject.toml -o requirements.txt
Here:
uv pip compile is the command to resolve dependencies from pyproject.toml.
pyproject.toml is the input file containing your project dependencies (in the [project] and [tool.uv] sections).
-o requirements.txt specifies the output file.
In pyproject.toml, your dependencies should be listed like this: