8000 Update README to say that using `requirement()` is optional (#594) · axivion/rules_python@84261bc · GitHub
[go: up one dir, main page]

Skip to content

Commit 84261bc

Browse files
person142alexeagle
andauthored
Update README to say that using requirement() is optional (bazel-contrib#594)
Co-authored-by: Alex Eagle <eagle@post.harvard.edu>
1 parent 628d40f commit 84261bc

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

README.md

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Bazel will only fetch/build wheels for the requirements in the subgraph of your
124124

125125
There are API differences between `pip_parse` and `pip_install`:
126126
1. `pip_parse` requires a fully resolved lock file of your python dependencies. You can generate this by using the `compile_pip_requirements` rule,
127-
running `pip-compile` directly, or using virtualenv and `pip freeze`. `pip_parse` uses a label argument called `requirements_lock` instead of
127+
running `pip-compile` directly, or using virtualenv and `pip freeze`. `pip_parse` uses a label argument called `requirements_lock` instead of
128128
`requirements` to make this distinction clear.
129129
2. `pip_parse` translates your requirements into a starlark macro called `install_deps`. You must call this macro in your WORKSPACE to
130130
declare your dependencies.
@@ -148,12 +148,11 @@ install_deps()
148148

149149
### Consuming `pip` dependencies
150150

151-
Each extracted wheel repo contains a `py_library` target representing the
152-
wheel's contents. Rather than depend on this target's label directly -- which
153-
would require hardcoding the wheel repo's mangled name into your BUILD files --
154-
you should instead use the `requirement()` function defined in the central
155-
repo's `//:requirements.bzl` file. This function maps a pip package name to a
156-
label.
151+
Each extracted wheel repo contains a `py_library` target representing
152+
the wheel's contents. There are two ways to access this library. The
153+
first is using the `requirement()` function defined in the central
154+
repo's `//:requirements.bzl` file. This function maps a pip package
155+
name to a label:
157156

158157
```python
159158
load("@my_deps//:requirements.bzl", "requirement")
@@ -169,12 +168,37 @@ py_library(
169168
)
170169
```
171170

171+
The reason `requirement()` exists is that the pattern for the labels,
172+
while not expected to change frequently, is not guaranteed to be
173+
stable. Using `requirement()` ensures that you do not have to refactor
174+
your `BUILD` files if the pattern changes.
172175

173-
For reference, the wheel repos are canonically named following the pattern:
174-
`@{central_repo_name}_pypi__{distribution}_{version}`. Characters in the
175-
distribution and version that are illegal in Bazel label names (e.g. `-`, `.`)
176-
are replaced with `_`. While this naming pattern doesn't change often, it is
177-
not guaranted to remain stable, so use of the `requirement()` function is recommended.
176+
On the other hand, using `requirement()` has several drawbacks; see
177+
[this issue][requirements-drawbacks] for an enumeration. If you don't
178+
want to use `requirement()` then you can instead use the library
179+
labels directly. For `pip_parse` the labels are of the form
180+
181+
```
182+
@{name}_{package}//:pkg
183+
```
184+
185+
Here `name` is the `name` attribute that was passed to `pip_parse` and
186+
`package` is the pip package name with characters that are illegal in
187+
Bazel label names (e.g. `-`, `.`) replaced with `_`. If you need to
188+
update `name` from "old" to "new", then you can run the following
189+
buildozer command:
190+
191+
```
192+
buildozer 'substitute deps @old_([^/]+)//:pkg @new_${1}//:pkg' //...
193+
```
194+
195+
For `pip_install` the labels are instead of the form
196+
197+
```
198+
@{name}//pypi__{package}
199+
```
200+
201+
[requirements-drawbacks]: https://github.com/bazelbuild/rules_python/issues/414
178202

179203
#### 'Extras' requirement consumption
180204

@@ -196,15 +220,15 @@ you'd just put `requirement("useful_dep")`.
196220

197221
### Consuming Wheel Dists Directly
198222

199-
If you need to depend on the wheel dists themselves, for instance to pass them
223+
If you need to depend on the wheel dists themselves, for instance to pass them
200224
to some other packaging tool, you can get a handle to them with the `whl_requirement` macro. For example:
201-
225+
202226
```python
203-
filegroup(
204-
name = "whl_files",
205-
data = [
206-
whl_requirement("boto3"),
207-
]
227+
filegroup(
228+
name = "whl_files",
229+
data = [
230+
whl_requirement("boto3"),
231+
]
208232
)
209233
```
210234

0 commit comments

Comments
 (0)
0