Skip to content

Commit 0ff0597

Browse files
docs: Add compile_pip_requirements documentation to "Using dependencies from PyPI"
1 parent cc12610 commit 0ff0597

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

docs/pypi-dependencies.md

+30-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,34 @@
55

66
Using PyPI packages (aka "pip install") involves two main steps.
77

8-
1. [Installing third party packages](#installing-third-party-packages)
9-
2. [Using third party packages as dependencies](#using-third-party-packages)
8+
1. [Generating requirements file](#generating-requirements-file)
9+
2. [Installing third party packages](#installing-third-party-packages)
10+
3. [Using third party packages as dependencies](#using-third-party-packages)
11+
12+
{#generating-requirements-file}
13+
## Generating requirements file
14+
15+
Generally, when working on a Python project, you'll have some dependencies that themselves have other dependencies. You might also specify dependency bounds instead of specific versions. So you'll need to generate a full list of all transitive dependencies and pinned versions for every dependency.
16+
17+
Typically, you'd have your dependencies specified in `pyproject.toml` or `requirements.in` and generate the full pinned list of dependencies in `requirements_lock.txt`, which you can manage with the `compile_pip_requirements` Bazel rule:
18+
19+
```starlark
20+
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
21+
22+
compile_pip_requirements(
23+
name = "requirements",
24+
src = "requirements.in",
25+
requirements_txt = "requirements_lock.txt",
26+
)
27+
```
28+
29+
This rule generates two targets:
30+
- `bazel run [name].update` will regenerate the `requirements_txt` file
31+
- `bazel test [name]_test` will test that the `requirements_txt` file is up to date
32+
33+
For more documentation, see the API docs under {obj}`@rules_python//python:pip.bzl`.
34+
35+
Once you generate this fully specified list of requirements, you can install the requirements with the instructions in [Installing third party packages](#installing-third-party-packages).
1036

1137
{#installing-third-party-packages}
1238
## Installing third party packages
@@ -23,12 +49,11 @@ pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
2349
pip.parse(
2450
hub_name = "my_deps",
2551
python_version = "3.11",
26-
requirements_lock = "//:requirements_lock_3_11.txt",
52+
requirements_lock = "//:requirements_lock.txt",
2753
)
2854
use_repo(pip, "my_deps")
2955
```
30-
For more documentation, including how the rules can update/create a requirements
31-
file, see the bzlmod examples under the {gh-path}`examples` folder or the documentation
56+
For more documentation, see the bzlmod examples under the {gh-path}`examples` folder or the documentation
3257
for the {obj}`@rules_python//python/extensions:pip.bzl` extension.
3358

3459
```{note}

0 commit comments

Comments
 (0)