Skip to content Skip to sidebar Skip to footer

Python C Extension: Pyeval_getlocals() Returns Null

I need to read local variables from Python in C/C++. When I try to PyEval_GetLocals, I get a NULL. This happens although Python is initialized. The following is a minimal example.

Solution 1:

Turns out the right way to access variables in the scope is:

Py_Initialize();
PyObject *main = PyImport_AddModule("__main__");
PyObject *globals = PyModule_GetDict(main);
PyObject *a = PyDict_GetItemString(globals, "a");
std::cout<<globals<<std::endl; //Not NULL
Py_Finalize();

Post a Comment for "Python C Extension: Pyeval_getlocals() Returns Null"