Extending C ++ to Python with Pybind11

I have some code written in C ++ that I am trying to use in python without rewriting the complete code in python and I am using Pybind11 to create a python module to do this. I am trying to achieve this goal in Microsoft Visual Studio 2015 by following this tutorial here https://pybind11.readthedocs.io/en/stable/basics.html

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

2) Unzip the file

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

4) Added my python interpreter folder include (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 changed here Configuration Properties> General> Target Extension: .pyd

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

So, I can build my project and generate a .pyd file, but when importing this module, I get the following error: ImportError: dynamic module does not define init function (initProject11)

I was looking for this error and got this link http://pybind11.readthedocs.io/en/stable/faq.html but I couldn't find my solution.

So, I'm looking for a solution to the problem above. Thank you very much 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();
}

      

+3


source to share


1 answer


In python, the filename .pyd

must be the same as inside the module. 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()

. Then you can write Python "import foo" and Python will look for foo.pyd (as well as foo.py, foo.pyc) and if it finds it, it will try to call initfoo()

to initialize it.

In your code, you create a python module named example

, so the output file should be example.pyd

.



Edit:

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

ImportError: dynamic module does not define init function

  • Make sure the name specified in pybind::module

    and PYBIND11_PLUGIN

    is consistent and identical to the extension library file name. The latter should not contain any additional prefixes (for example, test.so instead of libtest.so).

  • If the above did not fix your problem, you may be using an incompatible version of Python (for example, the extension library was compiled against Python 2, while the interpreter is running on top of some version of Python 3, or vice versa)

+4


source







All Articles