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

Pybind11 for C++ code with inner struct created via static factory method

I have my own C++ that I'm trying to generate python binding using Pybind11: auto markerParams = MarkerDetector::Params::create(MarkerType::chessboard); markerDetector.detect(image, markerParams); I have problem of generating binding for…
pzo
  • 2,087
  • 3
  • 24
  • 42
6
votes
3 answers

How to wrap a singleton class using pybind11?

I have a singleton class in C++ (no public constructor, C++ programmers call class.instance() to create the singleton or return the existing one). I'd prefer to hide this at the Python level. If I was writing a Python singleton, I'd handle that in…
smontanaro
  • 1,537
  • 3
  • 15
  • 26
5
votes
1 answer

How to support len() method for Python enums created by Pybind11

Suppose that I have a C++ enumeration like this: enum class Kind {Kind1 = 1, Kind2, Kind3}; to bind this enumeration into a Python enumeration using Pybind11, I am doing something like this: py::enum_(py_module, "Kind") .value("Kind1",…
TonySalimi
  • 8,257
  • 4
  • 33
  • 62
5
votes
1 answer

Installing python extension module : understanding skbuild+setuptools

I am one of the devs of a (fairly large) C++ simulation tool. Disclaimer : I'm more of a physicist than a dev. I wrote Python bindings for that project using pybind11. I managed to get the Python module to compile with cmake. I then managed to write…
SergeD
  • 44
  • 9
5
votes
1 answer

C++ function not exported by .so seemingly because of pybind11 parameters

I am currently trying to define a shared library that I aim to use from a Python C++ extension as well as from vanilla C++ applications. I managed to build the shared library, and tried to link a simple C++ application against it to test its…
5
votes
1 answer

Passing Python large integers (>256bits) to and from C++ with pybind11

I'm developing a python wrapper for C++ based cryptography scheme where the keys and ciphertext are very large numbers (up to 2048 bits). In C++, I'm using Intel BigNumber class to store and handle such large numbers. As Python natively supports…
Ghostx
  • 113
  • 5
5
votes
1 answer

pybind11: Enum vs Enum Class?

I understand the difference between enums and enum classes in the context of C++, but in the context of binding enums and enum classes is there any real difference? Say for example: enum class options { maybe, yes, no, }; enum words { …
5
votes
0 answers

Making a cross-platform PyPI package with C++ extensions (wrapped with pybind11, built with bazel)

I have a hybrid C++/Python project. It uses pybind11 to wrap C++ code for Python and bazel to build the solution. I'm quite happy with this technology stack. Upon installing a C++ compiler, a Python distribution and bazel, a single bazel run command…
Andrei Matveiakin
  • 1,558
  • 1
  • 13
  • 17
5
votes
3 answers

Embedding a Python interpreter in a multi-threaded C++ program with pybind11

I'm trying to use pybind11 in order to make a 3rd party C++ library call a Python method. The library is multithreaded, and each thread creates a Python object, and then does numerous calls to the object's methods. My problem is that the call to…
bavaza
  • 10,319
  • 10
  • 64
  • 103
5
votes
1 answer

How to make a Python mock derive from a base class?

I am implementing python bindings for some C++ code using pybind11. Now I am trying to write unit tests for the bindings. There is a class A in C++ with constructor like this: class A { A(std::unique_ptr B_ptr); } It accepts a unique_ptr to…
In78
  • 440
  • 4
  • 17
5
votes
0 answers

pybind11: passing numpy array to c++ by value/reference

Here is my function signature auto fun(py::array_t x) The problem is that when passing array of np.float64, the array is not copied and can be modified in fun (which is not expected), when passing…
cncggvg
  • 657
  • 6
  • 12
5
votes
1 answer

Error by wrapping C++ abstract class with pybind11

I'm trying to make a simple example to wrap an abstract C++ class with pybind. The code is: #include #include #include namespace py = pybind11; using namespace std; class Base { public: virtual void…
mikanim
  • 409
  • 7
  • 21
5
votes
3 answers

CMake and pybind11 using inconsistent Python Versions

I am creating a starter project with CMake (3.16.3) and pybind11 (2.4.3) in VSCode (1.46.1) on Ubuntu (20.04) which has both Python 2.7 and 3.8 on it by default. I want to build a module for Python3. When I use the following two lines in my…
Steven
  • 253
  • 3
  • 11
5
votes
1 answer

CMake pybind11 cannot create target because another target with the same name already exists, how to bypass?

I have code which successfully runs. Its CmakeLists.txt is: cmake_minimum_required(VERSION 3.15) project(rotapro3) set(CMAKE_CXX_STANDARD 14) add_executable(rotapro3 main.cpp): I want to use pybind for this project, and following the instructions, I…
user12948426
5
votes
0 answers

Segfault when partially exporting class with virtual methods

(Note: I have already asked this on pybind11's GitHub. Reaching to SO for wider audience. Tagging boost.python too in case someone has insight, but the question is about how to do this with pybind11) Environment: Ubuntu 18.04, GCC 7.4.0, Python…
Boris Dalstein
  • 7,015
  • 4
  • 30
  • 59