使Bazel使用python 3 [英] make Bazel use python 3

查看:119
本文介绍了使Bazel使用python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Bazel中使用python 3运行py_test.

I would like to run my py_test with python 3 in Bazel.

py_library(
    name = "foo",
    srcs = ["foo.py"]
)

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

py_runtime(
    name = "python-3.6.3",
    files = [],
    interpreter_path = "/usr/local/bin/python3",
)

我能够使用命令来实现

bazel test --python_top=//path/to/foo:python-3.6.3 foo_test

但是,我想使用new_http_archive将python3导入bazel沙箱,并为py_runtime规则提供解释器路径,该规则指向bazel沙箱中的该http_archive.到目前为止,我仍然找不到什么解释器路径...我是否必须在py_runtime或其他地方引用http_archive标签?

However, I would like to import python3 to bazel sandbox with new_http_archive and provide the interpreter_path for the py_runtime rule that points to that http_archive within bazel sandbox. So far I am not able to find what is the interpreter_path... Do I have to reference the http_archive label somewhere from the py_runtime or somewhere else?

new_http_archive(
name = "python_version",
urls = ["https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz"],
strip_prefix = "Python-3.6.3",
build_file_content = """
py_library(
    name = "python_srcs",
    srcs = glob(["Lib/*.py"]),
    visibility = ["//visibility:public"]
)"""
)

推荐答案

您正在下载的tgz不包含解释器.它包含解释器的源代码.如果您想将解释器作为构建的一部分进行构建,则可以执行以下操作

The tgz that you're downloading doesn't contain an interpreter. It contains the source code for the interpreter. If you want to build the interpreter as part of your build, you could do something like this

new_http_archive(
    name = "python_version",
    urls = ["https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz"],
    strip_prefix = "Python-3.6.3",
    build_file_content = """
genrule(
    name = "build_python",
    srcs = glob(["**"]),
    outs = ["python"],
    cmd = "./external/python_version/configure && make && cp python $@",
    visibility = ["//visibility:public"],
)""",
)

然后您的py_runtime规则将设置解释器属性(而不是interpreter_path):

And then your py_runtime rule would set the interpreter attribute (not interpreter_path):

py_runtime(
    name = "python-3.6.3",
    files = [],
    interpreter = "@python_version//:python",
)

这篇关于使Bazel使用python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆