pybind11_bazel
pybind11_bazel copied to clipboard
Examples please.
This repo is nearly always the first hit when searching for bazel + pybind11. New to PyBind11, I am having a lot of trouble building a python module, then importing it into an embedded interpreter. All related searches are cmake oriented.
It would be great if the rules this bzlmod defined would be documented with examples.
Sorry, but just to confirm, are you looking for pybind11_bazel usage examples? Or are you actually looking for pybind11 usage examples?
I have the same issue. I have a very simple example I am trying to run. I keep getting the following error with bazel build /... :
ERROR: /home/dayd/.cache/bazel/_bazel_dayd/c693b355f9fcfefcc68f8a5aae9bbdac/external/rules_python/python/cc/BUILD.bazel:15:22: While resolving toolchains for target @@rules_python//python/cc:current_py_cc_headers (312a038): No matching toolchains found for types @@rules_python//python/cc:toolchain_type.
To debug, rerun with --toolchain_resolution_debug='@@rules_python//python/cc:toolchain_type'
If platforms or toolchains are a new concept for you, we'd encourage reading https://bazel.build/concepts/platforms-intro.
My workspace file is pretty simple:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
VERSION="0.32.2"
http_archive(
name = "rules_python",
#sha256 = SHA,
strip_prefix = "rules_python-{}".format(VERSION),
url = "https://github.com/bazelbuild/rules_python/releases/download/{}/rules_python-{}.tar.gz".format(VERSION,VERSION),
)
load("@rules_python//python:repositories.bzl", "py_repositories")
py_repositories()
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
python_register_toolchains(
name = "python_3_11",
# Available versions are listed in @rules_python//python:versions.bzl.
# We recommend using the same version your team is already standardized on.
python_version = "3.11.8",
)
load("@python_3_11//:defs.bzl", "interpreter")
http_archive(
name = "pybind11_bazel",
strip_prefix = "pybind11_bazel-2.12.0",
urls = ["https://github.com/pybind/pybind11_bazel/archive/v2.12.0.zip"],
)
# We still require the pybind library.
http_archive(
name = "pybind11",
build_file = "@pybind11_bazel//:pybind11-BUILD.bazel",
strip_prefix = "pybind11-2.12.0",
urls = ["https://github.com/pybind/pybind11/archive/v2.12.0.zip"],
)
and my BUILD file:
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
load("@rules_python//python:defs.bzl", "py_binary")
py_binary(
name = "main",
srcs = ["main.py"],
)
pybind_extension(
name = "my_pb_mod_ext",
srcs = ["my_pb_mod.cc"],
)
py_library(
name = "my_pb_mod",
data = [":my_pb_mod_ext.so"],
)
The py_binary there was just to see if I was using the rules_python stuff right.
Update - It seems that if I follow instructions fro 2.11.1 - seems to work. 2.12.0 - does not.
Quoting https://github.com/pybind/pybind11_bazel/issues/78#issuecomment-1992350163:
It's probably impossible to use rules_python from a
WORKSPACEfile with Bzlmod enabled. I suggest adopting Bzlmod at least partially: populate aMODULE.bazelfile to the extent possible; and then keep everything else in aWORKSPACE.bzlmodfile.
@roceb has very kindly contributed a basic example as per commit d85a646a002f1008e6c2429ba350e577d45ed61a. Is there anything else that folks would like to see?
I think the basic example is already very helpful and could be enough to close https://github.com/pybind/pybind11_bazel/issues/19, https://github.com/pybind/pybind11_bazel/issues/40, and this issue.
SGTM. Thanks!