1

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
  • 1
    Have you tried displaying the exception message? `NULL` indicates an error, the message may tell you what is wrong. Odds are something is wrong with `coreDM` that's raising an exception on import (usually a `SyntaxError` or a recursive dependency on `dmEngine`), but without a [MCVE], we can't say much beyond that. – ShadowRanger Feb 05 '20 at 21:40
  • Added minimal reproducible example. BTW how to display the exception message from `PyImport_Import`? – Shubhadeep Das Feb 06 '20 at 05:34
  • I figured out how to print the error message. I used `PyErr_Print();`. Updated question with the error message received. – Shubhadeep Das Feb 06 '20 at 05:48
  • 1
    `test` is always a dublious name for a module because the CPython testsuite is called `test`. – DavidW Feb 06 '20 at 12:35
  • 1
    Tried changing test.py to some other name. Still didn't work. – Shubhadeep Das Feb 07 '20 at 09:38
  • `from .modulename import coreDM`? Note the extra dot – DavidW Feb 07 '20 at 13:12

1 Answers1

1

Use PyList_Insert in place of PyList_Append to get test imported from the location you want.

as @DavidW mentioned there is an importable module in the core libs called test.

change

PyList_Append(sysPath, PyUnicode_FromString("/jarvis_repo/src/cpp/packages/jarvis/nlp/"));

to

PyList_Insert(sysPath, 0, PyUnicode_FromString("/jarvis_repo/src/cpp/packages/jarvis/nlp/"));

so the test module is first found in /jarvis_repo/src/cpp/packages/jarvis/nlp/ and not in core libs.

Note: you should rename test instead

Szabolcs Dombi
  • 5,493
  • 3
  • 39
  • 71