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 t
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 functioninitfoo()
. 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 callinitfoo()
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
Make sure that the name specified in
pybind::module
andPYBIND11_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).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)