Closed
Description
Issue description
When creating a module using the constructor directly instead of using a macro from pybind11
, and when defining multiple functions in it using py::exec
, there seem to be an issue. I'm not sure yet if the issue comes from py::exec
, but the following code works fine when py::exec
is replaced by py::module::import("builtins").attr("exec")
.
Reproducible example code
py::module my_module("my_module");
std::string my_code = "\n"
"def foo():\n"
" return 4\n"
"\n"
"def bar():\n"
" return foo()\n";
py::exec(my_code, py::globals(), my_module.attr("__dict__"));
py::print(my_module.attr("foo")()); // Prints "4" as expected
py::print(my_module.attr("bar")()); // Oops, "NameError: global name 'foo' is not defined"
Metadata
Metadata
Assignees
Labels
No labels