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

How to wrap templated classes with pybind11

I've got a class structure similar to the one below, where I have two types A and B with similar signatures that only differ in argument/return type. I then use a class template to be able to process both classes, achieving a static variant of…
Johan
  • 135
  • 1
  • 6
8
votes
1 answer

Which $path is needed so g++/pybind11 could locate Python.h?

I started using pybind11 (on Ubuntu 16.04 with Python 2.7). To test the waters, I created a do-nothing wrapper around my c++ library. Alas, the compilation is unable to find Python.h: $ g++ -std=c++0x -fPIC -pedantic -g…
boardrider
  • 5,882
  • 7
  • 49
  • 86
7
votes
2 answers

Any C++ macro that can export all member variables of a struct for pybind11

I have a simple structure like: struct Config { bool option1; bool option2; int arg1; }; Using pybind11, I have to export the member variables like: py::class_(m, "Config") .def_readwrite("option1", &Config::option1) …
Jimmy Chen
  • 305
  • 2
  • 11
7
votes
1 answer

pybind11 for C++14/C++17

Does pybind11 work for C++14 and C++17 seamlessly ? I'm planning to use Boost.python for my project which is currently in C++11. In future I may have to upgrade to C++14 or C++17. So I wanted to understand what will be the right choice here -…
Vinu
  • 147
  • 1
  • 7
7
votes
2 answers

Best way to mark a pybind11-binding as deprecated

I have a C++ class with Python bindings using pybind11. Now I want to mark the binding of one method as deprecated. Let's assume it looks something like this: PYBIND11_MODULE(my_module, m) { pybind11::class_(m, "PyFoobar") …
luator
  • 4,769
  • 3
  • 30
  • 51
7
votes
1 answer

Pybind11: init<> with lambda

I use pybind11 as a wrapper of my C++ code into a python library. It happens that there are arguments that I can't provide or sometimes I want to do a conversion/initialization that I know in the C++ side. It could be because the class is not known…
Emile D.
  • 602
  • 2
  • 11
  • 20
7
votes
1 answer

Dynamically linking a shared library from a pybind11-wrapped code

I am trying to add python bindings to a medium-sized C++ scientific code (some tens of thousands LOCs). I have managed to make it work without too many issues, but I have now incurred in an issue which I am incapable of solving myself. The code is…
lr1985
  • 1,085
  • 1
  • 9
  • 21
7
votes
3 answers

returning multiple py::array without copying in pybind11

I am trying to build a python module in C++ using pybind11. I have the following code: #include #include #include namespace py = pybind11; struct ContainerElement { uint8_t i; …
Frank
  • 2,446
  • 7
  • 33
  • 67
7
votes
1 answer

How to set the include path in a pybind11 project

I have a large C++ library that I'm trying to expose using pybind11. I'm having trouble setting the include path correctly. The project directories are structured as follows: root - sub-project-1 -> C++ files that know nothing about python. -…
Steven Scott
  • 481
  • 3
  • 14
7
votes
1 answer

Named Default Arguments in pybind11

I'm using pybind11 to wrap a C++ class method in a conversion lambda "shim" (I must do this because reasons). One of the method's arguments is defaulted in C++. class A { void meow(Eigen::Matrix4f optMat = Eigen::Matrix4f::Identity()); }; In…
Adi Shavit
  • 16,743
  • 5
  • 67
  • 137
7
votes
1 answer

sphinx remove module prefix for args in automodule

I know how to remove the module prefix for functions in automodule directive by set "add_module_names = False". Remove package and module name from sphinx function However it can't remove the module prefix for the args in automodule directive.…
徐力有
  • 79
  • 4
7
votes
1 answer

pybind how can I operate over a py::list object

For a better understanding of how to pass arguments from Python to C++ functions with the pybind library, I wanted to build a small dummy/demo code where I could receive a Python list on the C++ side, cast it to a float pointer object, and then…
JuanPabloMF
  • 397
  • 1
  • 3
  • 14
7
votes
1 answer

Python and C++: How to use pybind11 with Cmakelists including GSL libraries

I want to be able to call my C++ code as a python package. To do this I am using pybind11 with CMakelists (following this example https://github.com/pybind/cmake_example). My problem is that I have to include GSL libraries in the compilation of the…
Duccio Piovani
  • 1,410
  • 2
  • 15
  • 27
7
votes
1 answer

Polymorphism and pybind11

I have a strange behaviour with pybind11 when I want to use C++ polymorphism in Python. Here is a simple example of my problem: import polymorphism as plm a = plm.mylist() print(a) a[0].print() a[1].print() The output of this script is…
Aleph
  • 1,343
  • 1
  • 12
  • 27
7
votes
2 answers

How to use pybind11 in multithreaded application

I want to run Python in a worker thread. However I get strange segfaults and deadlocks of the threads in the worker pool. How do I correctly use pybind11/Python C API to allow the threads to run the jobs? I know that it does not make much sense to…
ManuelSchneid3r
  • 15,850
  • 12
  • 65
  • 103
1 2
3
69 70