Calling Python from C code with subs as strings

Makhno mak at
Sun Apr 23 13:28:37 EDT 2000

Can somebody suggest why the following C/Python code does not work? It's
supposed to call a C function with a string argument that describes what
Python function to call. That C function then calls that Python function.


void CallPythonSub(char *subname)
   if (subname[0]==0) return; //do check this....
   PyObject *moduleref=PyImport_ImportModule("__main__");
   PyObject *thesub=PyObject_GetAttrString(moduleref,subname);
   if (thesub==NULL) {printf("Error\n");return;}


def thissub():
   print 'im working!'


No Nulls are returned from the Py... calls. It just does nothing.

More information about the Python-list mailing list