Skip to content

Instantly share code, notes, and snippets.

@justinian
Created May 14, 2014 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinian/cbfb502a53a60b828a8b to your computer and use it in GitHub Desktop.
Save justinian/cbfb502a53a60b828a8b to your computer and use it in GitHub Desktop.
Simple C app to run a python file
#include <stdio.h>
#include <stdlib.h>
#include <Python.h>
#ifndef PYTHON_FILE
#error "Please define PYTHON_FILE."
#endif
int main(int argc, char **argv)
{
// Clear out the environment to disallow
// PYTHONPATH changes, or PYTHONSTARTUP, etc
clearenv();
Py_SetProgramName(argv[0]);
Py_Initialize();
PySys_SetArgv(argc, argv);
const char* filename = PYTHON_FILE;
// Disallow non-absolute script paths
if( *filename != '/' ) {
fprintf(stderr, PYTHON_FILE " is not an absolute path, please recompile.\n");
return 1;
}
FILE* file = fopen(filename, "r");
int result = 1;
if(file)
result = PyRun_SimpleFileEx(file, filename, 1);
Py_Finalize();
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment