pybind11_bazel icon indicating copy to clipboard operation
pybind11_bazel copied to clipboard

Examples please.

Open mmes opened this issue 1 year ago • 1 comments

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.

mmes avatar May 07 '24 21:05 mmes

Sorry, but just to confirm, are you looking for pybind11_bazel usage examples? Or are you actually looking for pybind11 usage examples?

junyer avatar May 08 '24 15:05 junyer

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.

tenspd137 avatar May 19 '24 17:05 tenspd137

Update - It seems that if I follow instructions fro 2.11.1 - seems to work. 2.12.0 - does not.

tenspd137 avatar May 19 '24 17:05 tenspd137

Quoting https://github.com/pybind/pybind11_bazel/issues/78#issuecomment-1992350163:

It's probably impossible to use rules_python from a WORKSPACE file with Bzlmod enabled. I suggest adopting Bzlmod at least partially: populate a MODULE.bazel file to the extent possible; and then keep everything else in a WORKSPACE.bzlmod file.

junyer avatar May 20 '24 20:05 junyer

@roceb has very kindly contributed a basic example as per commit d85a646a002f1008e6c2429ba350e577d45ed61a. Is there anything else that folks would like to see?

junyer avatar Jun 21 '24 17:06 junyer

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.

hofbi avatar Jun 24 '24 17:06 hofbi

SGTM. Thanks!

junyer avatar Jun 25 '24 14:06 junyer