问题
Someone created a C module for python with Nuitka. (The original Python code is not available, the module is already compiled - so it is a machine binary file.) I would like to use the code within another tool, which only excepts Python files. So I would like to include the C code into Python.
To get more specific: So far I have the files thatmodule.pyi
and a thatmodule.so
. I can include them into my current Python code simply by running import thatmodule
inside mymodule.py
. Now I only want one single Python file mymodule.py
.
My current idea is to copy the code from thatmodule.pyi
to the beginning of mymodule.py
and to convert thatmodule.so
to a binary string with
with open('thatmodule.so', mode='rb') as file:
fileContent = file.read()
... missing ... how to convert fileContent to b'string'...
and put this binary string into mymodule.py
. And then I have to execute this binary string from within my python module mymodule.py
. How can I do this?
回答1:
You'll have to write it out to a file (and presumably the .pyi
too) and then use python's importlib to dynamically import it.
回答2:
If you have a documentation, which describes functions for thatmodule.so
, you can use the following:
import ctypes
mylib = ctypes.CDLL("thatmodule.so")
Documentation here
来源:https://stackoverflow.com/questions/60788861/execute-binary-string-of-c-module