c嵌入python_c中嵌入python

c编译器用DEV-C++,

python(sys.version):'2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]'

先新建DEV-C++工程后,设置工程属性:

连接参数如下:

-IC:\Python27\include

-LC:\Python27\libs

-lpython27

test.py的代码:

print 'Go into the test.py!'

words = raw_input('What do you want to say? ')

print 'you said:'+words

main.c的代码

#include "python.h"

#include

int main(int argc, char *argv[])

{

char *szPythonFileName="test.py";

Py_Initialize();

PyRun_SimpleString("print( 'PyRun_SimpleString ' )" );

PyObject *pyfile = PyFile_FromString(szPythonFileName,"r");

if(pyfile==NULL)

{

return 1;

}

FILE *f = PyFile_AsFile(pyfile);

if(f==NULL)

{

return 1;

}

PyRun_AnyFileEx(f,szPythonFileName,0);

Py_Finalize();

system("PAUSE" );

return 0;

}

运行效果: