尝试在以下情况下使用pip_install结果: "Error in repository_rule: 'repository rule http_archive' can only be cal



考虑以下由三个文件组成的示例:

构建

load("@rules_python//python:pip.bzl", "pip_install")
pip_install(
requirements = ":requirements.txt",
)
py_binary(
name = "bin",
srcs = ["bin.py"],
)

工作空间

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_python",
sha256 = "954aa89b491be4a083304a2cb838019c8b8c3720a7abb9c4cb81ac7a24230cea",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_python/releases/download/0.4.0/rules_python-0.4.0.tar.gz",
"https://github.com/bazelbuild/rules_python/releases/download/0.4.0/rules_python-0.4.0.tar.gz",
],
)

bin.py

print('hello, world')

我得到以下错误:

> bazel run bin
ERROR: Traceback (most recent call last):
File "/data/d33tah/workspace/tmp/experiments/bazel/3-dependencies-without-docker/repro/BUILD", line 3, column 12, in <toplevel>
pip_install(
File "/home/d33tah/.cache/bazel/_bazel_d33tah/055ed32c3fa80a842a34f0252f6032c8/external/rules_python/python/pip.bzl", line 82, column 29, in pip_install
pip_install_dependencies()
File "/home/d33tah/.cache/bazel/_bazel_d33tah/055ed32c3fa80a842a34f0252f6032c8/external/rules_python/python/pip_install/repositories.bzl", line 67, column 14, in pip_install_dependencies
maybe(
File "/home/d33tah/.cache/bazel/_bazel_d33tah/055ed32c3fa80a842a34f0252f6032c8/external/bazel_tools/tools/build_defs/repo/utils.bzl", line 201, column 18, in maybe
repo_rule(name = name, **kwargs)
Error in repository_rule: 'repository rule http_archive' can only be called during workspace loading
ERROR: Skipping 'bin': no such target '//:bin': target 'bin' not declared in package '' defined by /data/d33tah/workspace/tmp/experiments/bazel/3-dependencies-without-docker/repro/BUILD
WARNING: Target pattern parsing failed.
ERROR: no such target '//:bin': target 'bin' not declared in package '' defined by /data/d33tah/workspace/tmp/experiments/bazel/3-dependencies-without-docker/repro/BUILD
INFO: Elapsed time: 0.091s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded)
FAILED: Build did NOT complete successfully (0 packages loaded)

创建空的requirements.txt没有帮助。此外,在任何相关的情况下:

> bazel version
Bazelisk version: v1.10.1
Build label: 4.2.2
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Thu Dec 2 18:15:58 2021 (1638468958)
Build timestamp: 1638468958
Build timestamp as int: 1638468958

我做错了什么?我该如何解决?

Matt Mackay在Bazel Slack上立即找到了解决方案:

pip_install是一个存储库规则,因此它应该在WORKSPACE文件中。https://github.com/bazelbuild/rules_python/blob/main/docs/pip.md#pip_install

詹姆斯也在那里指出";jsharpe";夏普:

快速查看,您在BUILD文件中调用pip_install,而它应该在WORKSPACE文件中。

以下是更正后的代码:

构建


py_binary(
name = "bin",
srcs = ["bin.py"],
)

工作空间

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_python",
sha256 = "954aa89b491be4a083304a2cb838019c8b8c3720a7abb9c4cb81ac7a24230cea",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_python/releases/download/0.4.0/rules_python-0.4.0.tar.gz",
"https://github.com/bazelbuild/rules_python/releases/download/0.4.0/rules_python-0.4.0.tar.gz",
],
)
load("@rules_python//python:pip.bzl", "pip_install")
pip_install(
requirements = ":requirements.txt",
)

bin.py

print('hello, world')

requirements.txt

# intentionally left empty

最新更新