读书人

调用Python脚本中的步骤莫名失败请

发布时间: 2012-09-24 13:49:42 作者: rapoo

调用Python脚本中的方法,莫名失败,请高手指教!!!
test.py 脚本中只有一个方法 hello()

//PyRun_SimpleString("print 'hi,python!'"); //如果执行这两句,正常执行
//PyRun_SimpleString("execfile('test.py')");
.......
printDict(pDict); //打印模块的字典列表
PyObject* pFunHi = PyDict_GetItemString(pDict,"hello");
if(!pFunHi) //这里失败了,没有查找到hello方法
{
printf("can't found the function\n");
return -1;
}
执行后打印结果:

__builtins__
__file__
__package__
__path__
__name__
__doc__
can't found the function

麻烦高手们分析下是什么问题,我刚学习python,python版本 2.7.3,编译器vs2008,谢谢!!


[解决办法]
不知道你前面怎么写的?
这是一个例子调用main.py中的test函数

C/C++ code
//#========================================================//#              author:ago                                //#              2012/08/24 19:26:57                  //#========================================================#include "python_lib/Python.h"#include <windows.h>#pragma comment(lib,"python27_d.lib")int main(int argc, char *argv[]){    Py_Initialize();    //PyRun_SimpleString("import sys");    //PyRun_SimpleString("sys.path.append('./')");    PyObject* pName = PyString_FromString("main");    //python 文件的名字    PyObject* pModule = PyImport_Import(pName);    if ( !pModule )    {        MessageBox(NULL, ("找不到main.py"), ("提示"), MB_OK);        return 1;    }    PyObject* pDict = PyModule_GetDict(pModule);    PyObject* pFunc = PyDict_GetItemString(pDict, "test");     //python 文件中函数名:test    PyObject_CallObject(pFunc, NULL);    Py_Finalize();    system("pause");} 

读书人网 >perl python

热点推荐