使用已编译的动态共享库分发Python包 [英] Distribute a Python package with a compiled dynamic shared library
问题描述
如何将Python模块和预编译的.so
库打包在一起?具体来说,我该如何编写setup.py
,以便在Python中执行此操作
How do I package a Python module together with a precompiled .so
library? Specifically, how do I write setup.py
so that when I do this in Python
>>> import top_secret_wrapper
无需设置LD_LIBRARY_PATH
就能轻松找到top_secret.so
?
It can easily find top_secret.so
without having to set LD_LIBRARY_PATH
?
在我的模块开发环境中,我具有以下文件结构:
In my module development environment, I have the following file structure:
.
├── top_secret_wrapper
│ ├── top_secret.so
│ └── __init__.py
└── setup.py
在__init__.py
内部,我有类似的东西:
Inside __init__.py
, I have something like:
import top_secret
这是我的setup.py
from setuptools import setup, Extension
setup(
name = 'top_secret_wrapper',
version = '0.1',
description = 'A Python wrapper for a top secret algorithm',
url = None,
author = 'James Bond',
author_email = 'James.Bond.007@mi6.org',
license = 'Spy Game License',
zip_safe = True,
)
我确定我的setup.py
缺少用于指定top_secret.so
位置的设置,尽管我不确定如何执行该操作.
I'm sure my setup.py
is lacking a setting where I specify the location of top_secret.so
, though I'm not sure how to do that.
推荐答案
我最终要做的是:
setup(
name='py_my_lib',
version=version, # specified elsewhere
packages=[''],
package_dir={'': '.'},
package_data={'': ['py_my_lib.so']},
)
通过这种方式,我可以按名称导入lib,并且没有其他层次的嵌套:
This way I get to import the lib by its name, and don't have another level of nestedness:
import py_my_lib
不是
from py_my_lib_wrapper import py_my_lib
这篇关于使用已编译的动态共享库分发Python包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!