使用bazel new_http_archive规则

时间:2017-10-27 15:36:10

标签: python python-requests bazel

我得到" ImportError:没有名为requests的模块"尝试使用new_http_archive规则获取请求时出错。

WORKSPACE:

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"]
)"""
)

BUILD:

py_library(
    name = "foo",
    deps = ["@requests//:srcs"],
    srcs = glob(["foo.py",]),
)

py_test(
    name = "foo_test",
    srcs = glob(["foo_test.py",]),
    deps = glob([":foo",]),
)

如果我使用' srcs = glob([" *"])'在new_http_archive规则中,我得到了关于缺少.py文件的各种错误(这使得sence - 请求存储库中存在各种文件)

我的问题是,如何以这样的方式指定build_file_content,它会给我一个工作请求库? (此时我不确定我是否正在使用正确的url,以及build_file_content的正确规则) 我只想用Bazel运行我的python代码,让Bazel管理提供请求库。

1 个答案:

答案 0 :(得分:3)

你非常接近。我们可以通过查看请求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"]),因此不匹配任何内容。要解决此问题,您可以使用strip_prefix规则的new_http_archive属性:

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"]
)"""
)