C中调用Python函数,找不到模块

我装的是Python 2.7.采用的例程如下。
现在的问题是运行时报错:can't find pytest.py
我是初学者,不知道为什么?请高手解答。
谢谢!

#Python脚本,存为pytest.py
def add(a,b):
return a + b
// C代码调,用上面的add函数
#include <stdio.h>
#include <stdlib.h>
#include "C:/Python27/include/python.h"
#pragma comment(lib, "C:\\Python27\\libs\\python27.lib")
int main(int argc, char** argv)
{

PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pRetVal;
Py_Initialize();

if ( !Py_IsInitialized() ) // 检查初始化是否成功
{
return -1;
}
// 载入名为pytest的脚本(注意:不是pytest.py)
pName = PyString_FromString("pytest");
pModule = PyImport_Import(pName);
if ( !pModule )
{
printf("can't find pytest.py");
getchar();
return -1;
}

是因为你的模块的路径不对,必须先指定路径
PyObject *sys = PyImport_ImportModule("sys");
PyObject *path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyString_FromString("."));追问

我加了着三句后,依然不行呀??

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网