我试图用C++和CMake 11将现有的Python代码和新的CMake 11代码打包在一起。我想我遗漏了一些很简单的东西,可以添加到CMake脚本中,但在任何地方都找不到: pybind11示例中只有C++代码,没有Python,其他在线资源相当复杂,没有最新的版本--所以我只是想不出如何将两种语言中的函数打包在一起,并通过Python的import my_package在一行中提供它们.例如,我已经将cmake_example从pybind11和增加了一个多功能中克隆到了cmake_example/mult.py中。
def mult(a, b):
return a * b如何使add和subtract一起可见以通过下面的测试?
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目前,此测试失败..。
谢谢!
发布于 2017-12-05 10:42:29
最简单的解决方案与pybind11本身无关。当作者希望将纯Python和C/Cython/其他本地扩展合并到同一个包中时,他们通常做的事情如下。
创建两个模块。
mymodule是一个公共接口,是一个纯Python模块。_mymodule是一个私有实现,是一个已编译的模块。然后,在mymodule中,从_mymoudle导入必要的符号(如果需要的话,返回到纯_mymoudle版本)。
下面是来自纱,纱包的示例:
更新
下面是脚本。为了重现性,我是针对原始示例做的。
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发布于 2017-12-05 03:30:58
一旦您将回购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和./ $PYTHONPATH /my_mod都附加到您的中,它可能会工作(在我的示例中是这样)
https://stackoverflow.com/questions/47599162
复制相似问题