Posted on 2009-07-27 19:27
silentneil 阅读(713)
评论(0) 编辑 收藏 引用
// Python.cpp : Defines the entry point for the console application.
//
#include "Python.h"
int main()
{
Py_Initialize();
if(!Py_IsInitialized())
return -1;
PyRun_SimpleString("print '路径测试!'\n");
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
// PyRun_SimpleString("sys.path.append('D:\\bz\\Python24\\bz\\Python\\Release')");
// PyRun_SimpleString("sys.path");
PyObject *pName,*pModule,*pDict,*pFunc,*pArgs;
pName=PyString_FromString("pytest");
pModule=PyImport_Import(pName);
if(!pModule)
{
printf("无法打开文件pytest.py!");
getchar();
return -1;
}
pDict=PyModule_GetDict(pModule);
if(!pDict)
return -1;
pFunc=PyDict_GetItemString(pDict,"add");
if(!pFunc||!PyCallable_Check(pFunc))
{
printf("无法找到函数add()");
getchar();
return -1;
}
*pArgs;
pArgs=PyTuple_New(2);
PyTuple_SetItem(pArgs,0,Py_BuildValue("1",3));
PyTuple_SetItem(pArgs,1,Py_BuildValue("1",4));
PyObject_CallObject(pFunc,pArgs);
Py_DECREF(pName);
Py_DECREF(pArgs);
Py_DECREF(pModule);
Py_Finalize();
getchar();
return 0;
}
#-*-coding:cp936-*-
#测试函数
def add(a,b):
print "in python fuinction"
print 'a='+str(a)
print 'b='+str(b)
print 'result='+str(a+b)
return