Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to embed Python interpreter? #80

Closed
axbycc-mark opened this issue Mar 23, 2024 · 5 comments
Closed

How to embed Python interpreter? #80

axbycc-mark opened this issue Mar 23, 2024 · 5 comments

Comments

@axbycc-mark
Copy link

Sorry I'm not so good at Bazel. The current doc says "embed Python, add @rules_python//python/cc:current_py_cc_libs as a dependency to your cc_binary."

But this doesn't seem enough because the header "pybind11/embed.h" would not be found. What additional dependencies should I add to build the following minimal example?

#include <pybind11/embed.h> // everything needed for embedding
namespace py = pybind11;

int main() {
    py::scoped_interpreter guard{}; // start the interpreter and keep it alive

    py::print("Hello, World!"); // use the Python API
}
@junyer
Copy link
Collaborator

junyer commented Mar 23, 2024

For something like "hello, world" tinkering, it might be easiest to use the pybind_library_test() rule, which will provide the headers and the libraries (i.e. don't specify @rules_python//python/cc:current_py_cc_libs in deps) and will output a C++ binary. We only just got it working again about a month ago, mind you, so please let me know if you encounter any problems!

@junyer junyer closed this as completed Mar 23, 2024
@axbycc-mark
Copy link
Author

I tried this out and I'm getting a runtime exception on Windows 10. Step by step debugging in Visual Studio takes me down into pylifecycle.c from the Python source code, which was originally at C:\Users\runneradmin\AppData\Local\Temp\python-build-ppgzl_qt\Python-3.11.7\Python\pylifecycle.c on my machine. I'm guessing Bazel put it there when it built the default hermetic host python toolchain. Is there a way for Bazel to regenerate this file so that I can continue debugging?

@junyer
Copy link
Collaborator

junyer commented Mar 27, 2024

Oh, whoa! Now I'm wondering whether such crashing might be related to "trying to take settings from the local environment" somehow? Apart from that, the hermetic Python toolchains are indygreg/python-build-standalone releases; Bazel doesn't build Python itself and so neither rules_python nor pybind11_bazel can do much to help here, AFAIK.

@axbycc-mark
Copy link
Author

I've investigated this issue much more and I posted my findings under the issue you linked. The resolution must wait on some new code on the python_rules side. It is also blocked on a bug fix for bazelbuild/rules_python#1823 which is currently in progress.

@junyer
Copy link
Collaborator

junyer commented Mar 28, 2024

Many thanks for digging into this! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants