Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pybind11/pybind11.h>
- #include <pybind11/embed.h>
- #include <iostream>
- namespace py = pybind11;
- PYBIND11_EMBEDDED_MODULE(testmodule, m)
- {
- m.def("start", [](py::module& mod) {
- using namespace pybind11::literals;
- auto import_fn = py::globals()["__builtins__"].attr("__import__");
- auto bar = import_fn(mod.attr("__name__")
- , "fromlist"_a=py::make_tuple("bar")
- ).attr("bar");
- py::print(bar.attr("test"));
- });
- }
- int main()
- {
- py::scoped_interpreter guard{};
- try {
- py::exec(R"(\
- import testmodule
- import foo
- testmodule.start(foo)
- )");
- } catch (py::error_already_set& e) {
- std::cerr << e.what() << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement