Python inline code in C ++ - error importing python libraries

I am trying to use the Python 3.5 interpreter built into a C ++ program to get an image from C ++ and use it as input for my trained tensor flow model. First, I convert my image into a numpy array, and then send it to python. This is my simplified code that works great (codes accepted here ):

Python Code:

def multiply_fun(M): V = M*2 print(V) 

My C ++ code that calls the function above:

 #include <Python.h> #include <abstract.h> #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #include <ndarrayobject.h> #include <vector> int main() { Py_InitializeEx(1); PyObject* sysPath = PySys_GetObject((char*)"path"); PyObject* curDir = PyUnicode_FromString("."); PyList_Append(sysPath, curDir); Py_DECREF(curDir); PyObject* python_code = PyImport_ImportModule("python_code"); PyObject* multiply_fun = PyObject_GetAttrString(python_code, "multiply_fun"); Py_XDECREF(python_code); import_array1(-1); npy_intp dim[] = { 5, 5 }; std::vector<double> buffer(5*5, 1); PyObject* array_2d = PyArray_SimpleNewFromData(2, dim, NPY_DOUBLE, &buffer[0]); PyObject* return_value1 = PyObject_CallFunction(multiply_fun, "O", array_2d); Py_XDECREF(return_value1); Py_XDECREF(array_2d); Py_XDECREF(multiply_fun); Py_Finalize(); return 0; } 

However, when I want to use most python libraries, I get an error. For example, for this Python code:

 def multiply_fun(M): from skimage.io import imsave imsave('test.png', M) 

I got this error:

 Exception ignored in: <module 'threading' from 'C:\\Users\\Matin\\Anaconda3\\Lib\\threading.py'> Traceback (most recent call last): File "C:\Users\Matin\Anaconda3\Lib\threading.py", line 1283, in _shutdown assert tlock.locked() SystemError: <built-in method locked of _thread.lock object at 0x0000000002AF4418> returned a result with an error set 

By the way, this discussion could not help me.

Thank you for your help.

EDIT 1: Moving from skimage.io import imsave beyond the scope of the Python function (as @moooeeeep suggested in the comments), I get Null in this line:

 PyObject* python_code = PyImport_ImportModule("python_code"); 
+6
source share
2 answers

The problem seems to be that PyImport_ImportModule cannot load submodules of some packages when using from package.submodule import function . This is explained in the Python / C API Reference :

When the name argument contains a period (when it specifies the submodule of the package), the fromlist argument is set to the list ['*'], so the return value is a named module, not a top-level package containing it, as otherwise. (Unfortunately, this is an additional side effect when the name actually defines the subpackage instead of the submodule: the submodules specified in the all packages.) Return a new link to the imported module or NULL with the exception set when it fails. A failed import module does not leave the module in sys.modules.

This function always uses absolute imports.

0
source

I am facing a similar problem. How did you decide that?

0
source

Source: https://habr.com/ru/post/1271712/


All Articles