reading a global variable of python in c

后端 未结 2 765
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 04:10

I am trying to learn how to use the Python/C API correctly - all I actually need to do is to read a global variable (in my case dictionary - but I\'m starting with a simple

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-20 04:50

    You should call PyObject_GetAttrString on the result of PyImport_ImportModule. I have no idea why you think that the __main__ module should define that variable:

    PyObject *mod = PyImport_ImportModule("tryStuff");
    PyObject *var1Py = PyObject_GetAttrString(mod, "var1");
    

    You should also add the check on the results, since PyImport_ImportModule can return NULL when the import fails.

提交回复
热议问题