Monday 15 July 2013

C and Python

Not really sure why I would ever want to do this but here is how to run python code from within C Code. Here is a test program that prints a string using python being called from C.

/*
 * Test program to run python code from C
 * Compile with gcc -Wall -I /usr/include/python2.7/ -lpython2.7 -o runpython runpython.c
 *
 */

#include <Python.h>

int main() {
  Py_Initialize();
  PyRun_SimpleString("print('print using python')");
  return 0;
}