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
7
votes
1 answer

Smart way to CMake a project using pybind11 by ExternalProject_Add

I'm writing a python module using pybind11 with CMake 3.9.4. As it is convenience, I wish to download pybind11 source files using ExternalProject_Add in my CMakeLists.txt. When I run cmake ., it does not download pybind11 source files, and throws…
Pengin
  • 771
  • 1
  • 10
  • 16
7
votes
1 answer

pybind11 equivalent of boost::python::extract?

I am considering port of a complex code from boost::python to pybind11, but I am puzzled by the absence of something like boost::python::extract<...>().check(). I read pybind11::cast can be used to extract c++ object from a py::object, but the…
eudoxos
  • 18,545
  • 10
  • 61
  • 110
6
votes
1 answer

How to expose an array of opaque type with pybind11 and NumPy

TL;DR Using pybind11, how can I expose an array of POD struct with NumPy, while also having them appear to the user as nice Python objects? The problem I am exposing a C style API to Python using pybind11. There are some types, implemented as simple…
Julien Guertault
  • 1,324
  • 1
  • 15
  • 25
6
votes
1 answer

Cmake: using conan pybind11 package

I'm having trouble understanding how to use pybind11 conan package. I can use some others, but pybind11 is giving me hard time. My starting point is as follows: conanfile.txt: [requires] pybind11/2.7.1 [generators] cmake main.cpp: #include…
psarka
  • 1,562
  • 1
  • 13
  • 25
6
votes
1 answer

How to use pybind11 to convert between Eigen::Quaternion and numpy ndarray

I am trying to wrap up a c++ function that takes in Eigen::Quaternion as the argument for python usage. The function is defined as something like: void func(const Eigen::Quaternion &rotation) {...} I am using pybind11 to wrap it for python…
shelper
  • 10,053
  • 8
  • 41
  • 67
6
votes
0 answers

How to bundle external CMake dependencies to generate a Python package?

I am trying to generate Python bindings for the OpenVSlam project. I am using PyBind11 and following the cmake_example template. I successfully build the .so file using pybind11_add_module in my development environment, in which I have all…
josealeixo.pc
  • 391
  • 3
  • 13
6
votes
1 answer

How to reload python module that is build with c++ source code using pybind11

I am using the first step example in pybind11's documentation #include int add(int i, int j) { return i + j; } PYBIND11_MODULE(example, m) { m.doc() = "pybind11 example plugin"; // optional module docstring …
shelper
  • 10,053
  • 8
  • 41
  • 67
6
votes
1 answer

Can someone explain the pybind11 install?

The pybind11 documentation is generally good, but one area in which it is not is explaining the install process and the process of getting and running examples using cmake. I've managed to figure out how to get and build examples. But it leads to…
Andrew Voelkel
  • 513
  • 4
  • 10
6
votes
1 answer

How to convert Python dict to the C++ counter part in Pybind11?

Currently I'm trying to convert a py::dict into its counterpart C++s std::map. Trying to use the automatic conversion like this fails : #include namespace py = pybind11; using namespace py::literals; ... py::dict py_kwargs =…
Hossein
  • 24,202
  • 35
  • 119
  • 224
6
votes
1 answer

How can I build manually C++ extension with mingw-w64, Python and pybind11?

My final goal is to compile Python C++ extension from my C++ code. Currently to get started I am following a simple example from the first steps of pybind11 documentation. My working environment is a Windows 7 Professional 64 bit, mingw-w64…
Vladimir S.
  • 511
  • 4
  • 13
6
votes
2 answers

How to invoke Python function as a callback inside C++ thread using pybind11

I designed a C++ system that invokes user defined callbacks from procedure running in a separate thread. Simplified system.hpp looks like this: #pragma once #include #include #include #include class…
pptaszni
  • 5,591
  • 5
  • 27
  • 43
6
votes
1 answer

Return an std::vector to python as a numpy array

Using Pybind11, I am trying to pass a numpy array to c++ into a std::vector, multiply it by 2, and return this std::vector to python as a numpy array. I have achieved the first step but the third is doing some strange things. For passing it back I…
Joachim
  • 490
  • 5
  • 24
6
votes
1 answer

Pybind11 - Returning a pointer to a container of unique_ptr

I've been using the excellent pybind11 library but have hit a brick wall. I need to return to Python a pointer to a non-copyable object (as the object contains unique_ptrs). Generally this works fine with caveat of using…
AdrianH
  • 163
  • 6
6
votes
2 answers

building a pybind11 module with cpp and cuda sources using cmake

I'm trying to generate python bindings for a dummy class which needs to be compiled with a cuda enabled compiler. I'm using cmake 3.12.0, pybind11 v2.2.3 and nvcc 7.5.17. Compilation fails because options like -flto and -fno-fat-lto-objects are…
gravy
  • 121
  • 1
  • 6
6
votes
1 answer

how to use pybind11 with cmake for linking 2 modules

I am trying to combine a few modules, created by pybind11 and unfortunately can't get it to work. Hopefully someone can help out. I have tried to simplify the problem as much as possible. Am trying to create the following two modules: point: can be…
Matthijs
  • 439
  • 3
  • 16