Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f0efec5

Browse filesBrowse files
authored
Standardise on pip_parse (bazel-contrib#807)
1 parent a364fca commit f0efec5
Copy full SHA for f0efec5

File tree

Expand file treeCollapse file tree

23 files changed

+139
-455
lines changed
Filter options
Expand file treeCollapse file tree

23 files changed

+139
-455
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+28-53Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
This repository is the home of the core Python rules -- `py_library`,
99
`py_binary`, `py_test`, and related symbols that provide the basis for Python
10-
support in Bazel. It also contains packaging rules for integrating with PyPI
11-
(`pip`). Documentation lives in the
10+
support in Bazel. It also contains package installation rules for integrating with PyPI and other package indices. Documentation lives in the
1211
[`docs/`](https://github.com/bazelbuild/rules_python/tree/main/docs)
1312
directory and in the
1413
[Bazel Build Encyclopedia](https://docs.bazel.build/versions/master/be/python.html).
@@ -24,7 +23,7 @@ Once they are fully migrated to rules_python, they may evolve at a different
2423
rate, but this repository will still follow
2524
[semantic versioning](https://semver.org).
2625

27-
The packaging rules (`pip_install`, etc.) are less stable. We may make breaking
26+
The package installation rules (`pip_install`, `pip_parse` etc.) are less stable. We may make breaking
2827
changes as they evolve.
2928

3029
This repository is maintained by the Bazel community. Neither Google, nor the
@@ -101,70 +100,27 @@ py_binary(
101100
)
102101
```
103102

104-
## Using the packaging rules
103+
## Using the package installation rules
105104

106105
Usage of the packaging rules involves two main steps.
107106

108-
1. [Installing `pip` dependencies](#installing-pip-dependencies)
109-
2. [Consuming `pip` dependencies](#consuming-pip-dependencies)
107+
1. [Installing third_party packages](#installing-third_party-packages)
108+
2. [Using third_party packages as dependencies](#using-third_party-packages-as-dependencies)
110109

111-
The packaging rules create two kinds of repositories: A central external repo that holds
110+
The package installation rules create two kinds of repositories: A central external repo that holds
112111
downloaded wheel files, and individual external repos for each wheel's extracted
113112
contents. Users only need to interact with the central external repo; the wheel repos
114113
are essentially an implementation detail. The central external repo provides a
115114
`WORKSPACE` macro to create the wheel repos, as well as a function, `requirement()`, for use in
116115
`BUILD` files that translates a pip package name into the label of a `py_library`
117116
target in the appropriate wheel repo.
118117

119-
### Installing `pip` dependencies
118+
### Installing third_party packages
120119

121120
To add pip dependencies to your `WORKSPACE`, load the `pip_install` function, and call it to create the
122121
central external repo and individual wheel external repos.
123122

124123

125-
```python
126-
load("@rules_python//python:pip.bzl", "pip_install")
127-
128-
# Create a central external repo, @my_deps, that contains Bazel targets for all the
129-
# third-party packages specified in the requirements.txt file.
130-
pip_install(
131-
name = "my_deps",
132-
requirements = "//path/to:requirements.txt",
133-
)
134-
```
135-
136-
Note that since `pip_install` is a repository rule and therefore executes pip at WORKSPACE-evaluation time, Bazel has no
137-
information about the Python toolchain and cannot enforce that the interpreter
138-
used to invoke pip matches the interpreter used to run `py_binary` targets. By
139-
default, `pip_install` uses the system command `"python3"`. This can be overridden by passing the
140-
`python_interpreter` attribute or `python_interpreter_target` attribute to `pip_install`.
141-
142-
You can have multiple `pip_install`s in the same workspace. This will create multiple external repos that have no relation to
143-
one another, and may result in downloading the same wheels multiple times.
144-
145-
As with any repository rule, if you would like to ensure that `pip_install` is
146-
re-executed in order to pick up a non-hermetic change to your environment (e.g.,
147-
updating your system `python` interpreter), you can force it to re-execute by running
148-
`bazel sync --only [pip_install name]`.
149-
150-
### Fetch `pip` dependencies lazily
151-
152-
One pain point with `pip_install` is the need to download all dependencies resolved by
153-
your requirements.txt before the bazel analysis phase can start. For large python monorepos
154-
this can take a long time, especially on slow connections.
155-
156-
`pip_parse` provides a solution to this problem. If you can provide a lock
157-
file of all your python dependencies `pip_parse` will translate each requirement into its own external repository.
158-
Bazel will only fetch/build wheels for the requirements in the subgraph of your build target.
159-
160-
There are API differences between `pip_parse` and `pip_install`:
161-
1. `pip_parse` requires a fully resolved lock file of your python dependencies. You can generate this by using the `compile_pip_requirements` rule,
162-
running `pip-compile` directly, or using virtualenv and `pip freeze`. `pip_parse` uses a label argument called `requirements_lock` instead of
163-
`requirements` to make this distinction clear.
164-
2. `pip_parse` translates your requirements into a starlark macro called `install_deps`. You must call this macro in your WORKSPACE to
165-
declare your dependencies.
166-
167-
168124
```python
169125
load("@rules_python//python:pip.bzl", "pip_parse")
170126

@@ -174,14 +130,33 @@ pip_parse(
174130
name = "my_deps",
175131
requirements_lock = "//path/to:requirements_lock.txt",
176132
)
177-
178133
# Load the starlark macro which will define your dependencies.
179134
load("@my_deps//:requirements.bzl", "install_deps")
180135
# Call it to define repos for your requirements.
181136
install_deps()
182137
```
183138

184-
### Consuming `pip` dependencies
139+
Note that since `pip_parse` is a repository rule and therefore executes pip at WORKSPACE-evaluation time, Bazel has no
140+
information about the Python toolchain and cannot enforce that the interpreter
141+
used to invoke pip matches the interpreter used to run `py_binary` targets. By
142+
default, `pip_parse` uses the system command `"python3"`. This can be overridden by passing the
143+
`python_interpreter` attribute or `python_interpreter_target` attribute to `pip_parse`.
144+
145+
You can have multiple `pip_parse`s in the same workspace. This will create multiple external repos that have no relation to
146+
one another, and may result in downloading the same wheels multiple times.
147+
148+
As with any repository rule, if you would like to ensure that `pip_parse` is
149+
re-executed in order to pick up a non-hermetic change to your environment (e.g.,
150+
updating your system `python` interpreter), you can force it to re-execute by running
151+
`bazel sync --only [pip_parse name]`.
152+
153+
Note: The `pip_install` rule is deprecated. `pip_parse` offers identical functionality and both `pip_install`
154+
and `pip_parse` now have the same implementation. The name `pip_install` may be removed in a future version of the rules.
155+
The maintainers have taken all reasonable efforts to faciliate a smooth transition, but some users of `pip_install` will
156+
need to replace their existing `requirements.txt` with a fully resolved set of dependencies using a tool such as
157+
`pip-tools` or the `compile_pip_requirements` repository rule.
158+
159+
### Using third_party packages as dependencies
185160

186161
Each extracted wheel repo contains a `py_library` target representing
187162
the wheel's contents. There are two ways to access this library. The

‎docs/pip.md

Copy file name to clipboardExpand all lines: docs/pip.md
+9-58Lines changed: 9 additions & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/pip_repository.md

Copy file name to clipboardExpand all lines: docs/pip_repository.md
+5-7Lines changed: 5 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎examples/BUILD

Copy file name to clipboardExpand all lines: examples/BUILD
-5Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ bazel_integration_test(
3838
timeout = "long",
3939
)
4040

41-
bazel_integration_test(
42-
name = "relative_requirements_example",
43-
timeout = "long",
44-
)
45-
4641
bazel_integration_test(
4742
name = "bzlmod_example",
4843
)

‎examples/pip_install/BUILD

Copy file name to clipboardExpand all lines: examples/pip_install/BUILD
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ py_test(
8888
genquery(
8989
name = "yamllint_lib_by_version",
9090
expression = """
91-
attr("tags", "\\bpypi_version=1.26.3\\b", "@pip//pypi__yamllint")
91+
attr("tags", "\\bpypi_version=1.26.3\\b", "@pip_yamllint//:pkg")
9292
intersect
93-
attr("tags", "\\bpypi_name=yamllint\\b", "@pip//pypi__yamllint")
93+
attr("tags", "\\bpypi_name=yamllint\\b", "@pip_yamllint//:pkg")
9494
""",
9595
scope = [requirement("yamllint")],
9696
)
@@ -99,7 +99,7 @@ write_file(
9999
name = "write_expected",
100100
out = "expected",
101101
content = [
102-
"@pip//pypi__yamllint:pypi__yamllint",
102+
"@pip_yamllint//:pkg",
103103
"",
104104
],
105105
)

‎examples/pip_install/WORKSPACE

Copy file name to clipboardExpand all lines: examples/pip_install/WORKSPACE
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ pip_install(
5757
requirements = "//:requirements.txt",
5858
)
5959

60+
load("@pip//:requirements.bzl", "install_deps")
61+
62+
# Initialize repositories for all packages in requirements.txt.
63+
install_deps()
64+
6065
# You could optionally use an in-build, compiled python interpreter as a toolchain,
6166
# and also use it to execute pip.
6267
#

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.