Skip to content
Advertisement

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

I have a C++ Library with Python Bindings built as follows, and a Python Binary that needs to import the libPerceptionPybind.so that is generated.

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")
    ],
    
)

I see that the libPerceptionPybind.so has been generated in my bazel-bin/pybind folder. I tried to add PerceptionPybind to the deps as you can see, but it gives an error:

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

Advertisement

Answer

You can use the data field to have your dynamic library copied into the Python binary target folder:

py_binary(
    name = "TestPerceptionPybind",
    srcs = [ "TestPerceptionPybind.py" ],
    deps = [
        requirement("numpy"),
        requirement("torch")
    ],
    data = [
        ":PerceptionPybind",
    ],
)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement