I use Python 3.4 and Visual 2010. I'm embedding Python using the C API to give the user some script capabilities in processing his data. I call python functions defined by the user from my C++ code. I call specific function like Apply() for example that the user has to define in a Python file. Suppose the user has a file test.py where he has defined a function Apply() that process some data. All I have to do is to import his module and get a "pointer" to his python function from the C++.
PySys_SetPath(file_info.absolutePath().toUtf8().data()));
m_module = PyImport_ImportModule(module_name.toUtf8().data());
if (m_module)
{
m_apply_function = PyObject_GetAttrString(m_module, "Apply");
m_main_dict = PyModule_GetDict(m_module);
}
So far, so good. But if the user modifies his script, the new version of his function is never taken into account. I have to reboot my program to make it work... I read somewhere that I need to reload the module and get new pointers on functions but the PyImport_ReloadModule returns NULL with "Import error".
// .... code ....
// Reload the module
m_module = PyImport_ReloadModule(m_module);
Any ideas ? Best regards, Poukill
importlib.reload(module)? CallingPyImport_ImportModulethe second time is pointless; it probably just returns the cached value fromsys.modules. Make surePySys_SetPathargument contains all necessary paths (including pythonpath that is necessary for the user script).PyErr_Fetch(&ptype, &pvalue, &ptraceback), but it's not that easy. Restarting the interpreter may be a option, but clearly not the best as I would have to stop any running session.