#define PY_SSIZE_T_CLEAN
#include <Python.h>
int
main(int argc, char *argv[])
{
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
Py_SetProgramName(program); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
if (Py_FinalizeEx() < 0) {
exit(120);
}
PyMem_RawFree(program);
return 0;
}
I tried the example given by the official tutorial.
it worked on condition that the python path is set in environment path variables. What if the python environment is embeded in the C project, how can I assign the python path dynamically within the code. I've tried _putenv
method before the Python Virtual Machine is initiated, but in vain. As is instructed in the Python/C Api Doc, some function can be called before the initialization like Py_SetPath
or Py_SetPythonHome
in doc, it cast an error like "pythonxx.dll" cannot be found. I don't know how to do then?
gcc 9.2.0 mingw 32bit
windows10
python3.8.10 32bit