简体   繁体   English

如何安装C ++库的python绑定

[英]How to install python binding of a C++ library

Imaging that we are given a finished C++ source code of a library, called MyAwesomeLib . 成像我们获得了一个名为MyAwesomeLib的库的C ++源代码。 The goal is to expose some of its power to python, so we create a wrapper using swig and generated a python package called PyMyAwesomeLib . 目标是将其部分功能暴露给python,因此我们使用swig创建一个包装器并生成一个名为PyMyAwesomeLib的python包。

The directory structure now looks like 目录结构现在看起来像

root_dir
|-src/
|-lib/
|    |- libMyAwesomeLib.so
|    |- _PyMyAwesomeLib.so
|-swig/
|    |- PyMyAwesomeLib.py
|-python/
     |- Script_using_myawesomelib.py

So far so good. 到现在为止还挺好。 Ideally, all we want to do next is to copy lib/*.so swig/*.py and python/*.py into the corresponding directory in site-packages in a pythonic way, ie using 理想情况下,我们接下来要做的就是将lib/*.so swig/*.pypython/*.pypythonic方式复制到site-packages中的相应目录中,即使用

python setup.py install

However, I got very confused when trying to achieve this simple goal using setuptools and distutils . 但是,当我尝试使用setuptoolsdistutils实现这个简单的目标时,我感到非常困惑。 Both tools handles the compilation of python extensions through an internal system, where the source file, compiler flags etc. are passed using setup(ext_module=[Extension(...)]) . 这两个工具都通过内部系统处理python扩展的编译,其中源文件,编译器标志等使用setup(ext_module=[Extension(...)])传递。 But this is ridiculous since MyAsesomeLib has a fully functioning build system that is based on makefile. 但这很荒谬,因为MyAsesomeLib有一个基于makefile的功能完备的构建系统。 Porting the logic embedded in makefiles would be redundant and completely un-necessary work. 移植嵌入在makefile中的逻辑将是多余的并且是完全不必要的工作。

After some research, it seems there are two options left, I can either override setuptools.command.build and setuptools.command.install to use the existing makefile and copy the results directly, or I can somehow let setuptools know about these files and ask it to copy them during installation. 经过一些研究,似乎还有两个选项,我可以覆盖setuptools.command.buildsetuptools.command.install来使用现有的makefile并直接复制结果,或者我可以以某种方式让setuptools知道这些文件并询问它在安装过程中复制它们。 The second way is more appealing, but it is what gives me the most headache. 第二种方式更具吸引力,但它让我头疼。 I have tried the following optionts without success 我尝试了以下选项但没有成功

  • package_data , and include_package_data does not work because *.so files are not under version control and they are not inside of any package. package_datainclude_package_data不起作用,因为* .so文件不受版本控制,并且它们不在任何包内。
  • data_files does not seems to work since the files only get included when running python setup.py sdist , but ignored when python setup.py install . data_files似乎不起作用,因为文件仅在运行python setup.py sdist时包含,但在python setup.py install时被忽略。 This is the opposite of what I want. 这与我想要的相反。 The .so files should not be included in the source distribution, but get copied during the installation step. .so文件不应包含在源代码发行版中,而是在安装步骤中复制。
  • MANIFEST.in failed for the same reason as data_files . MANIFEST.in失败的原因与data_files相同。
  • eager_resources does not work either, but honestly I do not know the difference between eager_resources and data_files or MANIFEST.in . eager_resources也不起作用,但说实话,我不知道eager_resourcesdata_filesMANIFEST.in之间的区别。

I think this is actually a common situation, and I hope there is a simple solution to it. 我认为这实际上是一种常见的情况,我希望有一个简单的解决方案。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Porting the logic embedded in makefiles would be redundant and completely un-necessary work. 移植嵌入在makefile中的逻辑将是多余的并且是完全不必要的工作。

Unfortunately, that's exactly what I had to do. 不幸的是,这正是我必须要做的。 I've been struggling with this same issue for a while now. 我一直在努力解决同样的问题。

Porting it over actually wasn't too bad. 移植它实际上并不是太糟糕。 distutils does understand SWIG extensions , but it this was implemented rather haphazardly on their part. distutils确实理解SWIG扩展 ,但这是他们相当随意地实现的。 Running SWIG creates Python files, and the current build order assumes that all Python files have been accounted for before running build_ext . 运行SWIG创建Python文件,当前构建顺序假定在运行build_ext之前已考虑所有Python文件。 That one wasn't too hard to fix , but it's annoying that they would claim to support SWIG without mentioning this. 那个并不难解决 ,但是他们声称支持SWIG而不提这个就很烦人。 Distutils attempts to be cross-platform when compiling things, so there is still an advantage to using it. Distutils在编译时试图成为跨平台的,因此使用它仍然是一个优势。

If you don't want to port your entire build system over, use the system's package manager. 如果您不想移植整个构建系统,请使用系统的包管理器。 Many complex libraries do this (but they also try their best with setup.py). 许多复杂的库都这样做(但他们也尝试使用setup.py)。 For example, to get numpy and lxml on Ubuntu you'd just do: sudo apt-get install python-numpy python-lxml . 例如,要在Ubuntu上获取numpy和lxml,你只需要: sudo apt-get install python-numpy python-lxml No pip. 没有点子。

I realize you'd rather write one setup file instead of dealing with every package manager ever so this is probably not very helpful. 我意识到你宁愿写一个设置文件,而不是处理每个包管理器,所以这可能不是很有帮助。

If you do try to go the setuptools route there is one fatal flaw I ran into: dependencies. 如果你试图去setuptools路线,我遇到了一个致命的缺陷:依赖。

For instance, if you are distributing a SWIG-based project, it's going to need libpython. 例如,如果您要分发基于SWIG的项目,那么它将需要libpython。 If they don't have it, an error like this happens: 如果他们没有,会发生这样的错误:

#include <Python.h>
error: File not found

That's pretty unhelpful to the average user. 这对普通用户来说非常无益。

Even worse, if you require a shared library but the user's library is out of date, the user can get some crazy errors. 更糟糕的是,如果您需要共享库但用户的库已过期,则用户可能会遇到一些疯狂的错误。 You're at the mercy of their C++ compiler to output Google-friendly error messages so they can figure it out. 您可以使用他们的C ++编译器来输出Google友好的错误消息,以便他们能够解决这个问题。

The long-term solution would be to get setuptools/distutils to get better at detecting non-python libraries, hopefully as good as Ruby's gem. 长期解决方案是让setuptools / distutils更好地检测非python库,希望与Ruby的gem一样好。 I pretty much had to roll my own. 我几乎不得不自己动手。 For instance, in this setup.py I'm working on you can see a few functions at the top I hacked together for dependency detection (still doesn't work on all systems...definitely not Windows). 例如,在我正在开发的这个setup.py中 ,你可以看到顶层的一些函数我一起入侵以进行依赖项检测(仍然无法在所有系统上运行......绝对不是Windows)。

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

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