我想在c++中调用python的一个脚本,这个脚本中我只是import了一个numpy就报错了,而如果是简单的脚本(没有import第三方库)就不会出错,我已经把:
INCLUDEPATH += C:/Python27/include/
LIBS += C:/Python27/libs/python27.lib
添加进去了,
pyrun_simplestring("import sys");
pyrun_simplestring("import numpy");
pyrun_simplestring("sys.path.append('c:\python27\lib\site-packages\')");
pyerr_print();
pyobject * pmodule = null;
pyobject * pfunc = null;
pmodule =pyimport_importmodule("test_my"); //test001:python文件名
pfunc= pyobject_getattrstring(pmodule, "testdict"); //add:python文件中的函数名
pyobject *pargs = pytuple_new(1);
pyobject *pdict = pydict_new(); //创建字典类型变量
pydict_setitemstring(pdict, "name", py_buildvalue("s", "wangyao")); //往字典类型变量中填充数据
pydict_setitemstring(pdict, "age", py_buildvalue("i", 25)); //往字典类型变量中填充数据
pytuple_setitem(pargs, 0, pdict); //0---序号 将字典类型变量添加到参数元组中
pyobject *preturn = null;
preturn = pyeval_callobject(pfunc, pargs); //调用函数
int size = pydict_size(preturn);
cout << "返回字典的大小为: " << size << endl;
pyobject *pnewage = pydict_getitemstring(preturn, "age");
int newage;
pyarg_parse(pnewage, "i", &newage);
cout << "true age: " << newage << endl;
py_finalize();
这是python的脚本:
#import numpy as np
def HelloWorld():
print "Hello World"
def add(a, b):
#tmp=np.random.randint(10,88)
return a+b
def TestDict(dict):
print dict
dict["Age"] = 17
return dict
class Person:
def greet(self, greetStr):
print greetStr
#print add(5,7)
#a = raw_input("Enter To Continue...")
老是报错,但如果我把import numpy去掉就没问题,求大神解答,困扰好久了~~~