简体   繁体   English

c ++中的嵌入式python代码 - 导入python库时出错

[英]Embedded python code in c++ - error when importing python libraries

I am trying to use Python 3.5 interpreter embedded in a C++ program to receive an image from C++, and use it as an input for my trained tensorflow model. 我试图使用嵌入在C ++程序中的Python 3.5解释器从C ++接收图像,并将其用作我训练的张量流模型的输入。 First, I convert my image to numpy array and then send it to python. 首先,我将我的图像转换为numpy数组,然后将其发送到python。 This is my simplified code which works fine (codes adopted from here ): 这是我的简化代码,工作正常(从这里采用的代码):

Python code: Python代码:

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

My C++ code that calls the function above: 调用上述函数的我的C ++代码:

#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 of the python libraries, I get an error. 但是,当我想使用大多数python库时,我收到一个错误。 For example, for this python code: 例如,对于这个python代码:

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 related discussion couldn't help me. 顺便说一句, 这个相关的讨论无法帮助我。

Thanks for your help. 谢谢你的帮助。

EDIT 1: By moving from skimage.io import imsave to outside of the python function (as @moooeeeep suggested in comments) I get Null in this line: 编辑1:通过from skimage.io import imsave移动到python函数之外(如注释中的@moooeeeep建议)我在这行中得到Null:

PyObject* python_code = PyImport_ImportModule("python_code");

It seems the problem is that PyImport_ImportModule cannot load submodules of some packages when using from package.submodule import function . 似乎问题是当使用from package.submodule import function时, PyImport_ImportModule无法加载某些包的子模块。 It has been explained in Python/C API Reference Manual : 它已在Python / C API参考手册中进行了解释:

When the name argument contains a dot (when it specifies a submodule of a package), the fromlist argument is set to the list ['*'] so that the return value is the named module rather than the top-level package containing it as would otherwise be the case. 当name参数包含一个点(当它指定包的子模块时),fromlist参数设置为list ['*'],以便返回值是命名模块而不是包含它的顶级包否则就是这种情况。 (Unfortunately, this has an additional side effect when name in fact specifies a subpackage instead of a submodule: the submodules specified in the package's all variable are loaded.) Return a new reference to the imported module, or NULL with an exception set on failure. (不幸的是,当name实际指定子包而不是子模块时,这会产生额外的副作用:加载包的all变量中指定的子模块。)返回对导入模块的新引用,或者在失败时设置异常NULL 。 A failing import of a module doesn't leave the module in sys.modules. 导入模块失败不会将模块保留在sys.modules中。

This function always uses absolute imports. 此函数始终使用绝对导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM