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

Cast NumPy array to/from custom C++ Matrix-class using pybind11

I am trying to wrap my C++ code using pybind11. In C++ I have a class Matrix3D which acts as a 3-D array (i.e. with shape [n,m,p]). It has the following basic signature: template class Matrix3D { public: std::vector data; …
Tom de Geus
  • 5,625
  • 2
  • 33
  • 77
10
votes
1 answer

Handling GIL when calling python lambda from C++ function

The question Is pybind11 somehow magically doing the work of PyGILState_Ensure() and PyGILState_Release()? And if not, how should I do it? More details There are many questions regarding passing a python function to C++ as a callback using pybind11,…
JonasVautherin
  • 7,297
  • 6
  • 49
  • 95
10
votes
1 answer

Pybind11: Create and return numpy array from C++ side

How to create a numpy array from C++ side and give that to python? I want Python to do the clean up when the returned array is no longer used by Python. C++ side would not use delete ret; to free the memory allocated by new double[size];. Is the…
R zu
  • 2,034
  • 12
  • 30
10
votes
1 answer

PyBind11 Global-Level Enum

The PyBind11 documentation talks about using enum here. The example shown assumes that the enum is embedded within a class, like so: struct Pet { enum Kind { Dog = 0, Cat }; Pet(const std::string &name, Kind type) :…
Richard
  • 56,349
  • 34
  • 180
  • 251
10
votes
2 answers

Where are include and libs when using a Python virtual environment?

I use a Python virtual environment. Basically, it works fine, but I run into problems when compiling some Python bindings, namely with libIGL and pybind11. CMake has the following Python-related variables: PYTHON_EXECUTABLE …
Michael
  • 7,407
  • 8
  • 41
  • 84
9
votes
1 answer

Pybind11 and std::vector -- How to free data using capsules?

I have a C++ function that returns a std::vector and, using Pybind11, I would like to return the contents of that vector as a Numpy array without having to copy the underlying data of the vector into a raw data array. Current Attempt In this…
9
votes
2 answers

pybind11 modify numpy array from C++

EDIT: It works now, I do not know why. Don't think I changed anything I want to pass in and modify a large numpy array with pybind11. Because it's large I want to avoid copying it and returning a new one. Here's the code: #include…
Nimitz14
  • 2,138
  • 5
  • 23
  • 39
9
votes
1 answer

Returning and passing around raw POD pointers (arrays) with Python, C++, and pybind11

I have a C++ function which returns a raw float pointer, and another C++ function which accepts a raw float pointer as an argument. Something like: float* ptr = something; float* get_ptr(void) { return ptr; } void use_ptr(float* ptr) { do_work(ptr);…
AstrOne
  • 3,569
  • 7
  • 32
  • 54
9
votes
3 answers

pybind11 wrapping existing code

I am trying to wrap a c++ library using pybind11 so I can use it with Python 3.x. I tried wrapping the code using swig, but I ran into an issue where SWIG would generate the cxx file, but would not read the headers I was referencing, so it was…
code base 5000
  • 3,812
  • 13
  • 44
  • 73
8
votes
1 answer

pybind11 running the test cases

I'm trying to learn pybind11 and the first Google result is this page, where you should be guided towards compiling and running some test cases. From this page, I have installed bybind11 by: pip3 install pybind11 and I have installed: sudo apt…
Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
8
votes
1 answer

Undefined Symbol: _ZN3a13A when importing pybind11 bindings

I'm trying to create pybind11 bindings for an existing cmake project. The CMakeLists.txt file looks like the one in the tutorial. The project builds without errors, however, when trying to import the module in ipython, the following error comes…
Armin Meisterhirn
  • 801
  • 1
  • 13
  • 26
8
votes
1 answer

Splitting up pybind11 modules and issues with automatic type conversion

I have a set of modules that I've written in C++ and exported to Python using pybind11. All of these modules should be able to be used independently, but they use a common set of custom types that are defined in a utility library. In each module…
ktb
  • 1,498
  • 10
  • 27
8
votes
2 answers

Pybind Numpy access 2D / ND arrays

New to pybind - read the documentation but I do not grasp how to apply it to 2D arrays. I have two arrays storing 3d coordinates shape = (10,3) a = np.zeros(shape=(10,3)) b = np.ones(shape=(10,3)) * 3 c = a + b Now, using pybind, how do I perform…
El Dude
  • 5,328
  • 11
  • 54
  • 101
8
votes
3 answers

PyBind - Overloaded functions

Firstly, my thanks to all of you for trying to resolve this doubt of mine. I am working on converting a minimal C++ project to be used in Python. The real reason behind this effort is for speed. I came across PyBind and was quite surprised at its…
8
votes
3 answers

pybind11 "Python is 64-bit, chosen compiler is 32-bit"

I'm trying to compile pybind11 on a Windows machine that has VisualStudio 2015 installed. I also have python 3.5.3 64bit installed, and cmake 2.8.12. I get the error: CMake Error at tools/FindPythonLibsNew.cmake:122 (message): Python config…
Periodic Maintenance
  • 1,698
  • 4
  • 20
  • 32
1
2
3
69 70