Bazel,如何将 C++ 内置的 Python 库添加到 Python 二进制文件中?

Bazel, how to add Python library built in C++ into Python Binary?

我有一个带有 Python 绑定的 C++ 库,如下所示,还有一个需要导入生成的 libPerceptionPybind.so 的 Python 二进制文件。

package(default_visibility = ["//visibility:public"])
load("@pip_pybind//:requirements.bzl", "requirement")

cc_binary(
    name = "PerceptionPybind",
    srcs = ["PerceptionPybind.cpp"],
    deps = [
        "@pybind11",
        "@libtorch_cpu//:torch_cpu",
    ],
    linkshared = True,
)

py_binary(
    name = "TestPerceptionPybind",
    srcs = [ "TestPerceptionPybind.py" ],
    deps = [
        ":PerceptionPybind"
        requirement("numpy"),
        requirement("torch")
    ],
    
)

我看到 libPerceptionPybind.so 已在我的 bazel-bin/pybind 文件夹中生成。如您所见,我尝试将 PerceptionPybind 添加到 deps,但出现错误:

//pybind:PerceptionPybind' does not have mandatory providers: 'py' or 'PyInfo

您可以使用 data 字段将您的动态库复制到 Python 二进制目标文件夹中:

py_binary(
    name = "TestPerceptionPybind",
    srcs = [ "TestPerceptionPybind.py" ],
    deps = [
        requirement("numpy"),
        requirement("torch")
    ],
    data = [
        ":PerceptionPybind",
    ],
)