How to run .so files using through python script

user3415910 picture user3415910 · Feb 22, 2018 · Viewed 14.8k times · Source

I have a c program(.c file). I am converting that to a shared object(.so). How can i call and run the shared object from my python code? If possible, please suggest me a list of libraries that can help me to do this task.

Answer

Alan picture Alan · Feb 22, 2018

If you want to call functions inside a shared object, the the standard module ctypes is what you are after. No need for any external libraries.

Load a library:

from ctypes import *
# either
libc = cdll.LoadLibrary("libc.so.6")
# or
libc = CDLL("libc.so.6")

Then call a function from the library, the same as calling a Python function:

print(libc.time(None))