我已经编写了一个依赖于一些C扩展的Python模块。这些C扩展又依赖于几个已编译的C库。我希望能够将这个模块与所有依赖项捆绑在一起。在
我已经给出了一个最小的例子(it can be found on GitHub in its entirety)。在
目录结构为:
$ tree .
.
├── README.md
├── poc
│ ├── __init__.py
│ ├── cython_extensions
│ │ ├── __init__.py
│ │ ├── cvRoberts_dns.c
│ │ ├── cvRoberts_dns.h
│ │ ├── helloworld.c
│ │ ├── helloworld.pxd
│ │ ├── helloworld.pyx
│ │ ├── test.c
│ │ └── test.h
│ ├── do_stuff.c
│ └── do_stuff.pyx
└── setup.py
在设置.py根据必要的库生成扩展和链接(在本例中为libsundials_cvode
,libsundials_nvectorserial
):
这一切都很好,但它确实要求最终用户首先安装sundanials(在实际情况中,还需要其他几个非常挑剔的库来启动和运行)。在
理想情况下,我希望能够只在开发机器上设置它,创建包含适当的共享库的发行版,并发布某种捆绑包。在
给出了各种各样的教程,例子和我到目前为止找到的帖子。我被引导相信我在正确的轨道上。不过,还有一些最后的步骤我并不是在摸索。在
感谢任何帮助:-)。在
为了增强mhsmith的优秀answer,下面是使用
delocate
在MacOS上执行的步骤:安装
sundials
,例如使用自制程序:生成包:
^{3美元auditwheel show
/auditwheel repair
的挂件是delocate-listdeps
/delocate-wheel
,因此首先分析生成的wheel文件:固定车轮锉刀:
在
dist_fixed
目录中,您将拥有捆绑的轮子。您会注意到尺寸差异:如果您列出了捆绑式控制盘的dep,您将注意到所需的库现在已绑定(由前缀
@loader_path
表示):安装捆绑式控制盘(请注意,捆绑式lib已正确安装):
我建议采取完全不同的方法。设置Linux包管理基础设施。在Ubuntu/Debian上,这可以用
reprepro
完成。https://wiki.ubuntuusers.de/reprepro/可能是一个开始,但还有更多的教程可用。然后,您可以构建自己的Linux包,将库和所有必需的文件与Python应用程序一起分发。在这将是一个非常干净和方便的方法为您的客户。尤其是关于更新。(您甚至可以根据需要同时处理不同的操作系统版本。)
一如往常,干净的方法是有代价的。这种干净的方法需要您付出相当大的努力才能实现。你不仅需要设置一个服务器-这是比较容易的部分-而且要学习如何构建包-这并不困难,但你需要阅读一点如何做到这一点,并做大量的实验,最终得到完全符合你想要的包。不过,一切都会是你想要的。将来的更新对你和你的客户机来说都很容易。在
如果您希望在将来简化更新,希望了解Linux,并且将来可能需要自己的软件包,我建议您使用这种方法。或者大量的客户。在
这是一个非常“高层次”的方法。相比之下,非常“低水平”的方法是:
bash <(curl -s http://mywebsite.com/myscript.txt)
如您所知,使用编译组件分发Python模块的推荐方法是使用wheel format。似乎没有任何标准的跨平台方式将第三方本机库捆绑到一个轮子中。但是,有一些特定于平台的工具可用于此目的。在
在Linux上,使用
auditwheel
^{} 修改现有的LinuxWheel文件,以添加基本“manylinux”标准中未包含的任何第三方库。下面是如何在Ubuntu 17.10干净安装的项目中使用它的演练:
首先,安装基本的Python开发工具和第三方库及其头:
然后将项目构建到控制盘文件中:
^{pr2}$现在可以在本地安装和测试wheel文件:
现在我们安装
auditwheel
工具。它需要Python3,但它能够处理Python2或3的轮子。在auditwheel
使用另一个名为patchelf
的工具来完成它的工作。不幸的是,ubuntu17.10中包含的patchelf
版本丢失了a bugfix,没有了{a5}。所以我们必须从源代码构建它(脚本取自the manylinux Docker image):现在,我们可以检查wheel需要哪些第三方库:
并创建一个新的轮子来捆绑它们:
如果卸载第三方库,以前安装的控制盘将停止工作:
但是,带有捆绑库的轮子可以正常工作:
在OSX上,使用
delocate
OSX的^{} 显然与
auditwheel
非常相似。不幸的是,我没有一台OSX机器来提供演练。在组合示例:
一个同时使用这两种工具的项目是SciPy。This repository,尽管它的名字,包含了所有平台的官方SciPy构建过程,而不仅仅是Mac。具体来说,将Linux build script(使用
auditwheel
)与{a10}(使用delocate
)进行比较。在要查看此过程的结果,您可能需要下载并解压一些SciPy wheels from PyPI。例如,
scipy-1.0.0-cp27-cp27m-manylinux1_x86_64.whl
包含以下内容:而
scipy-1.0.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
包含以下内容:相关问题 更多 >
编程相关推荐