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
40
votes
3 answers

returning numpy arrays via pybind11

I have a C++ function computing a large tensor which I would like to return to Python as a NumPy array via pybind11. From the documentation of pybind11, it seems like using STL unique_ptr is desirable. In the following example, the commented out…
mrupp
  • 531
  • 1
  • 4
  • 6
21
votes
1 answer

PyBind11 Template Class of Many Types

I'd like to use PyBind11 to wrap a specialized array class. However, the array is available in many flavours (one per each plain-old-datatype). The code looks like this: py::class_>(m, "Array2Dfloat", py::buffer_protocol(),…
Richard
  • 56,349
  • 34
  • 180
  • 251
20
votes
2 answers

Pybind11 or Boost.Python or neither-

I'm curious what the most flexible, most efficient, and most seamless method is for getting C++ and Python to talk to each other. The contenders seem to be Pybind11, Boost.Python, and neither (simply writing functions and wrappers as below). using…
user2723494
  • 1,168
  • 2
  • 15
  • 26
20
votes
2 answers

pybind11: how to package c++ and python code into a single package?

I am trying to package together an existing Python code and a new C++ 11 code using CMake and pybind 11. I think I am missing something simple to add into CMake scripts, but can't find it anywhere: pybind11 examples have only C++ code and none of…
seninp
  • 712
  • 1
  • 6
  • 23
18
votes
3 answers

call a Python function from c++ using pybind11

I am trying to call a python function from a C++ code which contains main() function using Pybind11. But I found very few references are available. Most of existing documents talk about the reversed direction, i.e. calling C++ from Python. Is there…
stanleyli
  • 1,427
  • 1
  • 11
  • 28
15
votes
4 answers

Is there a way to call an `async` python method from C++?

We have a codebase in python which uses asyncio, and co-routines (async methods and awaits), what I'd like to do is to call one of these method from a C++ class which has been pulled into python (using pybind11) Let's say there is this code: class…
Nim
  • 33,299
  • 2
  • 62
  • 101
15
votes
1 answer

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…
flamelite
  • 2,654
  • 3
  • 22
  • 42
13
votes
1 answer

Making a C++ module part of a Python package

I have the following directory layout awesome_package \- module1.py \- build \- module2.so I currently import module1 as import awesome_package.module1 and module2 as import sys sys.path.append('path/to/awesome_package/build') import…
user357269
  • 1,835
  • 14
  • 40
12
votes
4 answers

How to make cmake find pybind11

I am trying to follow the simple example for embedding python within c++ using pybind11 as found on this page. However, when trying to use cmake to build the solution, I keep getting an error that says By not providing "Findpybind11.cmake" in…
bwolf
  • 167
  • 1
  • 1
  • 6
12
votes
1 answer

passing pointer to C++ from python using pybind11

I have created the following class using pybind11: py::class_(m, "Raster") .def(py::init()); However I have no idea how I would call this constructor in…
Frank
  • 2,446
  • 7
  • 33
  • 67
11
votes
1 answer

How to convert raw pointers to lightweight python datatype using pybind11?

Consider this little pybind11 wrapper + test: setup.py from pybind11.setup_helpers import Pybind11Extension from pybind11.setup_helpers import build_ext from setuptools import setup setup( name="wrapper", …
BPL
  • 9,632
  • 9
  • 59
  • 117
11
votes
1 answer

How can I build a setup.py to compile C++ extension using Python, pybind11 and Mingw-w64?

I'm currently trying to write a 'setup.py' script that when the user installs the python package automatically compiles my C++ extension bound with 'pybind11'. In Windows, I haven't got any problem making it happen with the 'VS19 MSVC' compiler. But…
Rafael
  • 402
  • 5
  • 17
11
votes
4 answers

How to get the OpenCV image from Python and use it in C++ in pybind11?

I'm trying to figure out how it is possible to receive an OpenCV image from a Python in C++. I'm trying to send a callback function, from C++ to my Python module, and then when I call a specific python method in my C++ app, I can access the needed…
Hossein
  • 24,202
  • 35
  • 119
  • 224
11
votes
1 answer

With pybind11, how to split my code into multiple modules/files?

With pybind11, how to split my code into multiple modules/files? This would speed up the compilation step. Pybind11 documentation addresses the special case of extending a type declared in a different extension module, here. But not the more…
gmagno
  • 1,770
  • 1
  • 19
  • 40
11
votes
1 answer

pybind11, convert std::vector to py::list

According to the pybind11 documentation https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html: When including the additional header file pybind11/stl.h, conversions between std::vector<>/std::list<>/std::array<>,…
Ben Farmer
  • 2,387
  • 1
  • 25
  • 44
1
2 3
69 70