There is a python code:

test.py:

from test2 import Say Say() 

test2.py:

 def Say(): print("Mew") 

And there is c ++ code:

 #include <Python.h> int main(){ Py_Initialize(); // инициализация интерпретатора PyObject *obj = Py_BuildValue("s", "test.py"); FILE *file = _Py_fopen_obj(obj, "r+"); if(file != NULL) { PyRun_SimpleFile(file, "test.py"); } Py_Finalize(); return 0; } 

It is compiled, but when trying to execute it gives the following

 File "test.py", line 1, in <module> from test2 import Say ModuleNotFoundError: No module named 'test2' 

What am I doing wrong, thanks in advance!

    1 answer 1

    The fact is that when the interpreter is launched from C-code, the current directory is not added to sys.path . This can be done manually, like this:

     PyObject *sysPath = PySys_GetObject("path"); PyList_Append(sysPath, PyUnicode_FromString("."));