【问题标题】:Import python requests with bazel new_http_archive rule使用 bazel new_http_archive 规则导入 python 请求
【发布时间】:2017-10-28 03:54:51
【问题描述】:

我在尝试使用 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 以提供一个工作请求库? (此时我不确定我是否使用了正确的 url,以及 build_file_content 的正确规则) 我只想能够使用 Bazel 运行我的 python 代码,并让 Bazel 管理提供请求库。

【问题讨论】:

    标签: python python-requests bazel


    【解决方案1】:

    你很亲密。我们可以通过查看请求 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"]
    )"""
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 2022-06-15
      • 1970-01-01
      相关资源
      最近更新 更多