I am remote Debugging a c++ application with Visual studio on a linux device (raspberry pi/raspbian). In this c++ application I embedded a simple Python script by loading the function using the Python/c api. This is my c++ Code:
#include <Python.h>
int main(int argc, char* argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue;
// Initialize the Python Interpreter
Py_Initialize();
// Build the name object
pName = PyUnicode_FromString("//home//pi//projects//InfoBeam//WebScraperPython.txt");
// Load the module object
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, "pyMain");
if (PyCallable_Check(pFunc))
{
PyObject_CallObject(pFunc, NULL);
}
else
{
PyErr_Print();
}
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
return 0;
}
The Problem is, that I get a segmantation fault while running the function PyModule_GetDict(pModule)
. What am I doing wrong? This is the error message:
Program received signal SIGSEGV, Segmentation fault. 0x76bfdd28 in PyModule_GetDict () from /usr/lib/arm-linux-gnueabihf/libpython3.5m.so.1.0 Segmentation fault
EDIT: Okay, pModule was indeed NULL, probably because PyUnicode_FromString failed. Since PyImport_Import is failing: Where do I Need to save my Script, or: how do I pass the api the info where it is?