调用Python脚本中的方法,莫名失败,请高手指教!!!

LLionTree 2012-09-21 10:05:30
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,谢谢!!
...全文
202 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gloveing 2012-09-21
  • 打赏
  • 举报
回复
不知道你前面怎么写的?
这是一个例子调用main.py中的test函数
//#========================================================
//# 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");
}
LLionTree 2012-09-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
不知道你前面怎么写的?
这是一个例子调用main.py中的test函数

C/C++ code
//#========================================================
//# author:ago
//# 2012/08/2……
[/Quote]

解决了,谢谢你的回复!

37,735

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧