ImportError:numpy.core.multiarray导入失败(将python嵌入到c中)

时间:2020-06-07 20:27:13

标签: python c python-2.7 numpy python-embedding

我正在从事与对象检测和ocr相关的项目。我正在使用Ubuntu 18.04和python 2.7,yolov3进行对象检测,并使用tesseract 5.0.0-alpha-692-g62ea进行ocr。我必须将ocr的python代码嵌入到C代码中以进行对象检测。我编写了此C代码,并在终端通过“ sudo gcc code.c -lpython2.7”进行编译

#include < /usr/include/python2.7/Python.h >

int main() {
Py_Initialize();
PyRun_SimpleString("import sys; sys.path.append('.')");
PyRun_SimpleString("import ocr3;");
PyRun_SimpleString("print ocr3.myabs(2.0)");
Py_Finalize();

return 0;
}

但是在运行a.out时会给出错误:

ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File "", line 1, in
File "./ocr3.py", line 1, in
import cv2
File "/home/asus/.local/lib/python2.7/site-packages/cv2/init.py", line 3, in
from .cv2 import *
ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File "", line 1, in
NameError: name 'ocr3' is not defined

所以,我尝试了另一个C代码:

#include < stdio.h >
#include < ncurses.h >
#include < /usr/local/include/python2.7/Python.h >

int main()
{
char filename[] = "ocr3.py";
FILE* fp;

Py_Initialize();

fp = _Py_fopen(filename, "r");
PyRun_SimpleFile(fp, filename);

Py_Finalize();
return 0;
}

但是再次在运行a.out时遇到问题:“ code.c :(。text + 0x3e):对'_Py_fopen'的未定义引用”

很遗憾,我找不到任何解决方案。任何人都可以帮助解决这些问题之一吗?

0 个答案:

没有答案