I wrote a demo on the subprocessing module a while back that addresses this.
Here is the example I used:
import os # Used to determine if machine is windows or unix/macOS
import subprocess # Used to run commands from python
def compile(filename, binary_filename):
"""Function to compile the provided file in gcc"""
# Below is equivalent to running: gcc -o hello_world hello_world.c
print(f"Creating binary: {binary_filename} From source file: {filename}\n")
subprocess.run(["gcc", "-o", binary_filename, filename])
def run_binary(binary_filename):
"""Runs the provided binary"""
print(f"Running binary: {binary_filename}\n")
subprocess.run([binary_filename])
if __name__ == "__main__":
compile("hello_world.c", "hello_world")
if os.name =="nt": # If on windows
run_binary("hello_world.exe")
else: # If on unix/mac
run_binary("hello_world")
print("Binary run")
I think this answers your question, if instead you want to call C code from python you will need the ctypes library.
If you are looking to go the other way, and run python from C code you can follow the answer to this question.