diff --git a/README.md b/README.md
index e708e079a9..6003cf6349 100644
--- a/README.md
+++ b/README.md
@@ -21,6 +21,7 @@ Status: This is **ALPHA** software.
### Packaging rules
* [pip_import](docs/pip.md#pip_import)
+* [pip3_import](docs/python/pip.md#pip3_import)
## Overview
@@ -95,6 +96,9 @@ load("@my_deps//:requirements.bzl", "pip_install")
pip_install()
```
+The `pip_import` rule uses the system `python` command, which is usually
+Python 2. `pip3_import` uses the system `python3` command.
+
## Consuming `pip` dependencies
Once a set of dependencies has been imported via `pip_import` and `pip_install`
diff --git a/python/pip.bzl b/python/pip.bzl
index b69eb9366e..ae258b0cbb 100644
--- a/python/pip.bzl
+++ b/python/pip.bzl
@@ -13,7 +13,7 @@
# limitations under the License.
"""Import pip requirements into Bazel."""
-def _pip_import_impl(repository_ctx):
+def _pip_import_impl(repository_ctx, binary_name):
"""Core implementation of pip_import."""
# Add an empty top-level BUILD file.
@@ -24,7 +24,7 @@ def _pip_import_impl(repository_ctx):
# To see the output, pass: quiet=False
result = repository_ctx.execute([
- "python",
+ binary_name,
repository_ctx.path(repository_ctx.attr._script),
"--name",
repository_ctx.attr.name,
@@ -39,6 +39,14 @@ def _pip_import_impl(repository_ctx):
if result.return_code:
fail("pip_import failed: %s (%s)" % (result.stdout, result.stderr))
+def _pip_system_import_impl(repository_ctx):
+ """System python implementation."""
+ _pip_import_impl(repository_ctx, "python")
+
+def _pip3_import_impl(repository_ctx):
+ """Python 3 implementation."""
+ _pip_import_impl(repository_ctx, "python3")
+
pip_import = repository_rule(
attrs = {
"requirements": attr.label(
@@ -51,14 +59,14 @@ pip_import = repository_rule(
cfg = "host",
),
},
- implementation = _pip_import_impl,
+ implementation = _pip_system_import_impl,
)
"""A rule for importing requirements.txt
dependencies into Bazel.
-This rule imports a requirements.txt
file and generates a new
-requirements.bzl
file. This is used via the WORKSPACE
-pattern:
+This rule imports a requirements.txt
file using the system
+python
, and generates a new requirements.bzl
file.
+This is used via the WORKSPACE
pattern:
pip_import(
name = "foo",
requirements = ":requirements.txt",
@@ -96,6 +104,63 @@ Args:
requirements: The label of a requirements.txt file.
"""
+pip3_import = repository_rule(
+ attrs = {
+ "requirements": attr.label(
+ mandatory = True,
+ allow_single_file = True,
+ ),
+ "_script": attr.label(
+ executable = True,
+ default = Label("//tools:piptool.par"),
+ cfg = "host",
+ ),
+ },
+ implementation = _pip3_import_impl,
+)
+
+"""A rule for importing requirements.txt
dependencies into Bazel.
+
+This rule imports a requirements.txt
file using the system
+python3
, and generates a new requirements.bzl
file.
+This is used via the WORKSPACE
pattern:
+pip3_import(
+ name = "foo",
+ requirements = ":requirements.txt",
+)
+load("@foo//:requirements.bzl", "pip_install")
+pip_install()
+
+
+You can then reference imported dependencies from your BUILD
+file with:
+load("@foo//:requirements.bzl", "requirement")
+py_library(
+ name = "bar",
+ ...
+ deps = [
+ "//my/other:dep",
+ requirement("futures"),
+ requirement("mock"),
+ ],
+)
+
+
+Or alternatively:
+load("@foo//:requirements.bzl", "all_requirements")
+py_binary(
+ name = "baz",
+ ...
+ deps = [
+ ":foo",
+ ] + all_requirements,
+)
+
+
+Args:
+ requirements: The label of a requirements.txt file.
+"""
+
def pip_repositories():
"""Pull in dependencies needed to use the packaging rules."""
# At the moment this is a placeholder, in that it does not actually pull in