pybind11:如何将 c++ 和 python 代码打包成一个包?

pybind11: how to package c++ and python code into a single package?

我正在尝试使用 CMake 和 pybind 11 将现有的 Python 代码和新的 C++ 11 代码打包在一起。我想我缺少一些简单的东西可以添加到 CMake 脚本中,但找不到它任何地方:pybind11 示例只有 C++ 代码和 none of Python,其他在线资源相当复杂且不是最新的——所以我只是不知道如何在两者中打包函数语言在一起,并通过 Python 的 import my_package 在线提供它们...例如,我已经将 cmake_example 从 pybind11 和 added a mult function 克隆到 cmake_example/mult.py

def mult(a, b):
    return a * b

如何让它与 addsubtract 一起可见才能通过下面的测试?

import cmake_example as m

assert m.__version__ == '0.0.1'
assert m.add(1, 2) == 3
assert m.subtract(1, 2) == -1
assert m.mult(2, 2) == 4

目前,这个测试fails..

谢谢!

克隆回购后,cd 到顶级目录`cmake_example'

更改 ./src/main.cpp 以包含 "mult" 函数:

#include <pybind11/pybind11.h>

int add(int i, int j) {
    return i + j;
}

int mult(int i, int j) {
   return i * j;
}

namespace py = pybind11;

PYBIND11_MODULE(cmake_example, m) {
    m.doc() = R"pbdoc(
        Pybind11 example plugin
        -----------------------

        .. currentmodule:: cmake_example

        .. autosummary::
           :toctree: _generate

           add
           subtract
           mult

    )pbdoc";

    m.def("add", &add, R"pbdoc(
        Add two numbers

        Some other explanation about the add function.
    )pbdoc");

   m.def("mult", &mult, R"pbdoc(
        Multiply two numbers

        Some other explanation about the mult function.
    )pbdoc");

(文件的其余部分相同)

现在开始:

$ cmake -H. -Bbuild
$ cmake --build build -- -j3

将在./build 目录中创建用于导入的模块。转到它,然后在 python shell 你的例子应该工作。

对于命名空间导入,您可以使用 pkgutil:

创建目录结构:

./my_mod
    __init__.py
    cmake_example.***.so

和另一个并行结构

./extensions
    /my_mod
        __init__.py
        cmake_example_py.py

并放在./my_mod/__init__.py

import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)

from .cmake_example import add, subtract
from .cmake_example_py import mult

./extensions/my_mod/__init__.py

from cmake_example_py import mult

然后将 ./my_mod 和 ./extensions/my_mod 附加到您的 $PYTHONPATH,它可能会起作用(在我的示例中确实如此)

最简单的解决方案与 pybind11 无关。当作者想要在同一包中组合纯 Python 和 C/Cython/other 原生扩展时,他们通常会做的如下。

您创建了两个模块。

  1. mymodule是一个public接口,一个纯Python模块
  2. _mymodule是私有实现,编译模块

然后在 mymodule 中从 _mymoudle 中导入必要的符号(如有必要则回退到纯 Python 版本)。

这是来自 yarl 包的示例:

  1. quoting.py

    try:
        from ._quoting import _quote, _unquote
        quote = _quote
        unquote = _unquote
    except ImportError:  # pragma: no cover
        quote = _py_quote
        unquote = _py_unquote
    
  2. _quoting.pyx

更新

下面是脚本。为了可重现性,我这样做是针对原始 cmake_example.

git clone --recursive https://github.com/pybind/cmake_example.git
# at the time of writing https://github.com/pybind/cmake_example/commit/8818f493  
cd cmake_example

现在创建纯 Python 模块(在 cmake_example/cmake_example 内)。

cmake_example/__init__.py

"""Root module of your package"""

cmake_example/math.py

def mul(a, b):
    """Pure Python-only function"""
    return a * b


def add(a, b):
    """Fallback function"""    
    return a + b    

try:
    from ._math import add
except ImportError:
    pass

现在让我们修改现有文件,将 cmake_example 模块变成 cmake_example._math

src/main.cpp(为简洁起见删除了subtract

#include <pybind11/pybind11.h>

int add(int i, int j) {
    return i + j;
}

namespace py = pybind11;

PYBIND11_MODULE(_math, m) {
    m.doc() = R"pbdoc(
        Pybind11 example plugin
        -----------------------

        .. currentmodule:: _math

        .. autosummary::
           :toctree: _generate

           add
    )pbdoc";

    m.def("add", &add, R"pbdoc(
        Add two numbers

        Some other explanation about the add function.
    )pbdoc");

#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)
project(cmake_example)

add_subdirectory(pybind11)
pybind11_add_module(_math src/main.cpp)

setup.py

# the above stays intact

from subprocess import CalledProcessError

kwargs = dict(
    name='cmake_example',
    version='0.0.1',
    author='Dean Moldovan',
    author_email='dean0x7d@gmail.com',
    description='A test project using pybind11 and CMake',
    long_description='',
    ext_modules=[CMakeExtension('cmake_example._math')],
    cmdclass=dict(build_ext=CMakeBuild),
    zip_safe=False,
    packages=['cmake_example']
)

# likely there are more exceptions, take a look at yarl example
try:
    setup(**kwargs)        
except CalledProcessError:
    print('Failed to build extension!')
    del kwargs['ext_modules']
    setup(**kwargs)

现在我们可以构建它了。

python setup.py bdist_wheel

在我的例子中它产生 dist/cmake_example-0.0.1-cp27-cp27mu-linux_x86_64.whl(如果 C++ 编译失败它是 cmake_example-0.0.1-py2-none-any.whl)。这是它的内容 (unzip -l ...):

Archive:  cmake_example-0.0.1-cp27-cp27mu-linux_x86_64.whl
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2017-12-05 21:42   cmake_example/__init__.py
    81088  2017-12-05 21:43   cmake_example/_math.so
      223  2017-12-05 21:46   cmake_example/math.py
       10  2017-12-05 21:48   cmake_example-0.0.1.dist-info/DESCRIPTION.rst
      343  2017-12-05 21:48   cmake_example-0.0.1.dist-info/metadata.json
       14  2017-12-05 21:48   cmake_example-0.0.1.dist-info/top_level.txt
      105  2017-12-05 21:48   cmake_example-0.0.1.dist-info/WHEEL
      226  2017-12-05 21:48   cmake_example-0.0.1.dist-info/METADATA
      766  2017-12-05 21:48   cmake_example-0.0.1.dist-info/RECORD
---------                     -------
    82775                     9 files