How do you call Python code from C code?

I recommend the approaches detailed here. It starts by explaining how to execute strings of Python code, then from there details how to set up a Python environment to interact with your C program, call Python functions from your C code, manipulate Python objects from your C code, etc.

EDIT: If you really want to go the route of IPC, then you’ll want to use the struct module or better yet, protlib. Most communication between a Python and C process revolves around passing structs back and forth, either over a socket or through shared memory.

I recommend creating a Command struct with fields and codes to represent commands and their arguments. I can’t give much more specific advice without knowing more about what you want to accomplish, but in general I recommend the protlib library, since it’s what I use to communicate between C and Python programs (disclaimer: I am the author of protlib).

Leave a Comment