Please check out Cython. It makes it very easy to wrap C code.

This is adapted from the documentation on calling external C functions:

cdef extern from "math.h":
    double sin(double)

def pysin(x):
    return sin(x)

You could then call pysin from the compiled module like a normal Python module.

Answer from Ryan Ginstrom on Stack Overflow
Discussions

How do you call Python code from C code? - Stack Overflow
Here a complete example ... in C, and then C embedded in Systemverilog using DPI. ... Save this answer. ... Show activity on this post. I recommend the approaches detailed here. It starts by explaining how to execute strings of Python code, then from there details ... More on stackoverflow.com
🌐 stackoverflow.com
How to Embed Python in C++?
I would recommend going the other way since it is significantly less icky in my opinion. The main reason I say that is embedding and calling python from C basically looses all of the advantages of using python in the first place. PyBind11 is generally the tool for the job. You can bind you C++ APIs to python equivalents and call them from python. You could also use SWIG although I feel like it is falling out of favor for modern code. Have fun! More on reddit.com
🌐 r/cpp_questions
24
13
September 26, 2022
how to compile and run C code with embedded python script? - Stack Overflow
You need to find where Python.h ... to the INCLUDE path for your compiler. What compiler are you using? ... You need to specify the directory where the Python.h header is found. You seem to have multiple versions around, so you need to decide which you want to link with — probably the 3.3 version. So, there is probably a /usr/bin/python3.3-config (or python3.3m-config) file; use that. There is definitely a 2.7 config, so if you're OK with linking to Python 2.7, you can use ... More on stackoverflow.com
🌐 stackoverflow.com
December 30, 2013
Using embedded C library in Python emulation - Stack Overflow
Because of this very reason, we ... for our embedded platforms. To clarify, I know that portions touching the hardware layer will have to be re-created and mapped to the PC's CAN drivers. I am hoping to find someone here on SO that has or even looked into porting a 5k LOC C library to Python. If there are any C to Python tools that work well that would be helpful for me to look into as well. ... Definitely option (2), but why not write the PC code in C or C++? ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Xmodulo
xmodulo.com › embed-python-code-in-c.html
How to embed Python code in C program
Another similar function is ... type. When you want to embed Python code in another language such as C, the code needs to be written in a Python module, which is then imported in another language....
🌐
GitHub
github.com › ftrias › pyembedc
GitHub - ftrias/pyembedc: Embed C/C++ code into Python source code · GitHub
Note: inline_c_precompile will compile the code only once at the beginning of the program to increase speed. The code must be given using the Python multi-line string format using three quotes '"""'. This is because the module will scan the source file and pick out the string.
Author: ftrias
🌐
Readthedocs
reptate.readthedocs.io › developers › python_c_interface.html
Tutorial: Interfacing Python and C code — RepTate 1.4.0 documentation
Fortunately, there are many solutions available to write code that will run fast in Python. We can cite Cython or Numba that transform Python code into C executable and require minimal addition to the existing Python code. There is also Ctypes that provides C compatible data types, and allows calling functions from external libraries, e.g.
🌐
CodeProject
codeproject.com › articles › Embedding-Python-program-in-a-C-Cplusplus-code
Coding Education Platforms for Beginners | .Software
September 22, 2014 - Programming courses should provide clear learning paths preventing aimless wandering through disconnected lessons. The most effective coding tutorials balance explanation, practice, and project work, developing both knowledge and practical skills. Language selection significantly impacts the beginner experience as some languages prove more approachable than others. Python's readability and versatility make it a popular first language for general software learning.
Find elsewhere
🌐
Python
docs.python.org › 3 › extending › embedding.html
1. Embedding Python in Another Application — Python 3.14.7 documentation
To show this, consider what the extension code from Python to C really does: ... Convert the data values from the call from C to Python. ... Convert the data values from the call from Python to C. As you can see, the data conversion steps are simply swapped to accommodate the different direction of the cross-language transfer. The only difference is the routine that you call between both data conversions. When extending, you call a C routine, when embedding, you call a Python routine.
🌐
Linux Journal
linuxjournal.com › article › 8497
Embedding Python in Your C Programs | Linux Journal
December 30, 2005 - In fact, if you look at Listing 1, you'll see that it can be done in as little as three lines of code, which initialize the interpreter, send it a string of Python code to execute and then shut the interpreter back down. Listing 1. Embedding Python in Three Lines
🌐
Python
docs.python.org › 3 › extending › extending.html
1. Extending Python with C or C++ — Python 3.14.7 documentation
In many cases, it is possible to avoid writing C extensions and preserve portability to other implementations. For example, if your use case is calling C library functions or system calls, you should consider using the ctypes module or the cffi library rather than writing custom C code.
🌐
Reddit
reddit.com › r/cpp_questions › how to embed python in c++?
r/cpp_questions on Reddit: How to Embed Python in C++?
September 26, 2022 -

Background: Hello! I am working on a project that will compute physics, graphics, and AI/ML tasks. I want to handle the physics and graphics calculations with C++ and the AI/ML calculations with Python. I have learned that you can embed Python in C++ recently but seem to be having trouble getting the embedding to work. I spent sometime away from computer science - about a year - and decided to comeback recently. I made the discovery that I like the theoretical side of computer science and want to conduct research. I am actually going to graduate school in computer science soon and I am very excited. I want to use this project to jump start my Ph.D.

Setup: Visual Studio Code on Windows 11

Questions: What is the best way to run and access Python scripts from C++ and how exactly can this be accomplished? I've tried inserting pybind11 as a submodule to my project git repository (I used this resource), but it has trouble finding certain header files, like Python.h - is there another solution that doesn't need pybind11? Is embedding Python in C++ even worth it? Should I just stick to using just one language or the other?

Thank you!

🌐
GitHub
gist.github.com › Coderx7 › cd42fe43a0add143255c1aea83a17ce8
Basic examples to show how to embed and extend Python in C including: creation of module in C with functions handling and building Python objects; importing and calling python functions from C. · GitHub
Save Coderx7/cd42fe43a0add143255c1aea83a17ce8 to your computer and use it in GitHub Desktop. ... Basic examples to show how to embed and extend Python in C including: creation of module in C with functions handling and building Python objects; importing and calling python functions from C.
🌐
Bsergean
bsergean.github.io › embedding_python
Embedding Python in C/C++.
# Hello world ## Something like a blog Renewing a letsencrypt certificate Embedding Python Chat like a nerd Dash dash Using rcc to reshard a Redis Cluster optimally Fixing Redis Cluster nodes ip address Disabling vi visual flashing mode Bin Packing, Redis Cluster and Cobra IXWebSocket ws tool ...
🌐
Cython
cython.readthedocs.io › en › latest › src › tutorial › embedding.html
Embedding Cython modules in C/C++ applications — Cython 3.3.0 documentation
For embedding multiple modules, the previously used cython_freeze script has been superseded by the cython --embed-modules option. ... See the documentation of the module init function in CPython and PEP 489 regarding the module initialisation mechanism in CPython 3.5 and later. Most importantly, DO NOT call the module init function instead of importing the module. This is not the right way to initialise an extension module. (It was always wrong but used to work before, but since Python 3.5, it is wrong and no longer works.)
🌐
Python Tips
book.pythontips.com › en › latest › python_c_extension.html
22. Python C extensions — Python Tips 0.1 documentation
The ctypes interface allows us ... seen while passing parameters to the adder.add_float(). We first create the required c_float types from python decimal values, and then use them as arguments to the C code....
🌐
Learning Python
learning-python.com › class › Workbook › unit16.htm
Learning Python: Unit 16, Embedding
1. C copies X and Y values to variables in module M · 2. C runs embedded code in module M’s name-space · 3. C fetches the final values of the Python variables · ♦ Python may be embedded in C or Python · ♦ See section “Dynamic coding” for Python tools ·
🌐
Real Python
realpython.com › python-bindings-overview
Python Bindings: Calling C or C++ From Python – Real Python
March 18, 2026 - You already have a large, tested, stable library written in C++ that you’d like to take advantage of in Python. This may be a communication library or a library to talk to a specific piece of hardware. What it does is unimportant. You want to speed up a particular section of your Python code by converting a critical section to C.
🌐
CodeProject
codeproject.com › Articles › 11805 › Embedding-Python-in-C-Cplusplus-Part-I
Embedding Python in C/C++: Part I
October 5, 2005 - Disclaimer: References to any specific company, product or services on this Site are not controlled by GoDaddy.com LLC and do not constitute or imply its association with or endorsement of third party advertisers