#include </usr/include/python2.7/Python.h>
#include <stdio.h>
//http://www.360doc.com/content/12/0718/13/1317564_225018509.shtml
int main(){
PyObject *modelname,*model,*dict,*func,*args;
char *name="os";//module name
//initialize python
Py_Initialize();
if(!Py_IsInitialized()){
printf("initialize failed\n");
return -1;
}
//run the Python sentence
PyRun_SimpleString("print 'hello world'");
//import the python module
modelname=PyString_FromString(name);
model=PyImport_Import(modelname);
if(model){
printf("load model ok\n");
}
else{
printf("model %s not found!\n",name);
return -1;
}
return 0;
#include <stdio.h>
//http://www.360doc.com/content/12/0718/13/1317564_225018509.shtml
int main(){
PyObject *modelname,*model,*dict,*func,*args;
char *name="os";//module name
//initialize python
Py_Initialize();
if(!Py_IsInitialized()){
printf("initialize failed\n");
return -1;
}
//run the Python sentence
PyRun_SimpleString("print 'hello world'");
//import the python module
modelname=PyString_FromString(name);
model=PyImport_Import(modelname);
if(model){
printf("load model ok\n");
}
else{
printf("model %s not found!\n",name);
return -1;
}
return 0;
}
编译:gcc python_in_C.c -lpython2.7
运行输出:
shiyan@ubuntu:~/Desktop/study/pythonLearn$ ./a.out
hello world
load model ok
shiyan@ubuntu:~/Desktop/study/pythonLearn$