C++ application crashes with embedded Python code

时间:2018-07-24 10:07:42

标签: python c++ embedding

I'm trying to run a Python code within a C++ application, based of example from this tutorial. Everything works fine with just running a script:

char filename[] = "D:\\Docs\\Embedding\\bin\\Debug\\load_continiuous_snl_data.py";
FILE* fp;

Py_Initialize();

fp = _Py_fopen(filename, "r");
PyRun_SimpleFile(fp, filename);

Py_Finalize();

But when I try to import script as a module, application crashes

std::vector<int> ExampleData(5);
for(int i=0;i<5;i++)
{
    ExampleData[i] = i+1;
}

PyObject *pName,*pModule,*pFunc,*pname;
PyObject *pValue,*pArgs,*pDict;
Py_Initialize();
pName = PyUnicode_FromString("D:\\Docs\\Embedding\\bin\\Debug\\load_continiuous_snl_data.py");

//pModule = PyImport_Import(pname); //running without this line doesn't lead to crashing

Py_Finalize();

I'm using Python 3.7 32bit on Windows. Thanks in advance for any suggestion.

0 个答案:

没有答案