uv icon indicating copy to clipboard operation
uv copied to clipboard

Add excluded dependencies

Open MrPandir opened this issue 1 year ago • 4 comments

I want to request functionality to exclude dependencies, even if it's a sub dependency.

I would like to see the --excluded flag in the uv add command, and excluded-dependencies in pyproject.toml.

This functionality is in rye, but not in uv for some reason. Is there any reason why it's not there, or are there other ways to avoid installing sub-dependencies? Let me know.

I have seen the uv sync --no-install-package parameter, but this solution only affects the virtual environment, and is not locked in pyproject.toml.

MrPandir avatar Sep 09 '24 11:09 MrPandir

Can you explain your use-case some more?

Is this a duplicate of https://github.com/astral-sh/uv/issues/4422?

zanieb avatar Sep 09 '24 11:09 zanieb

I think the use case is to prevent installing transitive dependencies. My use case for this is very similar. I need to use torch, which I've added to my project.dependencies. Since I'm not using GPU-acceleration, I would like to exclude its nvidia dependency to shave off 2.6 GiB of the resulting venv size.

elohmeier avatar Sep 09 '24 12:09 elohmeier

Can you explain your use-case some more?

The example given by @elohmeier perfectly illustrates one of the scenarios where this functionality would be useful. In my case, I primarily need this feature to reduce the size of Docker images. It's unnecessary to download and store libraries that will never be used in the project.

Is this a duplicate of https://github.com/astral-sh/uv/issues/4422?

Regarding whether this is a duplicate of #4422, I'm not certain it is, as this issue proposes a different approach to excluding dependencies.

I've come across one solution that currently works:

[tool.uv]
override-dependencies = [
    "polyfactory ; sys_platform == 'never'",
    "jinja2 ; sys_platform == 'never'",
]

However, this solution is not currently documented, and I find it not very intuitive. Moreover, it doesn't provide a convenient CLI solution for adding or removing items from this list.

When I first saw the sys_platform == 'never' construct, I was quite confused about its purpose. Without additional explanation, this construction is not obvious. It would be much clearer to have a list like:

[tool.uv]
excluded-dependencies = [
    "polyfactory",
    "typing-extensions",
    "faker",
    "jinja2",
]

This format is more straightforward and easier to understand at a glance. It would also be beneficial to have corresponding CLI commands to manage this list easily.

MrPandir avatar Sep 10 '24 09:09 MrPandir

I still think this is roughly a duplicate of #4422 but I'll leave it open for now.

zanieb avatar Oct 21 '24 21:10 zanieb

This doesn't appear to work when running uv lock or uv sync.

[tool.uv]
override-dependencies = [
    "futures; sys_platform == 'never'",
]
$ uv sync
⠦ futures==3.3.0
  × Failed to download and build `futures==3.3.0`
  ╰─▶ Build backend failed to determine requirements with `build_wheel()` (exit status: 1)

      [stderr]
      This backport is meant only for Python 2.
      It does not work on Python 3, and Python 3 users do not need it as the concurrent.futures package is available in the standard library.
      For projects that work on both Python 2 and 3, the dependency needs to be conditional on the Python version, like so:
      extras_require={':python_version == "2.7"': ['futures']}

What's the right work around here? Thanks

dsully avatar Nov 08 '24 21:11 dsully

Can you share a minimal pyproject.toml?

zanieb avatar Nov 08 '24 22:11 zanieb

I think I got it sorted. PEBKAC.

dsully avatar Nov 09 '24 03:11 dsully

This would be really useful when there are transitive dependencies that are not copyright compatible and not necessary for your project to run.

andreaimprovised avatar Jan 22 '25 03:01 andreaimprovised

We already support this; per the linked issue.

zanieb avatar Jan 22 '25 06:01 zanieb

Acknowledged.

That said, we're not currently using pip (or uv pip) to install dependencies in our target environments: local and docker. I'd like to avoid doing so if possible.

andreaimprovised avatar Jan 22 '25 06:01 andreaimprovised

@zanieb I don't think this solved the issue.

Perhaps I didn't emphasize this enough in my messages, but I still wanted to see a way to specify a list of excluded dependencies in the pyproject.toml file.

One of the possible designs for implementing this feature already exists in rye, which suits me perfectly. It's this specific design that I would like to see in uv.

MrPandir avatar Jan 23 '25 15:01 MrPandir

@zanieb is --no-install-package a uv sync only feature? Seems it can be also useful for a simple uv pip install

ion-elgreco avatar Jan 31 '25 13:01 ion-elgreco

We just added support for excluding dependencies entirely by name: https://github.com/astral-sh/uv/pull/16528 (available in the next release)

charliermarsh avatar Oct 31 '25 14:10 charliermarsh