How to compile a whole Python library along with it's dependencies so that it can be used in C (without invoking Python's runtime). That is, the compiled code has the Python interpreter embedded and Python does not need to be installed on the system.
From my understanding, when Python code is compiled using Cython it:
- Does not invoke the python runtime if the
--embed
argument is used - Compiles files individually
- Allows for different modules to be called (from the Python runtime / other compiled Cython files)
The question which are still unclear are:
- How to use these module files from C? Can the compiled Python files call other compiled Python files when used in C?
- Does only the library entry point need to be declared or do all functions need to be declared?
- How to manage the Python dependencies? how to compile them too (so that the Python runtime is not needed).
A simplified example for a python library called module
where __init__.py
is an empty file:
module/
├── run.py
├── http/
│ ├── __init__.py
│ ├── http_request.py
http_requests.py
contains:
import requests
def get_ip():
r = requests.get('https://ipinfo.io/ip')
print(r.text)
and run.py
contains the following:
from http import http_request
if __name__ == '__main__':
http_request.get_ip()
How to call the function get_ip
from C without using the Python runtime (needing to have Python installed when running the application).
The above example is very simple. The actual use case is collecting/processing robotics data in C at a high sampling rate. Whilst C is great for basic data processing there are excellent Python libraries which allow for much more comprehensive analysis. The objective would be to call the Python libraries on data which has been partially processed in C. This would allow us to get a much more detailed understanding of the data (and process it in "real time"). The data frameworks are way too large for our team to rewrite in C.