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++工程后,设置工程属性:

c中嵌入python_第1张图片

连接参数如下:

-IC:\Python27\include
-LC:\Python27\libs
-lpython27

c中嵌入python_第2张图片

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 <windows.h>

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;
}

运行效果:

c中嵌入python_第3张图片





你可能感兴趣的:(c,python)