Created
May 27, 2025 10:46
-
-
Save MurageKibicho/55c3a02f48a9a074d02788a1a76a5f01 to your computer and use it in GitHub Desktop.
Calling Python Code inside a C code base
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <python3.8/Python.h> | |
//Run : gcc Cmain.c -I/usr/include/python3.8 -lpython3.8 -o Cmain.o && ./Cmain.o | |
//Full walkthrough : https://leetarxiv.substack.com/p/making-c-and-python-talk-to-each | |
int main() | |
{ | |
printf("Python, you are inside me\n"); | |
char *pythonFileLocation = "./"; | |
//Set environment | |
setenv("PYTHONPATH", pythonFileLocation, 1); | |
//Initialize Python | |
Py_Initialize(); | |
char *pythonFileName = "PythonFunctions"; | |
PyObject *loadModule = PyImport_ImportModule(pythonFileName); | |
if(loadModule == NULL) | |
{ | |
/*Need to set path to overcome this error*/ | |
printf("Error importing module\n"); | |
PyErr_Print(); | |
exit(1); | |
} | |
PyObject *currentDirectoryFunction = PyObject_GetAttrString(loadModule, (char *)"PrintCurrentDirectory"); | |
if(currentDirectoryFunction == NULL) | |
{ | |
printf("Error loading currentDirectoryFunction\n"); | |
PyErr_Print(); | |
exit(1); | |
} | |
//Argument is Null because function takes zero arguments | |
PyObject *directoryFunctionCall = PyObject_CallObject(currentDirectoryFunction, NULL); | |
if(directoryFunctionCall == NULL) | |
{ | |
printf("Error calling directoryFunctionCall\n"); | |
PyErr_Print(); | |
exit(1); | |
} | |
//Print List Starts here | |
PyObject *printListFunction = PyObject_GetAttrString(loadModule, (char *)"PrintList"); | |
if(printListFunction == NULL){printf("Error loading printListFunction\n");PyErr_Print();exit(1);} | |
int numberOfArguments = 1; | |
int listLength = 1; | |
//Create a new list | |
PyObject *pythonList = PyList_New(listLength); | |
if(pythonList == NULL){printf("Error creating Python List\n");PyErr_Print();exit(1);} | |
//Create a new integer | |
PyObject *pythonInteger = PyLong_FromLong(55); | |
if(pythonInteger == NULL){printf("Error creating Python Int\n");PyErr_Print();exit(1);} | |
//Set integer at list index 0 | |
PyList_SetItem(pythonList, 0, pythonInteger); | |
PyObject *functionArguments = PyTuple_Pack(numberOfArguments, pythonList); | |
PyObject *printListFunctionCall = PyObject_CallObject(printListFunction, functionArguments); | |
if(printListFunctionCall == NULL) {printf("Error calling printListFunctionCall\n");PyErr_Print();exit(1);} | |
//Cleanup functionArguments | |
Py_XDECREF(functionArguments); | |
//Cleanup pythonList | |
Py_XDECREF(pythonList); | |
//Cleanup Call to PrintList Function | |
Py_XDECREF(printListFunctionCall); | |
//Cleanup PrintList Function | |
Py_XDECREF(printListFunction); | |
//Cleanup Call to Directory Function | |
Py_XDECREF(directoryFunctionCall); | |
//Cleanup Current Directory Function | |
Py_XDECREF(currentDirectoryFunction); | |
//Cleanup Load Module | |
Py_XDECREF(loadModule); | |
//End Python Session | |
Py_Finalize(); | |
return 0; | |
} | |
/*char *pythonFileLocation = "./"; | |
*/ | |
//clear && gcc Cmain.c -I/usr/include/python3.8 -lpython3.8 -o Cmain.o && ./Cmain.o |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
#Print Current Directory | |
def PrintCurrentDirectory(): | |
print(os.getcwd()) | |
def PrintSum(x,y): | |
print(x+y) | |
def PrintList(myList): | |
print(myList) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment