gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:57:55 26 4
gpt4 key购买 nike

我正在尝试使用嵌入在 C++ 程序中的 Python 3.5 解释器从 C++ 接收图像,并将其用作我训练的 tensorflow 模型的输入。首先,我将图像转换为 numpy 数组,然后将其发送到 python。这是我的简化代码,运行良好(代码采用自 here ):

Python代码:

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

调用上述函数的 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;
}

但是,当我想使用大多数 python 库时,我得到一个错误。例如,对于此 python 代码:

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

我遇到了这个错误:

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

顺便说一下,This related discussion帮不了我。

感谢您的帮助。

编辑 1:通过将 from skimage.io import imsave 移动到 python 函数之外(如评论中建议的 @moooeeeep),我在这一行中得到 Null:

PyObject* python_code = PyImport_ImportModule("python_code");

最佳答案

问题似乎是 PyImport_ImportModule 在使用 from package.submodule import function 时无法加载某些包的子模块。 Python/C API Reference Manual中已经解释过了:

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. (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. A failing import of a module doesn’t leave the module in sys.modules.

This function always uses absolute imports.

关于python - c++中的嵌入式python代码-导入python库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46169650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com