c++调用python脚本问题
python代码
def add(a,b):
3 print "in python function add"
4 print "a = " + str(a)
5 print "b = " + str(b)
6 print "ret = " + str(a+b)
7 return
c++调用代码
Py_Initialize();
8 if ( !Py_IsInitialized() )
9 {
10 return -1;
11 }
12 PyRun_SimpleString("import sys");
13 PyRun_SimpleString("sys.path.append('./')");
14 PyObject *pName,*pModule,*pDict,*pFunc,*pArgs;
15 pName = PyString_FromString("test");
16 pModule = PyImport_Import(pName);
17 if ( !pModule )
18 {
19 printf("can't find test.py");
20 getchar();
21 return -1;
22 }
23 pDict = PyModule_GetDict(pModule);
24 if ( !pDict )
25 {
26
27 return -1;
28 }
29 pFunc = PyDict_GetItemString(pDict,"add");
30 cout<<"加法"<<endl;
31 if ( !pFunc || !PyCallable_Check(pFunc) )
32 {
33 printf("can't find function [add]");
34 getchar();
35 return -1;
36 }
37 pArgs = PyTuple_New(2);
38 PyTuple_SetItem(pArgs, 0, Py_BuildValue("l",3));
39 PyTuple_SetItem(pArgs, 1, Py_BuildValue("l",4));
40 PyObject_CallObject(pFunc, pArgs);
输出总是显示找不到函数add(can't find function [add])