@@ -124,7 +124,7 @@ Bazel will only fetch/build wheels for the requirements in the subgraph of your
124
124
125
125
There are API differences between ` pip_parse ` and ` pip_install ` :
126
126
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
128
128
` requirements ` to make this distinction clear.
129
129
2 . ` pip_parse ` translates your requirements into a starlark macro called ` install_deps ` . You must call this macro in your WORKSPACE to
130
130
declare your dependencies.
@@ -148,12 +148,11 @@ install_deps()
148
148
149
149
### Consuming ` pip ` dependencies
150
150
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:
157
156
158
157
``` python
159
158
load(" @my_deps//:requirements.bzl" , " requirement" )
@@ -169,12 +168,37 @@ py_library(
169
168
)
170
169
```
171
170
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.
172
175
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
178
202
179
203
#### 'Extras' requirement consumption
180
204
@@ -196,15 +220,15 @@ you'd just put `requirement("useful_dep")`.
196
220
197
221
### Consuming Wheel Dists Directly
198
222
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
200
224
to some other packaging tool, you can get a handle to them with the ` whl_requirement ` macro. For example:
201
-
225
+
202
226
``` 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
+ ]
208
232
)
209
233
```
210
234
0 commit comments