如何在python wheel包中包含外部库 [英] How to include external library with python wheel package

查看:318
本文介绍了如何在python wheel包中包含外部库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为使用cffi模块在Linux上嵌入并使用外部库(.so)的python创建软件包.

I want to create package for python that embeds and uses an external library (.so) on Linux using the cffi module.

是否有将.so文件包含到python包中的标准方法?

Is there standard way to include .so file into python package?

该软件包仅在内部使用,不会发布到pypi.

The package will be used only internally and won't be published to pypi.

我认为Wheel软件包是最好的选择-他们将创建平台特定的软件包,其中所有文件都可以复制,因此无需在目标环境上构建任何东西.

I think Wheel packages are the best option - they would create platform specific package with all files ready to be copied so there will be no need to build anything on target environments.

推荐答案

您可以使用 auditwheel 来将外部库注入方向盘:

You can use auditwheel to inject the external libraries into the wheel:

auditwheel修复:将这些外部共享库复制到wheel本身,并自动修改适当的RPATH条目,以便在运行时提取这些库.就像在不更改构建系统的情况下静态链接库一样,这可以实现类似的结果.建议打包人员,像静态链接一样,捆绑可能会隐含版权问题.

auditwheel repair: copies these external shared libraries into the wheel itself, and automatically modifies the appropriate RPATH entries such that these libraries will be picked up at runtime. This accomplishes a similar result as if the libraries had been statically linked without requiring changes to the build system. Packagers are advised that bundling, like static linking, may implicate copyright concerns.

您可以通过通常执行以下操作来预先构建外部c ++库:

You can pre-build the external c++ library by typically executing the following:

./configure && make && make install

这将生成一个my_external_library.so文件并将其安装在适当的路径中.但是,您需要确保正确设置了库路径,以便审核轮发现丢失的依赖项.

This will generate an my_external_library.so file and install it in the appropriate path. However, you'll need to ensure that the library path is properly set in order for the auditwheel to discover the missing dependency.

export LD_LIBRARY_PATH=/usr/loca/lib

然后您可以通过执行以下命令来构建python wheel:

You can then build the python wheel by executing:

python setup.py bdist_wheel

最后,您可以修理轮子,这会将my_external_library.so注入包装中.

Finally, you can repair the wheel, which will inject the my_external_library.so into the package.

auditwheel repair my-python-wheel-1.5.2-cp35-cp35m-linux_x86_64.whl

我已成功将上述步骤应用于python库 confluent-kafka-python librdkafka 具有必需的c/c ++依赖关系.

I successfully applied the above steps to the python library confluent-kafka-python which has a required c/c++ dependency on librdkafka.

注意:auditwheel仅适用于Linux.对于MacOS,请参见定位工具.

Note: auditwheel is Linux-only. For MacOS, see the delocate tool.

这篇关于如何在python wheel包中包含外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆