首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用密封Python安装使用Bazel构建简单的pybind库

如何使用密封Python安装使用Bazel构建简单的pybind库
EN

Stack Overflow用户
提问于 2022-10-20 21:32:19
回答 1查看 102关注 0票数 0

我正在尝试设置一个简单的Bazel工作区来构建pybind库,并且我无法让Bazel使用特定的非系统python (使用rules_python Bazel提取)。

这是我的设计:

代码语言:javascript
运行
复制
.
├── BUILD
├── WORKSPACE
└── example.cpp

建造:

代码语言:javascript
运行
复制
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")

pybind_extension(
    name = "example",
    srcs = ["example.cpp"],
    deps = [ ],
)

工作空间:

代码语言:javascript
运行
复制
oad("@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:

代码语言:javascript
运行
复制
#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");
}

如果我将python_interpreter_target = "**what goes here**"行从WORKSPACE中删除,那么一切都可以工作,但它使用的是系统python,而这正是我所要避免的。但是我找不到bazel的目标-- python3二进制文件是从rules_bazel pakage拉出的。

如果有帮助,我可以看到@python3_9的所有目标

代码语言:javascript
运行
复制
% 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_interpreter_target想要的)

EN

回答 1

Stack Overflow用户

发布于 2022-10-26 11:25:50

请尝试将实际路径传递给目标python可执行文件。请将python可执行文件签入third_party dir,而不是使用python调用rules_python,而是将相对于工作区的可执行文件的相对路径传递给该可执行文件。谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74146522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档