使用 bazel new_http_archive 规则导入 python 请求
Import python requests with bazel new_http_archive rule
我在尝试使用 new_http_archive 规则获取请求时遇到 "ImportError: No module named requests" 错误。
工作空间:
new_http_archive(
name = "requests",
urls = ["https://github.com/requests/requests/tarball/master/requests-requests-v2.18.4-90-g81b6341.tar.gz"],
build_file_content = """
py_library(
name = "srcs",
srcs = glob(["requests/*.py"]),
visibility = ["//visibility:public"]
)"""
)
构建:
py_library(
name = "foo",
deps = ["@requests//:srcs"],
srcs = glob(["foo.py",]),
)
py_test(
name = "foo_test",
srcs = glob(["foo_test.py",]),
deps = glob([":foo",]),
)
如果我在 new_http_archive 规则中使用 'srcs = glob(["*"])',我会收到关于丢失 .py 文件的各种错误(这是有道理的 - 请求存储库中有各种文件)
我的问题是,如何指定 build_file_content 以便为我提供工作请求库?
(此时我不确定我是否使用了 right url,以及 build_file_content 的 right 规则)
我只想能够 运行 我的 python 代码与 Bazel 并让 Bazel 管理提供请求库。
你们很亲近。我们可以通过查看请求 tar.gz :
来查看问题
$ tar -tf requests-requests-v2.18.4-90-g81b6341.tar.gz
...
requests-requests-81b6341/requests/adapters.py
requests-requests-81b6341/requests/api.py
requests-requests-81b6341/requests/auth.py
requests-requests-81b6341/requests/certs.py
requests-requests-81b6341/requests/compat.py
requests-requests-81b6341/requests/cookies.py
...
所以所有文件都在名为 requests-requests-81b6341 的目录中。由于您的 BUILD 文件中包含 glob(["requests/*.py"])
,因此这与任何内容都不匹配。要解决这个问题,可以使用 new_http_archive
规则的 strip_prefix
属性:
new_http_archive(
name = "requests",
urls = ["https://github.com/requests/requests/tarball/master/requests-requests-v2.18.4-90-g81b6341.tar.gz"],
strip_prefix = "requests-requests-81b6341",
build_file_content = """
py_library(
name = "srcs",
srcs = glob(["requests/*.py"]),
visibility = ["//visibility:public"]
)"""
)
我在尝试使用 new_http_archive 规则获取请求时遇到 "ImportError: No module named requests" 错误。
工作空间:
new_http_archive(
name = "requests",
urls = ["https://github.com/requests/requests/tarball/master/requests-requests-v2.18.4-90-g81b6341.tar.gz"],
build_file_content = """
py_library(
name = "srcs",
srcs = glob(["requests/*.py"]),
visibility = ["//visibility:public"]
)"""
)
构建:
py_library(
name = "foo",
deps = ["@requests//:srcs"],
srcs = glob(["foo.py",]),
)
py_test(
name = "foo_test",
srcs = glob(["foo_test.py",]),
deps = glob([":foo",]),
)
如果我在 new_http_archive 规则中使用 'srcs = glob(["*"])',我会收到关于丢失 .py 文件的各种错误(这是有道理的 - 请求存储库中有各种文件)
我的问题是,如何指定 build_file_content 以便为我提供工作请求库? (此时我不确定我是否使用了 right url,以及 build_file_content 的 right 规则) 我只想能够 运行 我的 python 代码与 Bazel 并让 Bazel 管理提供请求库。
你们很亲近。我们可以通过查看请求 tar.gz :
来查看问题$ tar -tf requests-requests-v2.18.4-90-g81b6341.tar.gz
...
requests-requests-81b6341/requests/adapters.py
requests-requests-81b6341/requests/api.py
requests-requests-81b6341/requests/auth.py
requests-requests-81b6341/requests/certs.py
requests-requests-81b6341/requests/compat.py
requests-requests-81b6341/requests/cookies.py
...
所以所有文件都在名为 requests-requests-81b6341 的目录中。由于您的 BUILD 文件中包含 glob(["requests/*.py"])
,因此这与任何内容都不匹配。要解决这个问题,可以使用 new_http_archive
规则的 strip_prefix
属性:
new_http_archive(
name = "requests",
urls = ["https://github.com/requests/requests/tarball/master/requests-requests-v2.18.4-90-g81b6341.tar.gz"],
strip_prefix = "requests-requests-81b6341",
build_file_content = """
py_library(
name = "srcs",
srcs = glob(["requests/*.py"]),
visibility = ["//visibility:public"]
)"""
)