我如何 select bazel 中 Python 和 pip 的运行时?
How do I select the runtime in bazel for Python and pip?
我正在尝试在 Ubuntu 20.04 上构建应用程序,其中 python3 指向 Python3.8,我正在构建 aganist Python3.6
我在 WORKSPACE 的同一目录中有以下运行时。
$ cat BUILD.bazel
py_runtime(
name = "python3.6",
interpreter_path = "/usr/bin/python3.6",
)
我尝试通过 运行 以下内容构建应用程序,但 bazel 仍然指向 python3 即 python3.8
bazelisk build company/app_api:app --python_top=//:python3.6
我也尝试过弃用的选项,但也没有用。
bazelisk build company/app_api:app --python_path=/usr/bin/python3.6
这是我得到的错误:
...
subprocess.CalledProcessError: Command '['/usr/bin/python3', '-m', 'pip', '--isolated', 'wheel', '-r', '/source_code/src/python/third_party/requirements.txt']' returned non-zero exit status 1.
...
pip 正在尝试安装仅适用于 python3.6 的软件包,这就是它返回非零存在代码的原因。
如何强制 bazel 使用自定义 python 解释器?
py_runtime
通常必须与 py_runtime_pair
和 toolchain
一起使用。请参阅 py_runtime_pair
文档中的 the example。该示例稍作修改以适用于 OP 如下所示:
load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")
py_runtime(
name = "python3.6",
interpreter_path = "/usr/bin/python3.6",
python_version = "PY3",
)
py_runtime_pair(
name = "py3.6",
py3_runtime = ":python3.6",
)
toolchain(
name = "py3-tc",
toolchain = ":py3.6",
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
)
然后可以通过将 register_toolchains("//path/to/python3:py3-tc")
放入 WORKSPACE
文件或传递 --extra_toolchains //path/to/python3:py3-tc
命令行标志来使用新工具链。
在 WORKSPACE 中将 python_interpreter 添加到 pip_install。
pip_install(
requirements = "//third_party:requirements.txt",
python_interpreter = 'python3.6'
)
如果您使用来自 bazelbuild/rules_python, then there is no probably a clean way to do this like reusing a python interpreter from the toolchain. python3
is hardcoded in the rule definition
的 pip 支持
遵循 documentation 下面的示例应该适用于 python
版本 3.6.0
。我认为 bazel
只是不遵循符号链接并回退到 python
.
的系统默认版本
py_runtime(
name = "python-3.6.0",
interpreter_path = "/opt/pyenv/versions/3.6.0/bin/python",
)
您还可以按照 所述重新定义 python 工具链。
我正在尝试在 Ubuntu 20.04 上构建应用程序,其中 python3 指向 Python3.8,我正在构建 aganist Python3.6
我在 WORKSPACE 的同一目录中有以下运行时。
$ cat BUILD.bazel
py_runtime(
name = "python3.6",
interpreter_path = "/usr/bin/python3.6",
)
我尝试通过 运行 以下内容构建应用程序,但 bazel 仍然指向 python3 即 python3.8
bazelisk build company/app_api:app --python_top=//:python3.6
我也尝试过弃用的选项,但也没有用。
bazelisk build company/app_api:app --python_path=/usr/bin/python3.6
这是我得到的错误:
...
subprocess.CalledProcessError: Command '['/usr/bin/python3', '-m', 'pip', '--isolated', 'wheel', '-r', '/source_code/src/python/third_party/requirements.txt']' returned non-zero exit status 1.
...
pip 正在尝试安装仅适用于 python3.6 的软件包,这就是它返回非零存在代码的原因。
如何强制 bazel 使用自定义 python 解释器?
py_runtime
通常必须与 py_runtime_pair
和 toolchain
一起使用。请参阅 py_runtime_pair
文档中的 the example。该示例稍作修改以适用于 OP 如下所示:
load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")
py_runtime(
name = "python3.6",
interpreter_path = "/usr/bin/python3.6",
python_version = "PY3",
)
py_runtime_pair(
name = "py3.6",
py3_runtime = ":python3.6",
)
toolchain(
name = "py3-tc",
toolchain = ":py3.6",
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
)
然后可以通过将 register_toolchains("//path/to/python3:py3-tc")
放入 WORKSPACE
文件或传递 --extra_toolchains //path/to/python3:py3-tc
命令行标志来使用新工具链。
在 WORKSPACE 中将 python_interpreter 添加到 pip_install。
pip_install(
requirements = "//third_party:requirements.txt",
python_interpreter = 'python3.6'
)
如果您使用来自 bazelbuild/rules_python, then there is no probably a clean way to do this like reusing a python interpreter from the toolchain. python3
is hardcoded in the rule definition
遵循 documentation 下面的示例应该适用于 python
版本 3.6.0
。我认为 bazel
只是不遵循符号链接并回退到 python
.
py_runtime(
name = "python-3.6.0",
interpreter_path = "/opt/pyenv/versions/3.6.0/bin/python",
)
您还可以按照