简体   繁体   English

如何在Ubuntu上运行的Python应用程序中加载.so库

[英]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. 我有一些共享对象文件(.so libs),需要在将在Ubuntu平台上运行的python项目中加载。

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. 目的是我已经将一些库转换为.so文件,现在我需要将它们加载到python项目中。

Can anyone share the detailed steps for this? 谁能分享详细的步骤?

You can use ctypes for that, it's quite simple. 您可以使用ctypes ,这非常简单。

Say that you have a my-library.so with the following C function exported: 假设您有一个my-library.so ,并且导出了以下C函数:

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

You would load the library and call the function from Python like this: 您将加载库并像这样从Python调用函数:

>>> 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. 请注意,前导./很重要,否则LoadLibrary将在默认库路径而不是当前文件夹中查找。

Fore more information refer to the documentation for ctypes . 有关更多信息,请参阅ctypes文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使您的 python web 应用程序始终在 ubuntu 机器上运行 - how to make your python web app always running on ubuntu machine “在 ubuntu 20.04 上运行 tensorflow 时,无法加载动态库 'libcudnn.so.8'” - "Could not load dynamic library 'libcudnn.so.8'" when running tensorflow on ubuntu 20.04 如果MySQL正在运行,如何在ubuntu上用python找出? - how to find out with python on ubuntu if MySQL is running? 为什么我的 Python 代码运行这么慢? 我怎样才能加快速度? - Why is my Python code running so slow? How can I speed it up? 在Ubuntu Shell上运行Python - Running Python on Ubuntu Shell 加载python应用程序时Ubuntu服务器内存不足 - Ubuntu server running out of memory when loading python app 需要帮助在Ubuntu中使用Upstart将Python应用程序作为服务运行 - Need help running Python app as service in Ubuntu with Upstart Jetson Nano 与 64 Ubuntu 运行 32 位 Python 应用程序 - Jetson Nano with 64 Ubuntu running 32bit Python app 如何将我的 python 项目作为 ubuntu 中的软件 - how to make my python project as a software in ubuntu 如何让我的 Python Flask 应用程序在 Ubuntu ec2 上连接到 Z62A94414B4591DCCABB762A9414B45946A - How do I get my Python Flask app on an Ubuntu ec2 to connect with a MySQL db with RDS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM