I\'m embedding Python into a C/C++ application that will have a defined API.
The application needs to instantiate classes defined in a script, which are structured r
you need to specify userscript
and not userscript.py
also use PyImport_ImportModule
it directly takes a char *
userscript.py
means module py
in package userscript
this code works for me:
#include <stdio.h>
#include <stdlib.h>
#include <Python.h>
int main(void)
{
const char *scriptDirectoryName = "/tmp";
Py_Initialize();
PyObject *sysPath = PySys_GetObject("path");
PyObject *path = PyString_FromString(scriptDirectoryName);
int result = PyList_Insert(sysPath, 0, path);
PyObject *pModule = PyImport_ImportModule("userscript");
if (PyErr_Occurred())
PyErr_Print();
printf("%p\n", pModule);
Py_Finalize();
return 0;
}