I'm writing a Python module in C, but I would like the module to also contain functions written in Python. How can Python functions be added to a module written in C?
Asked
Active
Viewed 50 times
0
-
1[This manual documents the API used by C and C++ programmers who want to write extension modules or **embed Python.**](https://docs.python.org/3/c-api/index.html) – Random Davis Aug 23 '22 at 16:11
-
Check similar SO question https://stackoverflow.com/questions/6150835/python-c-api-how-to-get-pyrun-string-with-py-eval-input-to-use-imported-modules and https://docs.python.org/3/faq/extending.html#how-can-i-evaluate-an-arbitrary-python-expression-from-c – Robert Lujo Aug 23 '22 at 16:13
-
Right. But it's one thing to have a C program that wants to call some python function, and another to have a python module that needs to execute python code. In the second case, a python interpreter is already running. I imagine there's a way to make use of it, rather than creating a new python interpreter instance inside the module as if it's a completely independent program. – SU3 Aug 23 '22 at 16:16
-
1`PyRun_String` and friends don't create an entirely new interpreter. They're exactly what you want. – DavidW Aug 23 '22 at 17:12