astral-sh / uv

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

FR: Dynamic dependencies and optional dependencies #6422

Open chrisrodrigue opened 3 months ago

chrisrodrigue commented 3 months ago

Greetings uv team and congratulations on the release of 0.3!

I would like to present a rather vexing issue when it comes to project management.

As you may know, Anaconda has a significant presence in the realm of data science and scientific python usage. Currently, conda lags behind current PEP standards and does not support reading dependencies from pyproject.toml. It is typically used by orgs that deploy to airgapped environments since it comes bundled with Anaconda alongside a number of other popular packages such as pytorch.

These orgs vastly prefer to use modern packaging tools like uv in non-airgapped development environments, but this requires carefully maintaining project dependencies for compatibility with Anaconda/conda and avoidance of adding new runtime dependencies to the airgapped deployment environments.

One such solution to avoid duplication of dependencies between conda environment.yml files and those defined pyproject.toml is by utilizing requirements.txt in conjunction with dynamic project.dependencies (as seen in Drphoton’s answer here).

This allows users to have a project layout like so:

Project/
|-- src/
|   |-- __init__.py
|   |-- main.py
|
|-- pyproject.toml
|-- requirements.txt
|-- requirements-dev.txt

and a pyproject.toml with dynamic dependency fields:

[project]
name = "myproject"
# ...
dynamic = ["dependencies", "optional-dependencies"]

[tool.setuptools.dynamic]
dependencies = { file = ["requirements.txt"] }

[tool.setuptools.dynamic.optional-dependencies]
dev = { file = ["requirements-dev.txt"] }

This setup allows users to run conda in airgapped environments like so:

conda create -n ENVNAME "python>=3.12" --file requirements.txt

while also letting them use uv pip in non-airgapped environments:

uv pip install -e .

This approach places a dependency on setuptools to read the dynamic dependencies, and is admittedly just a stop-gap solution to make up for conda’s lack of pyproject.toml support.

It would be nice if uv supported reading dynamic dependencies such that commands like uv sync would work if dependencies were defined in other files (such as split being across numerous requirements.txt).

(Eagerly waiting to see if Astral ever offers a product similar to Anaconda that provides a number of high quality MIT-licensed python packages/python interpreter/uv/ruff/etc).

adamtheturtle commented 1 month ago

At least better for me than the current situation is having a warning / error when trying to use dynamic dependencies.