• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2
3Import pip requirements into Bazel.
4
5<a id="whl_library_alias"></a>
6
7## whl_library_alias
8
9<pre>
10whl_library_alias(<a href="#whl_library_alias-name">name</a>, <a href="#whl_library_alias-default_version">default_version</a>, <a href="#whl_library_alias-repo_mapping">repo_mapping</a>, <a href="#whl_library_alias-version_map">version_map</a>, <a href="#whl_library_alias-wheel_name">wheel_name</a>)
11</pre>
12
13
14
15**ATTRIBUTES**
16
17
18| Name  | Description | Type | Mandatory | Default |
19| :------------- | :------------- | :------------- | :------------- | :------------- |
20| <a id="whl_library_alias-name"></a>name |  A unique name for this repository.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
21| <a id="whl_library_alias-default_version"></a>default_version |  Optional Python version in major.minor format, e.g. '3.10'.The Python version of the wheel to use when the versions from <code>version_map</code> don't match. This allows the default (version unaware) rules to match and select a wheel. If not specified, then the default rules won't be able to resolve a wheel and an error will occur.   | String | optional | <code>""</code> |
22| <a id="whl_library_alias-repo_mapping"></a>repo_mapping |  A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.&lt;p&gt;For example, an entry <code>"@foo": "@bar"</code> declares that, for any time this repository depends on <code>@foo</code> (such as a dependency on <code>@foo//some:target</code>, it should actually resolve that dependency within globally-declared <code>@bar</code> (<code>@bar//some:target</code>).   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | required |  |
23| <a id="whl_library_alias-version_map"></a>version_map |  -   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | required |  |
24| <a id="whl_library_alias-wheel_name"></a>wheel_name |  -   | String | required |  |
25
26
27<a id="compile_pip_requirements"></a>
28
29## compile_pip_requirements
30
31<pre>
32compile_pip_requirements(<a href="#compile_pip_requirements-name">name</a>, <a href="#compile_pip_requirements-extra_args">extra_args</a>, <a href="#compile_pip_requirements-extra_deps">extra_deps</a>, <a href="#compile_pip_requirements-generate_hashes">generate_hashes</a>, <a href="#compile_pip_requirements-py_binary">py_binary</a>, <a href="#compile_pip_requirements-py_test">py_test</a>,
33                         <a href="#compile_pip_requirements-requirements_in">requirements_in</a>, <a href="#compile_pip_requirements-requirements_txt">requirements_txt</a>, <a href="#compile_pip_requirements-requirements_darwin">requirements_darwin</a>, <a href="#compile_pip_requirements-requirements_linux">requirements_linux</a>,
34                         <a href="#compile_pip_requirements-requirements_windows">requirements_windows</a>, <a href="#compile_pip_requirements-visibility">visibility</a>, <a href="#compile_pip_requirements-tags">tags</a>, <a href="#compile_pip_requirements-kwargs">kwargs</a>)
35</pre>
36
37Generates targets for managing pip dependencies with pip-compile.
38
39By default this rules generates a filegroup named "[name]" which can be included in the data
40of some other compile_pip_requirements rule that references these requirements
41(e.g. with `-r ../other/requirements.txt`).
42
43It also generates two targets for running pip-compile:
44
45- validate with `bazel test [name]_test`
46- update with   `bazel run [name].update`
47
48If you are using a version control system, the requirements.txt generated by this rule should
49be checked into it to ensure that all developers/users have the same dependency versions.
50
51
52**PARAMETERS**
53
54
55| Name  | Description | Default Value |
56| :------------- | :------------- | :------------- |
57| <a id="compile_pip_requirements-name"></a>name |  base name for generated targets, typically "requirements".   |  none |
58| <a id="compile_pip_requirements-extra_args"></a>extra_args |  passed to pip-compile.   |  <code>[]</code> |
59| <a id="compile_pip_requirements-extra_deps"></a>extra_deps |  extra dependencies passed to pip-compile.   |  <code>[]</code> |
60| <a id="compile_pip_requirements-generate_hashes"></a>generate_hashes |  whether to put hashes in the requirements_txt file.   |  <code>True</code> |
61| <a id="compile_pip_requirements-py_binary"></a>py_binary |  the py_binary rule to be used.   |  <code>&lt;function py_binary&gt;</code> |
62| <a id="compile_pip_requirements-py_test"></a>py_test |  the py_test rule to be used.   |  <code>&lt;function py_test&gt;</code> |
63| <a id="compile_pip_requirements-requirements_in"></a>requirements_in |  file expressing desired dependencies.   |  <code>None</code> |
64| <a id="compile_pip_requirements-requirements_txt"></a>requirements_txt |  result of "compiling" the requirements.in file.   |  <code>None</code> |
65| <a id="compile_pip_requirements-requirements_darwin"></a>requirements_darwin |  File of darwin specific resolve output to check validate if requirement.in has changes.   |  <code>None</code> |
66| <a id="compile_pip_requirements-requirements_linux"></a>requirements_linux |  File of linux specific resolve output to check validate if requirement.in has changes.   |  <code>None</code> |
67| <a id="compile_pip_requirements-requirements_windows"></a>requirements_windows |  File of windows specific resolve output to check validate if requirement.in has changes.   |  <code>None</code> |
68| <a id="compile_pip_requirements-visibility"></a>visibility |  passed to both the _test and .update rules.   |  <code>["//visibility:private"]</code> |
69| <a id="compile_pip_requirements-tags"></a>tags |  tagging attribute common to all build rules, passed to both the _test and .update rules.   |  <code>None</code> |
70| <a id="compile_pip_requirements-kwargs"></a>kwargs |  other bazel attributes passed to the "_test" rule.   |  none |
71
72
73<a id="multi_pip_parse"></a>
74
75## multi_pip_parse
76
77<pre>
78multi_pip_parse(<a href="#multi_pip_parse-name">name</a>, <a href="#multi_pip_parse-default_version">default_version</a>, <a href="#multi_pip_parse-python_versions">python_versions</a>, <a href="#multi_pip_parse-python_interpreter_target">python_interpreter_target</a>,
79                <a href="#multi_pip_parse-requirements_lock">requirements_lock</a>, <a href="#multi_pip_parse-kwargs">kwargs</a>)
80</pre>
81
82NOT INTENDED FOR DIRECT USE!
83
84This is intended to be used by the multi_pip_parse implementation in the template of the
85multi_toolchain_aliases repository rule.
86
87
88**PARAMETERS**
89
90
91| Name  | Description | Default Value |
92| :------------- | :------------- | :------------- |
93| <a id="multi_pip_parse-name"></a>name |  the name of the multi_pip_parse repository.   |  none |
94| <a id="multi_pip_parse-default_version"></a>default_version |  the default Python version.   |  none |
95| <a id="multi_pip_parse-python_versions"></a>python_versions |  all Python toolchain versions currently registered.   |  none |
96| <a id="multi_pip_parse-python_interpreter_target"></a>python_interpreter_target |  a dictionary which keys are Python versions and values are resolved host interpreters.   |  none |
97| <a id="multi_pip_parse-requirements_lock"></a>requirements_lock |  a dictionary which keys are Python versions and values are locked requirements files.   |  none |
98| <a id="multi_pip_parse-kwargs"></a>kwargs |  extra arguments passed to all wrapped pip_parse.   |  none |
99
100**RETURNS**
101
102The internal implementation of multi_pip_parse repository rule.
103
104
105<a id="package_annotation"></a>
106
107## package_annotation
108
109<pre>
110package_annotation(<a href="#package_annotation-additive_build_content">additive_build_content</a>, <a href="#package_annotation-copy_files">copy_files</a>, <a href="#package_annotation-copy_executables">copy_executables</a>, <a href="#package_annotation-data">data</a>, <a href="#package_annotation-data_exclude_glob">data_exclude_glob</a>,
111                   <a href="#package_annotation-srcs_exclude_glob">srcs_exclude_glob</a>)
112</pre>
113
114Annotations to apply to the BUILD file content from package generated from a `pip_repository` rule.
115
116[cf]: https://github.com/bazelbuild/bazel-skylib/blob/main/docs/copy_file_doc.md
117
118
119**PARAMETERS**
120
121
122| Name  | Description | Default Value |
123| :------------- | :------------- | :------------- |
124| <a id="package_annotation-additive_build_content"></a>additive_build_content |  Raw text to add to the generated <code>BUILD</code> file of a package.   |  <code>None</code> |
125| <a id="package_annotation-copy_files"></a>copy_files |  A mapping of <code>src</code> and <code>out</code> files for [@bazel_skylib//rules:copy_file.bzl][cf]   |  <code>{}</code> |
126| <a id="package_annotation-copy_executables"></a>copy_executables |  A mapping of <code>src</code> and <code>out</code> files for [@bazel_skylib//rules:copy_file.bzl][cf]. Targets generated here will also be flagged as executable.   |  <code>{}</code> |
127| <a id="package_annotation-data"></a>data |  A list of labels to add as <code>data</code> dependencies to the generated <code>py_library</code> target.   |  <code>[]</code> |
128| <a id="package_annotation-data_exclude_glob"></a>data_exclude_glob |  A list of exclude glob patterns to add as <code>data</code> to the generated <code>py_library</code> target.   |  <code>[]</code> |
129| <a id="package_annotation-srcs_exclude_glob"></a>srcs_exclude_glob |  A list of labels to add as <code>srcs</code> to the generated <code>py_library</code> target.   |  <code>[]</code> |
130
131**RETURNS**
132
133str: A json encoded string of the provided content.
134
135
136<a id="pip_install"></a>
137
138## pip_install
139
140<pre>
141pip_install(<a href="#pip_install-requirements">requirements</a>, <a href="#pip_install-name">name</a>, <a href="#pip_install-kwargs">kwargs</a>)
142</pre>
143
144Accepts a locked/compiled requirements file and installs the dependencies listed within.
145
146```python
147load("@rules_python//python:pip.bzl", "pip_install")
148
149pip_install(
150    name = "pip_deps",
151    requirements = ":requirements.txt",
152)
153
154load("@pip_deps//:requirements.bzl", "install_deps")
155
156install_deps()
157```
158
159
160**PARAMETERS**
161
162
163| Name  | Description | Default Value |
164| :------------- | :------------- | :------------- |
165| <a id="pip_install-requirements"></a>requirements |  A 'requirements.txt' pip requirements file.   |  <code>None</code> |
166| <a id="pip_install-name"></a>name |  A unique name for the created external repository (default 'pip').   |  <code>"pip"</code> |
167| <a id="pip_install-kwargs"></a>kwargs |  Additional arguments to the [<code>pip_repository</code>](./pip_repository.md) repository rule.   |  none |
168
169
170<a id="pip_parse"></a>
171
172## pip_parse
173
174<pre>
175pip_parse(<a href="#pip_parse-requirements">requirements</a>, <a href="#pip_parse-requirements_lock">requirements_lock</a>, <a href="#pip_parse-name">name</a>, <a href="#pip_parse-kwargs">kwargs</a>)
176</pre>
177
178Accepts a locked/compiled requirements file and installs the dependencies listed within.
179
180Those dependencies become available in a generated `requirements.bzl` file.
181You can instead check this `requirements.bzl` file into your repo, see the "vendoring" section below.
182
183This macro wraps the [`pip_repository`](./pip_repository.md) rule that invokes `pip`.
184In your WORKSPACE file:
185
186```python
187load("@rules_python//python:pip.bzl", "pip_parse")
188
189pip_parse(
190    name = "pip_deps",
191    requirements_lock = ":requirements.txt",
192)
193
194load("@pip_deps//:requirements.bzl", "install_deps")
195
196install_deps()
197```
198
199You can then reference installed dependencies from a `BUILD` file with:
200
201```python
202load("@pip_deps//:requirements.bzl", "requirement")
203
204py_library(
205    name = "bar",
206    ...
207    deps = [
208       "//my/other:dep",
209       requirement("requests"),
210       requirement("numpy"),
211    ],
212)
213```
214
215In addition to the `requirement` macro, which is used to access the generated `py_library`
216target generated from a package's wheel, The generated `requirements.bzl` file contains
217functionality for exposing [entry points][whl_ep] as `py_binary` targets as well.
218
219[whl_ep]: https://packaging.python.org/specifications/entry-points/
220
221```python
222load("@pip_deps//:requirements.bzl", "entry_point")
223
224alias(
225    name = "pip-compile",
226    actual = entry_point(
227        pkg = "pip-tools",
228        script = "pip-compile",
229    ),
230)
231```
232
233Note that for packages whose name and script are the same, only the name of the package
234is needed when calling the `entry_point` macro.
235
236```python
237load("@pip_deps//:requirements.bzl", "entry_point")
238
239alias(
240    name = "flake8",
241    actual = entry_point("flake8"),
242)
243```
244
245## Vendoring the requirements.bzl file
246
247In some cases you may not want to generate the requirements.bzl file as a repository rule
248while Bazel is fetching dependencies. For example, if you produce a reusable Bazel module
249such as a ruleset, you may want to include the requirements.bzl file rather than make your users
250install the WORKSPACE setup to generate it.
251See https://github.com/bazelbuild/rules_python/issues/608
252
253This is the same workflow as Gazelle, which creates `go_repository` rules with
254[`update-repos`](https://github.com/bazelbuild/bazel-gazelle#update-repos)
255
256To do this, use the "write to source file" pattern documented in
257https://blog.aspect.dev/bazel-can-write-to-the-source-folder
258to put a copy of the generated requirements.bzl into your project.
259Then load the requirements.bzl file directly rather than from the generated repository.
260See the example in rules_python/examples/pip_parse_vendored.
261
262
263**PARAMETERS**
264
265
266| Name  | Description | Default Value |
267| :------------- | :------------- | :------------- |
268| <a id="pip_parse-requirements"></a>requirements |  Deprecated. See requirements_lock.   |  <code>None</code> |
269| <a id="pip_parse-requirements_lock"></a>requirements_lock |  A fully resolved 'requirements.txt' pip requirement file containing the transitive set of your dependencies. If this file is passed instead of 'requirements' no resolve will take place and pip_repository will create individual repositories for each of your dependencies so that wheels are fetched/built only for the targets specified by 'build/run/test'. Note that if your lockfile is platform-dependent, you can use the <code>requirements_[platform]</code> attributes.   |  <code>None</code> |
270| <a id="pip_parse-name"></a>name |  The name of the generated repository. The generated repositories containing each requirement will be of the form <code>&lt;name&gt;_&lt;requirement-name&gt;</code>.   |  <code>"pip_parsed_deps"</code> |
271| <a id="pip_parse-kwargs"></a>kwargs |  Additional arguments to the [<code>pip_repository</code>](./pip_repository.md) repository rule.   |  none |
272
273
274