I have a cpp code like this:
void callPython() {
Py_Initialize();
PyObject* sysPath = PySys_GetObject("path");
PyList_Append(sysPath, PyUnicode_FromString("/jarvis_repo/src/cpp/packages/jarvis/nlp/"));
// Load the module
PyObject *pName = PyUnicode_FromString("my_mod");
PyObject *pModule = PyImport_Import(pName);
if (pModule != NULL) {
std::cout << "Python module found\n";
PyObject* pFunc = PyObject_GetAttrString(pModule, "my_func");
if(pFunc != NULL){
PyObject_CallObject(pFunc, NULL);
} else {
std::cout << "Couldn't find func\n";
}
}
else {
PyErr_Print();
std::cout << "Python Module not found\n";
}
Py_Finalize();
}
I also have two files my_mod.py and test.py in the same directory /jarvis_repo/src/cpp/packages/jarvis/nlp/
as follows:
my_mod.py
from test import coreDM
def my_func():
print("my_func() got called")
coreDM()
test.py
class coreDM():
def __init__(self):
print("Initialized test")
def print_message():
print("Hello from coreDM")
When from test import coreDM
and coreDM()
is omitted from my_mod.py
,
PyImport_Import works fine and prints my_func() got called
else it returns NULL. Any ideas why this might be happening?
Thanks in advance!
ERROR Message:
ImportError: cannot import name 'coreDM'
Python Module not found