python embedding simple example
in English http://niitsuma.blogspot.com/2009/01/python-ebeding-simple-example.html
http://docs.python.org/extending/embedding.html
This example fail to import python file multiple.py in some environment.
The following provides another example of accessing to python function's result and augments from C.
#include <Python.h> int main() { PyObject *pName, *pModule, *pDict, *pFunc; PyObject *pArgs, *pValue; Py_Initialize(); //def myfun PyRun_SimpleString( "def myfun(a):\n" "\tprint a\n" "\treturn a*a\n" ); //extract interface to myfun pName = PyString_FromString( "__main__"); pModule = PyImport_Import(pName); Py_DECREF(pName); pFunc = PyObject_GetAttrString(pModule, "myfun"); pArgs = PyTuple_New(1); pValue = PyInt_FromLong(atoi("3")); PyTuple_SetItem(pArgs, 0, pValue); //execute myfun pValue = PyObject_CallObject(pFunc, pArgs); Py_DECREF(pArgs); //get result printf("Result of call: %ld\n", PyInt_AsLong(pValue)); Py_DECREF(pValue); //simple do PyRun_SimpleString("print myfun(3)\n"); //finalize Py_DECREF(pModule); Py_Finalize(); return 0; }