astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
27.76k stars 797 forks source link

Add excluded dependencies #7214

Open MrPandir opened 2 months ago

MrPandir commented 2 months ago

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.

zanieb commented 2 months ago

Can you explain your use-case some more?

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

elohmeier commented 2 months ago

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.

MrPandir commented 2 months ago

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.

zanieb commented 1 month ago

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

dsully commented 2 weeks ago

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

zanieb commented 2 weeks ago

Can you share a minimal pyproject.toml?

dsully commented 2 weeks ago

I think I got it sorted. PEBKAC.