I'm new to python, so maybe this is a dumb question. I want to write a simple c program with a built-in python script. I have two files:
Call-function.c:
#include <Python.h> int main(int argc, char *argv[]) { PyObject *pName, *pModule, *pDict, *pFunc, *pValue; if (argc < 3) { printf("Usage: exe_name python_source function_name\n"); return 1; } // Initialize the Python Interpreter Py_Initialize(); // Build the name object if ((pName = PyString_FromString(argv[1])) == NULL) { printf("Error: PyString_FromString\n"); return -1; } // Load the module object if ((pModule = PyImport_Import(pName)) == NULL) { printf("Error: PyImport_Import\n"); return -1; } // pDict is a borrowed reference if ((pDict = PyModule_GetDict(pModule))==NULL) { printf("Error: PyModule_GetDict\n"); return -1; } ...
and
hello.py:
def hello(): print ("Hello, World!")
I compile and run it like this:
gcc -g -o call-function call-function.c -I/usr/include/python2.6 -lpython2.6 ./call-function hello.py hello
and do the following:
Error: PyImport_Import
i.e. PyImport_Import returns NULL . Could you help me in this matter? Any help would be appreciated.
Best wishes,
Alex
source share