如何通过Bazel使用独立Python环境构建简易pybind库?
解决Bazel使用rules_python指定Python版本构建pybind扩展库的问题
问题说明
我正在搭建简易Bazel工作区构建pybind扩展库,希望通过rules_python拉取特定版本的Python(而非系统默认Python),但配置遇到阻碍:
- 移除
WORKSPACE中python_configure规则的python_interpreter_target参数时,构建可正常运行,但会使用系统Python; - 查询
@python3_9的所有目标后,仍无法确定python_interpreter_target的正确取值。
目录结构
. ├── BUILD ├── WORKSPACE └── example.cpp
BUILD文件内容
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension") pybind_extension( name = "example", srcs = ["example.cpp"], deps = [ ], )
WORKSPACE文件内容
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "rules_python", sha256 = "8c8fe44ef0a9afc256d1e75ad5f448bb59b81aba149b8958f02f7b3a98f5d9b4", strip_prefix = "rules_python-0.13.0", url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.13.0.tar.gz", ) load("@rules_python//python:repositories.bzl", "python_register_toolchains") python_register_toolchains( name = "python3_9", python_version = "3.9", ) load("@python3_9//:defs.bzl", "interpreter") load("@rules_python//python:pip.bzl", "pip_parse") pip_parse( python_interpreter_target = interpreter, ) http_archive( name = "pybind11_bazel", strip_prefix = "pybind11_bazel-9a24c33cbdc510fa60ab7f5ffb7d80ab89272799", urls = ["https://github.com/pybind/pybind11_bazel/archive/9a24c33cbdc510fa60ab7f5ffb7d80ab89272799.zip"], ) http_archive( name = "pybind11", build_file = "@pybind11_bazel//:pybind11.BUILD", strip_prefix = "pybind11-2.10.0", urls = ["https://github.com/pybind/pybind11/archive/refs/tags/v2.10.0.zip"], ) load("@pybind11_bazel//:python_configure.bzl", "python_configure") python_configure( name = "local_config_python", python_interpreter_target = "**what goes here**", )
example.cpp文件内容
#include <pybind11/pybind11.h> namespace py = pybind11; void do_something() { } PYBIND11_MODULE(example, m) { m.def("do_something", &do_something, "A function that does something"); }
查询@python3_9目标结果
% bazel query @python3_9//... @python3_9//:files @python3_9//:includes @python3_9//:libpython @python3_9//:pip @python3_9//:py3_runtime @python3_9//:python3 @python3_9//:python_headers @python3_9//:python_runtimes Loading: 0 packages loaded
解决方案
核心问题是**python_configure是旧版Python配置规则,与rules_python的toolchain机制不兼容**,无需纠结python_interpreter_target的取值,直接移除旧规则并配置toolchain即可:
清理WORKSPACE中的旧规则
删除以下内容:load("@pybind11_bazel//:python_configure.bzl", "python_configure") python_configure( name = "local_config_python", python_interpreter_target = "**what goes here**", )配置使用指定的Python toolchain
在项目根目录创建或修改.bazelrc文件,添加以下配置:build --python_version=3.9 build --toolchain=@python3_9//:py3_runtime这条配置会全局指定Bazel使用
@python3_9注册的Python 3.9版本进行构建。验证构建
重新执行构建命令,此时Bazel会使用rules_python拉取的Python 3.9,而非系统Python。
补充说明:@python3_9//:python3就是对应拉取的Python解释器目标,但由于python_configure属于旧规则体系,和新的toolchain无法协同工作,因此直接弃用旧规则是最简洁的解决方案。
内容的提问来源于stack exchange,提问作者digitalPhonix
相关产品推荐
相关产品推荐

