C++调用python,PyImport_ImportModule|PyObject_GetAttrString出错,高人帮忙,没分了,555
#Filename test.py
you = "world!"
print "aaaaaaaa"
def Hello():
print "Hello, world!"
Hello()
#include "stdafx.h"
//#include <python.h>
int main()
{
Py_Initialize();
PyObject * pModule = NULL;
PyObject * pFunc = NULL;
pModule = PyImport_ImportModule("test");
//pModule = PyImport_ImportModule("test.py");
pFunc = PyObject_GetAttrString(pModule, "Hello");
//pFunc = PyObject_GetAttrString(pModule, "you");
PyEval_CallObject(pFunc, NULL);
Py_Finalize();
return 0;
}
test.py应该放在哪呢?
我把py文件在python25/lib, projectdir/debug和projectdir下各放一份,
调试看到,pModule 不为0了,pFunc 还是0。
为了解决没有python25_d.lib的问题,我在stdafx.h里有如下设定
stdafx.h:
#pragma once
#include <afx.h>
#include <afxcmn.h>
#ifdef _DEBUG
#undef _DEBUG
#include "E:\software\Python25\include\Python.h"
#define _DEBUG
#else
#include "E:\software\Python25\include\Python.h"
#endif