简体   繁体   中英

How to load .so libraryes in my Python app that is running on Ubuntu

I have some shared object files (.so libs) that I need to load in my python project that will run on Ubuntu platform.

The goal is that I have some libraries that have already been converted into .so files and now I need to load them in my python project.

Can anyone share the detailed steps for this?

You can use ctypes for that, it's quite simple.

Say that you have a my-library.so with the following C function exported:

void say_hello(char *name) {
    printf("Hello, %s!\n", name);
}

You would load the library and call the function from Python like this:

>>> from ctypes import cdll
>>> mylib = cdll.LoadLibrary('./my-library.so')
>>> mylib.say_hello("world")
Hello, world!
>>>

Note that the leading ./ is important, otherwise LoadLibrary will look in the default library path and not the current folder.

Fore more information refer to the documentation for ctypes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM