astral-sh / uv

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

How to use `uv pip compile <path to pyproject.toml> -o requirements.txt` to generate a requirements.txt for a package member #9115

Open rushwing opened 1 week ago

rushwing commented 1 week ago

[Background]: Amazon AWS Greengrass doesn't support to install third party tools to manage python virtual env, but it's good to manage local development environment using uv. So when I try to deploy multiple components to Greengrass devices, I need to convert dependencies list to a requirements.txt.

I was struggling to manage multiple greengrass components in one workspace.

Suggest we had two package members (bird-feeder and seeds) in one project, and the structure is like this:

albatross
├── packages
│   ├── bird-feeder
│   │   ├── pyproject.toml
│   │   └── src
│   │       └── bird_feeder
│   │           ├── __init__.py
│   │           └── foo.py
│   └── seeds
│       ├── pyproject.toml
│       └── src
│           └── seeds
│               ├── __init__.py
│               └── bar.py
├── pyproject.toml
├── README.md
├── uv.lock
└── src
    └── albatross
        └── main.py

According to the docs, there were two ways to manage multiple package members in one project:

  1. Using workspace and package method.
    
    [project]
    name = "albatross"
    version = "0.1.0"
    requires-python = ">=3.12"
    dependencies = ["bird-feeder", "seeds", "tqdm>=4,<5"]

[tool.uv.sources] bird-feeder = { workspace = true } seeds = { workspace = true } tqdm = { git = "https://github.com/tqdm/tqdm" }

[tool.uv.workspace] members = ["packages/*"]

[build-system] requires = ["hatchling"] build-backend = "hatchling.build"

2. Using path dependencies for member packages.
```toml
[project]
name = "albatross"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["bird-feeder", "seeds", "tqdm>=4,<5"]

[tool.uv.sources]
bird-feeder = { path = "packages/bird-feeder" }
seeds = { path = "packages/seeds" }
tqdm = { git = "https://github.com/tqdm/tqdm" }

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = [
    "packages/bird-feeder",
    "packages/seeds",
]

Suggest there is no additional dependencies in bird-feeder, so in the pyproject.toml under bird-feeder folder, we will not write dependencies section and assume that it would reuse the dependencies from workspace.

Then when I was using the second solution, I try to build a requirements.txt under bird-feeder using command uv pip compile pyproject.toml -o requirements.txt, but it seemed not creating any packages dependencies in the generated requirements.txt. (It didn't try to get the dependencies list from parent pyproject.toml).

What's the best practice for my usage?

charliermarsh commented 1 week ago

Did you try uv export --package bird-feeder?