0

So in visual studio c++ application, I have a bit where I need to call python script

Py_Initialize();

PyObject *obj = Py_BuildValue("s", "..\\..\\SDK\\scripts\\JsonToExcel.py");

FILE *file = _Py_fopen_obj(obj, "r+");

if (file != NULL) {

`PyRun_SimpleFile(file, "..\\..\\SDK\\scripts\\`[`JsonToExcel.py`](https://JsonToExcel.py)`");`
}

Py_Finalize();

In the JsonToExcel.py I import pandas. So when I run the application and call this program it executes perfectly, but when I call the program once again I get an error for line "import pandas", saying

C:\A\31\s\Objects\structseq.c:401: bad argument to internal function

This error does not occur if I close the application and run it again

I saw a similar problem in this stack overflow question

Python C API error restarting interpreter

But there wasn't any solution.

Any ways to resolve this?

  • Python modules like Pandas do not clean up properly when you call `Py_Finalize`. Rewrite the script to be a proper module so you can have ONE python interpreter for the entire lifetime of your program and call a function within the module several times. – Botje Apr 07 '23 at 10:29

0 Answers0