uv icon indicating copy to clipboard operation
uv copied to clipboard

How to convert requirements.txt to `tool.uv.sources` ?

Open sbidoul opened this issue 1 year ago • 6 comments

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?

sbidoul avatar Aug 20 '24 21:08 sbidoul

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

zanieb avatar Aug 20 '24 21:08 zanieb

Needs documentation still.

zanieb avatar Aug 21 '24 16:08 zanieb

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"

yehoshuadimarsky avatar Aug 23 '24 04:08 yehoshuadimarsky

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 avatar Aug 23 '24 04:08 yehoshuadimarsky

@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 avatar Aug 23 '24 13:08 zanieb

@zanieb sure, created https://github.com/astral-sh/uv/issues/6518

yehoshuadimarsky avatar Aug 23 '24 14:08 yehoshuadimarsky

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.

helderco avatar Sep 11 '24 13:09 helderco

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.

charliermarsh avatar Sep 11 '24 13:09 charliermarsh

Thanks for the tip, but do you plan on handling this automatically? Just considering what to put in our docs.

helderco avatar Sep 11 '24 13:09 helderco

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 avatar Nov 27 '24 20:11 prrao87

@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 avatar Jan 09 '25 05:01 shanmuga-priyan314

@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.

prrao87 avatar Jan 09 '25 13:01 prrao87

how i can generate requirement.txt file when i generate my requirement file is empty using uv pip freeze > requiment.txt

Burnaviour avatar Feb 11 '25 07:02 Burnaviour

@Burnaviour that is off topic. Please see #9452.

zanieb avatar Feb 12 '25 14:02 zanieb

Another vote to include this in the official documentation. This is necessary as useful for anyone migrating from the default virtualenv and pip tools

shipping-man avatar Apr 10 '25 14:04 shipping-man

See #12382

zanieb avatar Apr 10 '25 16:04 zanieb