Skip to content Skip to sidebar Skip to footer

Extending C++ To Python Using Pybind11

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

Solution 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)

Post a Comment for "Extending C++ To Python Using Pybind11"