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
5
votes
2 answers

Can I use pybind11 to pass a numpy array to a function accepting a Eigen::Tensor?

Can I use pybind1 to pass a three-dimensional numpy array to a c++ function accepting an Eigen::Tensor as argument. For example, consider the following c++ function: Eigen::Tensor addition_tensor(Eigen::Tensor a, …
fabian
  • 1,413
  • 1
  • 13
  • 24
5
votes
1 answer

Is there a (deep) copy constructor for pybind11::array_t?

I have an existing pybind11::array_t, and need to do a copy-construction. Is there a function inside pybind11 that allows me to do a deep copy of an array_t? I know that I could create a new array_t, size it properly, and then copy the original…
fnrizzi
  • 51
  • 2
5
votes
0 answers

How to implement bindings with PyBind11 that return Python Protobuf objects from C++?

I'm writing pybindngs for a C++ class that has Google Protobuf objects as member variables. I'd like these binded-functions to return (in Python land) the Python version of the protobuf object (which Google's proto compiler natively generates). If I…
5
votes
2 answers

Returning a list or tuple of arrays from pybind11 wrapping eigen

I have a c++ function using eigen, which is wrapped using pybind11 so that I can call it from python. A simple version of the intended function returns an Eigen::MatrixXd type, which pybind successfully converts to a 2D numpy array. I would like…
rxFt20
  • 257
  • 3
  • 11
5
votes
1 answer

unable to return shared_ptr from static function in pybind11

I tried to bind a static function which returns a shared_ptr pointed to another class. Here is the sample code class Example { public: Example() {} ~Example() {} }; class ABC { public: static std::shared_ptr get_example()…
alec.tu
  • 1,647
  • 2
  • 20
  • 41
5
votes
1 answer

Template metafunctions to generate pybind11 bindings

I am trying to create python bindings for some C++ functions using pybind11. The functions are templated but in the python bindings, I need one of the template parameter to be a function argument. Currently the way I have it is a lot of repetition…
Bilentor
  • 486
  • 1
  • 5
  • 13
5
votes
3 answers

How to apt install python-pybind11?

I see form my project document that I need to install python-pybind11 by using sudo apt -y install python-pybind11 but I got error like this: Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to…
user10838321
5
votes
0 answers

setup.py: add dependencies required for installation

For distributing Python libraries on PyPi, I usually specify the package's dependencies in setup.py à la setup( # ... install_requires=["numpy", "scipy"], # ... ) In some cases, however, I already need to import something in the…
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
5
votes
2 answers

Pybind11: Possible to use mpi4py?

Is it possible in Pybind11 to use mpi4py on the Python side and then to hand over the communicator to the C++ side? If so, how would it work? If not, is it possible for example with Boost? And if so, how would it be done? I searched the web…
Quasar
  • 295
  • 3
  • 13
5
votes
2 answers

pybind11, cmake: how to install files?

I'm interfacing a C++ library with the ever more popular pybind11 to get native Python bindings; configuration is via CMake. My CMakeLists.txt looks like cmake_minimum_required(VERSION 3.0) project(foo) FILE(GLOB foo_SRCS…
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
5
votes
3 answers

Referencing a C++ allocated object in pybind11

I'm trying to create a python binding with pybind11 that references a C++ instance whose memory is handled on the C++ side. Here is some example code: import struct Dog { void bark() { printf("Bark!\n"); } }; int main() { …
Dov Grobgeld
  • 4,783
  • 1
  • 25
  • 36
4
votes
1 answer

Pybind11 - Bound class method returns new class instance, rather than editing in-place

I am unable to return the input of a class method (input: specific instances of a seperate class) to Python. The binding compiles and I can use the resulting module in Python. The class method should however return the same instances as it admits…
JMy
  • 73
  • 8
4
votes
0 answers

Python/pip: How to do an editable package install with extension modules?

I am developing a python package with a C++ extension module that interops with the python code via pybind11, and which I am building using CMake (based on this answer). I have the package installed as editable to aid in development via pip install…
Ben Jones
  • 41
  • 1
4
votes
1 answer

Debug a Python C/C++ Pybind11 extension in VSCode [Linux]

Problem Statement I want to run and debug my own C++ extensions for python in "hybrid mode" in VSCode. Since defining your own python wrappers can be quite tedious, I want to use pybind11 to link C++ and python. I love the debugging tools of vscode,…
JakobThumm
  • 63
  • 1
  • 5
4
votes
1 answer

How to call a python function from C++ with pybind11?

Please consider the following C++ pybind11 program: #include namespace py = pybind11; int main() { py::scoped_interpreter guard{}; py::dict locals; py::exec(R"( import sys def f(): …
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319