問題描述
如何將 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']},
)
這樣我可以按名稱導入庫,并且沒有其他級別的嵌套:
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 包的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!