How to convert requirements.txt to `tool.uv.sources` ?
With pip I use constraints files containing a bunch of git URLs to achieve the equivalent of tool.uv.sources.
Is there a convenient way to convert such a requirements file to a tool.uv.sources section?
Well, we should support this with uv add -r requirements.txt but that doesn't propagate tool.uv.sources like it should — we'll fix that and document it.
e.g.
❯ uv init
Initialized project `example`
❯ echo "uv-public-pypackage @ git+https://github.com/astral-test/uv-public-pypackage" > requirements.txt
❯ uv add -r requirements.txt
Using Python 3.12.1
Creating virtualenv at: .venv
Updated https://github.com/astral-test/uv-public-pypackage (b270df1)
Resolved 9 packages in 673ms
Building example @ file:///Users/zb/workspace/example
Building example @ file:///Users/zb/workspace/example
Building example @ file:///Users/zb/workspace/example
Building example @ file:///Users/zb/workspace/example
Built uv-public-pypackage @ git+https://github.com/astral-test/uv-public-pypackage@b270df1a2fb5d012294e9aaf05e7e0bab1e6a389[1merror: Failed to prepare distributions
Caused by: Failed to fetch wheel: example @ file:///Users/zb/workspace/example
Caused by: Build backend failed to build wheel through `build_editable()` with exit status: 1
Needs documentation still.
Just an idea, can we instead add a new flag -c/--constraint to uv add, so that you can pass in constraint files directly? Instead of needing to add them to a requirements.txt.
My specific use case is using Apache Airflow which only supports pip (not Poetry etc) because it basically requires constraint files, see https://github.com/apache/airflow/blob/main/README.md#installing-from-pypi
That way, I can do (copying from their example):
uv add 'apache-airflow==2.10.0' \
--constraint "https://raw.githubusercontent.com/apache/airflow/constraints-2.10.0/constraints-3.8.txt"
Or, thinking aloud, more broadly are there any other flags that exist in uv pip install that can be also added to uv add?
@yehoshuadimarsky can you please open a dedicated issue for that request?
You need to add that constraint file to tool.uv.constraint_dependencies? It needs to be tracked in the project. uv add --constraint could perhaps do that for you, but it's going to apply to all of your dependencies.
@zanieb sure, created https://github.com/astral-sh/uv/issues/6518
The problem with uv add -r requirements.txt is it adds all of those dependencies to project.dependencies.
If a requirements.txt file is used for pinning transient dependencies, how do you migrate to uv.lock?
Basically need this, without saving any constraints to pyproject.toml or anything, just the one time migration and keep the same pinned versions:
uv lock --constraints requirements.txt
rm requirements.txt
Doesn't have to be --constraints of course, since that may mean I'd want to save them in pyproject.toml or something.
The typically workaround (which isn't very nice) is:
- Add all the constraints to
tool.uv.constraint-dependencies. - Run
uv lock. - Remove the constraints from
tool.uv.constraint-dependencies.
uv will then avoid upgrading past the pinned versions in the future by default.
\cc @zanieb since we've discussed this in-person before.
Thanks for the tip, but do you plan on handling this automatically? Just considering what to put in our docs.
This still isn't documented, but is a great feature! Found this via stackoverflow.
Something like this:
Generating pyproject.toml from requirements.txt
uv add -r requirements.txt
cc @zanieb
@prrao87 This is not working anymore. I am getting the following error after running the command you mentioned
error: No `pyproject.toml` found in current directory or any parent directory
@prrao87 This is not working anymore. I am getting the following error after running the command you mentioned
error: No `pyproject.toml` found in current directory or any parent directory
@shanmuga-priyan314
You also need to initialize the uv project by running the command uv init first. That'll create an empty pyproject.toml in the directory. Then the add command should work.
how i can generate requirement.txt file when i generate my requirement file is empty using uv pip freeze > requiment.txt
@Burnaviour that is off topic. Please see #9452.
Another vote to include this in the official documentation. This is necessary as useful for anyone migrating from the default virtualenv and pip tools
See #12382