pybind / pybind11_bazel

Bazel wrapper around the pybind11 repository
Other
97 stars 54 forks source link

Add example for embedding python in a C++ application #92

Open JBPennington opened 1 month ago

JBPennington commented 1 month ago

I've started an example repo for embedding python in a C++ program and I'd like add it to the examples here. I'm having trouble getting the two example files to work. The goal is to use a hermetic C++ toolchain and hermetic python toolchain to produce a plot with matplotlib. I've cobbled this together from the other Issue How to set python interpreter within cc_binary #77

JBPennington commented 1 month ago

Could someone help me at least get the plotting application up and running?

rwgk commented 1 month ago

Sorry I cannot help, except let you know that we've tragically lost our pybind11_bazel maintainer.

https://github.com/google/re2/issues/502

I hope someone else will step up: Please contact me for admin permissions.

jiawen commented 1 month ago

Could someone help me at least get the plotting application up and running?

I can give it a shot. Can you point me to any necessary build instructions beyond the basics (I'm familiar with rules_python's hermetic Python toolchain but less so with a hermetic C++ one).

Is the issue the Python side with matplotlib?

jiawen commented 1 month ago

Sorry I cannot help, except let you know that we've tragically lost our pybind11_bazel maintainer.

https://github.com/google/re2/issues/502

I hope someone else will step up: Please contact me for admin permissions.

Ouch, that's tragic news indeed. I'm an Xoogler and current active pybind11_bazel user. I'm happy to pitch in with maintaining the project in whatever capacity I can. @junyer and I worked on a few small PRs for this repo in the past.

rwgk commented 1 month ago

@jiawen I just sent an invite for adding you with admin permissions.

@rickeylev for awareness.

JBPennington commented 1 month ago

@jiawen Thanks for the help. Yes there are two binaries in the BUILD.bazel. One ("embed") is from the examples provided in the other issue. You can ignore that one.

The primary one I'm looking to get functional is "plot". I've had success with this using a local python repo and direct python calls, but I've had trouble reproducing that work with a hermetic python instance and pybind. The hermetic C++ toolchain works fine. The issue is currently in finding the associated libraries of the python toolchain (I think). I think the reason this worked with a local python instance is because it just used the local PATH variables to find the libraries needed (such as numpy).

JBPennington commented 1 month ago

Sorry I cannot help, except let you know that we've tragically lost our pybind11_bazel maintainer.

google/re2#502

I hope someone else will step up: Please contact me for admin permissions.

I'm so sorry to hear that.

JBPennington commented 1 week ago

I have an example up and running. https://github.com/JBPennington/pybind_cpp_w_python_example

The only current issue is that I can't create a cc_library with the python dependency because the current implementation requires setting env variables in the cc_binary or cc_test and cc_library doesn't support the env variable. It would be great to enforce that as a transitive property on the binary. I could instead set those as defines and set the env variables in the lib call.