Questions tagged [pybind11]

pybind11 is a C++/Python package offering seamless operability between C++11 and Python, in the spirit of “boost::python” but without the heavy-duty Boost dependency.

pybind11 — Seamless operability between C++11 and Python.

The following core C++ features can be mapped to Python:

  • Functions accepting and returning custom data structures per value, reference, or pointer
  • Instance methods and static methods
  • Overloaded functions
  • Instance attributes and static attributes
  • Many more…

In addition to the core functionality, pybind11 provides some extra goodies:

  • Python 2.7, 3.x, and PyPy (PyPy2.7 >= 5.7) are supported with an implementation-agnostic interface.
  • It is possible to bind C++11 lambda functions with captured variables. The lambda capture data is stored inside the resulting Python function object.
  • pybind11 uses C++11 move constructors and move assignment operators whenever possible to efficiently transfer custom data types.
  • Still more…

Supported compilers:

  • Clang/LLVM (any non-ancient version with C++11 support)
  • GCC 4.8 or newer
  • Microsoft Visual Studio 2015 or newer
  • Intel C++ compiler v15 or newer

See documentation at https://pybind11.readthedocs.io/en/latest/index.html

1040 questions
0
votes
1 answer

Instantiate wrapper class from C++

i have wrapped a simple c++ interface / class using pybind11 py::class_>(m, "BaseObject") .def(py::init([]() { return BaseObject_Create(); })) ) IBaseObject is interface, SmartPtr is custom…
0
votes
1 answer

How to handle Celery WorkerLostError due to c++ assertion in python imported module?

I have a python module with c++ code using pybind11 that I have imported in my django + celery 3.1.25 app. The c++ code contains assertions that might trigger inside of a celery worker which then leads to the WorkerLostError. I have tried to put the…
Ronny
  • 454
  • 4
  • 15
0
votes
1 answer

read from pointer argument of a c++ function in python with pybind11

I have a c++ function like: int add(int *i, int j) { *i += 3; return *i + j; } I have created python binding for it using pybind11 as PYBIND11_MODULE(example, m) { m.doc() = R"pbdoc(add)pbdoc"; m.def("add", &add, R"pbdoc(Add two…
0
votes
1 answer

How with pybind11 to bind a function that takes as argument a numpy.array() with, for instance, a shape (10, 10, 3)?

I would like to write a function that can take a multidimensional numpy array, not just 2D. void compute(Eigen::Ref array3d) { // change the array in-place // ... } or Eigen::MatrixXd &compute() { // create array //…
gmagno
  • 1,770
  • 1
  • 19
  • 40
0
votes
1 answer

pybind11 bad_alloc problem with std::vector>

I have structs like this: struct A{ void do_stuff(){...does stuff}; } struct B{ std::vector> objs; } my pybind: py::class_(m, "A") .def("do_stuff", &A::do_stuff) py::class_(m, "B") .def_readwrite("objs",…
0
votes
1 answer

pybind11 cmake example cannot find the main function

I git cloned pybind11's cmake exmaple. Then I built it with pip install ./cmake_example. My python file contains the following: import cmake_example print(cmake_example.add(1, 2)) This works fine. Now I want to use pybind11's interpreter. I changed…
user3132457
  • 789
  • 2
  • 11
  • 29
0
votes
1 answer

Nested dict and pybind11

I have a C++ extension bundled with Python by using pybind11. The extension returns a nested Python dict object: #include namespace py = pybind11; py::dict cpp_ext(void) { // Variables py::dict res; // Result …
Medical physicist
  • 2,510
  • 4
  • 34
  • 51
0
votes
2 answers

PyBind11 with SystemC using CMake: ImportError

I'm working with a SystemC project into which I would like to add python bindings using PyBind11 and I've been following the docs on the site here to write the bindings. I'm using CMake to build my project and the challenge I'm facing is that I need…
mankeyboy
  • 11
  • 5
0
votes
0 answers

How to catch mwclient errors in pybind11?

How would I go about catching mwclient errors in pybind11? I am specifically looking to catch mwclient.ProtectedPageError and mwclient.EditError (in that order). I am asking since python exceptions seem to just be considered py::error_already_set in…
Markyroson
  • 109
  • 10
0
votes
0 answers

Pybind11 Virtual Child Class Method not being called during recursion, Base Called instead

I have a simple test case for polymorphism that isn't working quite right.. I have two C++ classes, Client and Dispatcher. The dispatcher has a method called "Dispatch" that takes a Client* as an input and calls the clients virtual ProcessEvent()…
IAS_LLC
  • 135
  • 11
0
votes
2 answers

cgal c++ integration in python3.6 template error

I try to integrate cgal library to my python program. I made some test by following diverse tutorial. And I try to do a template but I have an error when I compile and I don't know how to fixe it. I use pybind11 to integrate cgal. Part of my…
sophieT
  • 3
  • 1
0
votes
0 answers

Error during compilation when using pybind11

I'm trying to compile a basic example copied and pasted directly from pybind11, with g++-8 on a Mac. This is the c++ code (example.cpp): #include int add(int i, int j) { return i + j; } PYBIND11_MODULE(example, m) { …
tryingtosolve
  • 763
  • 5
  • 20
0
votes
0 answers

Including pybind11 cmake files with Hunter

I am using Hunter to manage dependencies of my project. One of them is pybind11 It has been installed correctly and I can use it in my code. However when building my project with CMake I need to use some cmake function provided by pybind11 in some…
EntrustName
  • 421
  • 6
  • 19
0
votes
2 answers

How to bind using pybind11?

This is the structure of my program: I'm trying to bind my program in C++ with a GUI in python. I'm using pybind11 and I have a python_binding.cpp file for the bind and some ".h" and ".cpp" with the methods in other directories. I include the ".h"…
Yorch
  • 109
  • 1
  • 10
0
votes
1 answer

python embedding in C++ with functions returning shared_ptr(pybind11/boost_python)

I am currently working on a usecase where I have implemented APIs in python and they need to return struct, vector and map to C++ caller functions. Could you please help me with an example how to do that. All the usecases I see in pybind/boost…