简体   繁体   English

使用Python Egg / Wheel打包共享对象(库)

[英]Package shared object (library) with Python Egg / Wheel

I've done this so far: 到目前为止,我已经做到了:

  1. Created MANIFEST.in with: include path/to/libfoo.so 使用以下内容创建MANIFEST.in include path/to/libfoo.so
  2. Created setup.py that after calling setupt.py install puts the libfoo.so into /usr/local/lib/python/site-packages/foo.egg/path/to/libfoo.so . 创建的setup.py会在调用setupt.py installlibfoo.so放入/usr/local/lib/python/site-packages/foo.egg/path/to/libfoo.so

This, of course, doesn't help Python to find libfoo when it's required at run time. 当然,这在运行时需要Python时无法帮助Python查找libfoo What do I need to do to make Python actually find this library? 我需要怎么做才能使Python真正找到该库?

Note 注意

This library doesn't have Python bindings, it's just a shared library with some native code in it. 该库没有Python绑定,它只是其中包含一些本机代码的共享库。 It is called from another shared library which sits in /usr/local/lib/python/site-packages/foo.egg/path/wrapped.cpython-36m-x86_64-linux-gnu.so . 从位于/usr/local/lib/python/site-packages/foo.egg/path/wrapped.cpython-36m-x86_64-linux-gnu.so另一个共享库中调用它。

If you want to hard-code the location of shared library, you can use the rpath option. 如果要对共享库的位置进行硬编码,则可以使用rpath选项。 For that you would do something like.. 为此,您会做类似的事情。

python setup.py build_ext --rpath=/usr/local/lib/python/site-packages/foo.egg/path/to

Where the setup.py above is the script used to build wrapped.cpython-36m-x86_64-linux-gnu.so and the rpath is the path to libfoo.so Of course you should be able to put this directly inside the build script, depending on what that process looks like. 上面的setup.py是用于构建wrapped.cpython-36m-x86_64-linux-gnu.so的脚本,而rpathlibfoo.so的路径。因此,您当然应该可以将其直接放在构建脚本中,取决于该过程是什么样的。

-rpath=dir -rpath = DIR

Add a directory to the runtime library search path. 将目录添加到运行时库搜索路径。 This is used when linking an ELF executable with shared objects. 在将ELF可执行文件与共享库链接时使用。 All -rpath arguments are concatenated and passed to the runtime linker, which uses them to locate shared objects at runtime. 所有-rpath参数都被串联并传递到运行时链接程序,该链接程序使用它们在运行时定位共享对象。 The -rpath option is also used when locating shared objects which are needed by shared objects explicitly included in the link 在查找链接中显式包含的共享库所需的共享库时,也使用-rpath选项

If it's not an option to update the build process for wrapped.cpython-36m-x86_64-linux-gnu.so I think your only option it to put libfoo.so somewhere that's in the load library path or manually add the location at run-time. 如果不是更新wrapped.cpython-36m-x86_64-linux-gnu.so的构建过程的选项, wrapped.cpython-36m-x86_64-linux-gnu.so我认为您唯一的选择是将libfoo.so放在加载库路径中的某个位置,或者在运行时手动添加位置时间。

In answer to a few of your follow-on questions... 在回答您的一些后续问题时...

The system load library locations come from /etc/ld.so.conf and references the locations in the ld.so.conf.d directory. 系统装入库的位置来自/etc/ld.so.conf并引用ld.so.conf.d目录中的位置。 The ldconfig command rebuilds the the cache for shared libraries from this data so if you change things be sure to call this command. ldconfig命令从该数据重建共享库的高速缓存,因此,如果您进行更改,请务必调用此命令。

At the command line or in your .bashrc you can use export LD_LIBRARY_PATH=.... to add additional directories to the search path. 在命令行或.bashrc ,可以使用export LD_LIBRARY_PATH=....将其他目录添加到搜索路径。

You can manually load shared objects. 您可以手动加载共享库。 See https://docs.python.org/2/library/ctypes.html Loading shared libraries . 请参阅https://docs.python.org/2/library/ctypes.html 加载共享库

I haven't tried this myself but I've read that if you manually load a subordinate shared library in your python code and then import the higher level library, the linker won't have to go out and find the lower one since it's already loaded. 我自己还没有尝试过,但我读过,如果您在python代码中手动加载下级共享库,然后导入更高级别的库,则链接器将不必走出去找到较低的链接器,因为它已经存在加载。 This would look something like... 看起来像...

import ctypes
foolib = ctypes.CDLL('/full/path/to/libfoo.so')
import wrapped

There's a number of examples on StackOverflow on how to do this and lots of additional info/examples on manipulating the library search paths. StackOverflow上有许多有关如何执行此操作的示例,以及有关操纵库搜索路径的许多其他信息/示例。

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

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