python编译成c打包_pybind11:如何将c和python代码打包成一个包?

最简单的解决方案与pybind11无关.当作者想要在同一个包中组合纯Python和C/C++ython /其他原生扩展时,通常会做什么,如下所示.

您创建了两个模块.

> mymodule是一个公共接口,一个纯Python模块

> _mymodule是一个私有实现,一个编译模块

然后在mymodule中从_mymoudle导入必要的符号(如果需要,可以回退到纯Python版本).

这是yarl包的示例:

try:

from ._quoting import _quote, _unquote

quote = _quote

unquote = _unquote

except ImportError: # pragma: no cover

quote = _py_quote

unquote = _py_unquote

更新

以下是脚本.为了重复性,我正在对着原始的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(为简洁起见,删除了减法)

#include

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).这是它的内容(解压缩-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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值