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

Access violation when trying to read out object created in Python passed to std::vector on C++ side and then returned to Python

Working with VS 2019, Python 3.7 64bit on Windows 10 and pybind11 2.4.3 I have run into the following problem: When I create an object with a pybind11 py::class_ on the Python side and pass it directly to a method on the C++ side storing it in a…
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
4
votes
1 answer

How to build and execute a cpp file with embedded python interpreter(pybind11)

I'm trying to find a way to build and run a cpp file with an embedded python interpreter using pybind11. From this tutorial, it uses CMake but I'm looking for a way to do this without CMake. Here's what I tried. In example.cpp: #include…
Zack Lee
  • 2,784
  • 6
  • 35
  • 77
4
votes
1 answer

Problems passing a std::vector by reference though virtual functions using pybind11

I have some C++ code being bound to Python via pybind11, and we are having a strange issue with passing std::vector by reference through a virtual method and having the changes to that std::vector persist outside that method. What we want to do is…
Mike Owen
  • 41
  • 2
4
votes
0 answers

memory usage of c++ module in python

I have written a c++ module that I want to use in Python, using pybind11. It contains, amongst other, this code: class Foo{ ... void Foo::Bar(BigObject& returnBigObject) { for (int i=0; i<4; i++) { …
Frank
  • 2,446
  • 7
  • 33
  • 67
4
votes
3 answers

pybind11 Redirect python sys.stdout to c++ from print()

I have a c++ program with a pybind11 embedded python interpreter, executing the following python file, it prints directly to std::cout # test.py print("text") The c++ program executing the file: #include namespace py =…
jaihysc
  • 55
  • 1
  • 7
4
votes
0 answers

pybind11 crash on constructor call with reference

I have the following code: #include namespace py = pybind11; class Foo { private: int value; public: Foo(int val) : value(val) {}; int getValue() { return value; } }; class Bar { private: Foo& mFoo; public: …
Frank
  • 2,446
  • 7
  • 33
  • 67
4
votes
3 answers

Python.h not found while building sample application with cmake and pybind11

I want to build simple app with pybind11, pybind is already installed in my Ubuntu system with cmake (and make install). I use this simple cmake file: cmake_minimum_required(VERSION 3.0 FATAL_ERROR) project(trt_cpp_loader ) find_package(pybind11…
Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206
4
votes
0 answers

custom type casting - pybind11

I'm trying to cast a custom type according to the pybind11 docs. But I don't know how to cast a struct with multiple field. what I'm trying to do so far is below struct vect { vect(int x, int y): x(x), y(y){} int x, y; }; …
some dev
  • 99
  • 1
  • 6
4
votes
0 answers

pybind11 how to use custom type caster for simple example class

Motivation I am currently trying to use a custom class both in python and in c++ with pybind11. The motivation behind this is to use a python-trained classifier within c++. There are some working examples, e.g. in the official documentation…
raef
  • 51
  • 2
  • 4
4
votes
1 answer

Use C++ object in python with pybind11

My main.cpp file looks like this: // Embeding the interpreter into c++ // https://pybind11.readthedocs.io/en/master/advanced/embedding.html #include #include #include // Define namespace for…
Blind0ne
  • 1,015
  • 12
  • 28
4
votes
1 answer

How to implement pybind11 with buffer protocol for non trivial (POD) class

I've just learned about Python Buffer Protocol and I would like to leverage it to create python numpy arrays from C++ raw data. I can either use pybind11 or c++ python lib directly, but no other binding generator =/ Reading pybind11 docs and…
4
votes
2 answers

Python bindings using pybind11 with std::filesystem as function argument giving TypeError

I have a class Foo() and class Foo() has a function with the following declaration: bool Foo::copyFile(const std::filesystem::path& src, const std::filesystem::path& dest) The requirement is that the class Foo should have Python bindings. I am using…
user304255
  • 121
  • 1
  • 7
4
votes
3 answers

PyBind11 destructor not invoked?

I have a c++ class wrapped with PyBind11. The issue is: when the Python script ends the c++ destructor is not being automatically invoked. This causes an untidy exit because networking resources need to be released by the destructor. As a…
GoFaster
  • 835
  • 1
  • 12
  • 23
4
votes
1 answer

pybind11: return c++ class (with an existing python binding) to python

I am trying to create a wrapper for a c++ method that returns a c++ class(vtkPolyData) which comes from an external c++ library (vtk). The same library has python binding available which is already installed in my python environment. How do you tell…
Marc Wang
  • 171
  • 2
  • 12
4
votes
1 answer

How do you call the destructor or when is it automatically called from python in pybind11?

If I am using the take_ownership return value policy in pybind11, and I invoke a function that returns, say, a std::vector, how can I ensure that the vector and its contents' destructors are called? Does it have anything to do with going out of…
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126