We will use a simple example of Python embedding into a C program that can be found on the Python documentation pages. The source file is called hello-embedded-python.c:
#include <Python.h>
int main(int argc, char *argv[]) {
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}
This code samples will initialize an instance of the Python interpreter within the program and print the date using the time Python module.
The embedding sample code can be found online in the Python documentation pages at https://docs.python.org/2/extending/embedding.html and https://docs.python.org/3/extending/embedding.html.