Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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();
}
like image 274
flamelite Avatar asked Jul 12 '17 10:07

flamelite


People also ask

Can you combine C with Python?

Extending Python with C or C++ It is quite easy to add new built-in modules to Python, if you know how to program in C. Such extension modules can do two things that can't be done directly in Python: they can implement new built-in object types, and they can call C library functions and system calls.

What is PyBind11 in Python?

pybind11 is a lightweight header-only library that exposes C++ types in Python and vice versa, mainly to create Python bindings of existing C++ code. Its goals and syntax are similar to the excellent Boost.

Can I use C++ libraries in Python?

In order to take advantage of the strength of both languages, developers use Python bindings which allows them to call C/C++ libraries from python.

How do you call a function in C++ Python?

Calling the Function You'll write the bindings, build them, and then run Python code to call them. Cython can support both C and C++. For this example, you'll use the cppmult library that you used for the PyBind11 example above.


1 Answers

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)

like image 197
pschill Avatar answered Oct 04 '22 23:10

pschill