Extending C++ to Python using Pybind11

强颜欢笑 提交于 2021-02-06 08:52:12

问题


I have got some code written in c++ which i am trying to use in python without rewriting the complete code in python again and i am using Pybind11 to build a python module for that. I am trying to achieve this thing in Microsoft Visual Studio 2015 by following this tutorial here https://pybind11.readthedocs.io/en/stable/basics.html

I did following things in visual studio. 1) Downloaded Pybind11 from https://codeload.github.com/pybind/pybind11/zip/master

2) Unzipped the file

3) In visual studio a started a new empty C++ project.

4) Added my python interpreter include folder ( C:/python27/include) and Pybind11( C:/Pybind11/include) in VC++ directories > include directories

5) Added additional dependencies (C:\Python27\libs\python27.lib) in Linker>input>Additional dependencies

6) To use the output file in Python i need a .pyd file so i modified here Configuration Properties>General>Target extension : .pyd

7) Change project defaults > configuration type to Dynamic Library (.dll)

So i am able to build my project and generate the .pyd file but when importing this module i am getting following error: ImportError: dynamic module does not define init function (initProject11)

I searched for this error and got this link http://pybind11.readthedocs.io/en/stable/faq.html but i could not find my solution.

So i am looking for the solution for above problem. Thanks a lot in advance.

here is my CPP file code

#include <pybind11/pybind11.h>

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

namespace py = pybind11;

PYBIND11_PLUGIN(example) {
    py::module m("example", "pybind11 example plugin");

    m.def("add", &add, "A function which adds two numbers");

    return m.ptr();
}

回答1:


In python, the name of the .pyd file must be the same as the module that is inside. From the documentation (https://docs.python.org/2/faq/windows.html):

If you have a DLL named foo.pyd, then it must have a function initfoo(). You can then write Python “import foo”, and Python will search for foo.pyd (as well as foo.py, foo.pyc) and if it finds it, will attempt to call initfoo() to initialize it.

In your code you create a python module with the name example, so the output file must be example.pyd.

Edit:

The pybind11 FAQ mentions an incompatible python version as another possible error source (https://pybind11.readthedocs.io/en/stable/faq.html):

ImportError: dynamic module does not define init function

  1. Make sure that the name specified in pybind::module and PYBIND11_PLUGIN is consistent and identical to the filename of the extension library. The latter should not contain any extra prefixes (e.g. test.so instead of libtest.so).

  2. If the above did not fix your issue, then you are likely using an incompatible version of Python (for instance, the extension library was compiled against Python 2, while the interpreter is running on top of some version of Python 3, or vice versa)



来源:https://stackoverflow.com/questions/45054860/extending-c-to-python-using-pybind11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!