10000 Support python interpreter target in pip_import. (#312) · alexeagle/rules_python@06672cd · GitHub
[go: up one dir, main page]

Skip to content

Commit 06672cd

Browse files
Kevin Kuandyscott
andauthored
Support python interpreter target in pip_import. (bazel-contrib#312)
* Support python interpreter target in pip_import. This allows users to use a custom python interpreter that is built by another repository rule instead of using a pre-built interpreter binary that is checked-in. This tangentially addresses bazel-contrib#257 since a common setup is to use the custom built interpreter in the python toolchain. For example, see: https://github.com/kku1993/bazel-hermetic-python * Actually use interpreter path. Co-authored-by: Andy Scott <andyscott@users.noreply.github.com>
1 parent e251c60 commit 06672cd

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

docs/pip.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## pip_import
66

77
<pre>
8-
pip_import(<a href="#pip_import-name">name</a>, <a href="#pip_import-extra_pip_args">extra_pip_args</a>, <a href="#pip_import-python_interpreter">python_interpreter</a>, <a href="#pip_import-requirements">requirements</a>, <a href="#pip_import-timeout">timeout</a>)
8+
pip_import(<a href="#pip_import-name">name</a>, <a href="#pip_import-extra_pip_args">extra_pip_args</a>, <a href="#pip_import-python_interpreter">python_interpreter</a>, <a href="#pip_import-python_interpreter_target">python_interpreter_target</a>, <a href="#pip_import-requirements">requirements</a>, <a href="#pip_import-timeout">timeout</a>)
99
</pre>
1010

1111
A rule for importing `requirements.txt` dependencies into Bazel.
@@ -86,6 +86,18 @@ wheels.
8686
</p>
8787
</td>
8888
</tr>
89+
<tr id="pip_import-python_interpreter_target">
90+
<td><code>python_interpreter_target</code></td>
91+
<td>
92+
<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>; optional
93+
<p>
94+
If you are using a custom python interpreter built by another repository rule,
95+
use this attribute to specify its BUILD target. This allows pip_import to invoke
96+
pip using the same interpreter as your toolchain. If set, takes precedence over
97+
python_interpreter.
98+
</p>
99+
</td>
100+
</tr>
89101
<tr id="pip_import-requirements">
90102
<td><code>requirements</code></td>
91103
<td>

python/pip.bzl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@ def _pip_import_impl(repository_ctx):
2222
# requirements.bzl without it.
2323
repository_ctx.file("BUILD", "")
2424

25+
interpreter_path = repository_ctx.attr.python_interpreter
26+
if repository_ctx.attr.python_interpreter_target != None:
27+
target = repository_ctx.attr.python_interpreter_target
28+
interpreter_path = repository_ctx.path(target)
29+
2530
args = [
26-
repository_ctx.attr.python_interpreter,
31+
interpreter_path,
2732
repository_ctx.path(repository_ctx.attr._script),
2833
"--python_interpreter",
29-
repository_ctx.attr.python_interpreter,
34+
interpreter_path,
3035
"--name",
3136
repository_ctx.attr.name,
3237
"--input",
@@ -56,6 +61,12 @@ pip_import = repository_rule(
5661
"python_interpreter": attr.string(default = "python", doc = """
5762
The command to run the Python interpreter used to invoke pip and unpack the
5863
wheels.
64+
"""),
65+
"python_interpreter_target": attr.label(allow_single_file = True, doc = """
66+
If you are using a custom python interpreter built by another repository rule,
67+
use this attribute to specify its BUILD target. This allows pip_import to invoke
68+
pip using the same interpreter as your toolchain. If set, takes precedence over
69+
python_interpreter.
5970
"""),
6071
"requirements": attr.label(
6172
mandatory = True,

0 commit comments

Comments
 (0)
0