Questions tagged [pybind11]

5

votes
0

answer
35

Views

Template metafunctions to generate pybind11 bindings

I am trying to create python bindings for some C++ functions using pybind11. The functions are templated but in the python bindings, I need one of the template parameter to be a function argument. Currently the way I have it is a lot of repetition for each template parameter. enum MyEnum {E1, E2, E3...
Bilentor
1

votes
1

answer
1.1k

Views

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 following correct? #include 'pybind11/pybind11.h' #inc...
R zu
0

votes
0

answer
5

Views

importing std::vector into python using pybind11 gives python error

I'm trying to use pybind11's stl_bind header to no avail. I tried this: #include #include #include #include namespace py = pybind11; PYBIND11_PLUGIN(test) { py::module m('test', 'pybind11 example plugin'); py::bind_vector(m, 'std_vector'); } But when I try to use 'std_vector' in python I get th...
user2602914
1

votes
1

answer
237

Views

pybind11 multiple inheritance with custom holder type fails to cast to base types

Introduction: This question is specific to interfacing C++ to python using the pybind11 library. The issue is concerning wrapping C++ classes which engage in multiple inheritance, and using user-defined holder types around them in python (instead of unique_ptr or shared_ptr). Problem: Suppose there...
Gabor Varga
1

votes
1

answer
224

Views

Unable to bind rvalue reference arguments in member function

I'm using pybind11 to create python bindings for a C++ library whose source I cannot change. It contains a class that defines member functions with rvalue reference arguments (eg T &&val). I am unable to create a binding to a member function with rvalue reference arguments but binding to a non-mem...
Bob Barcklay
1

votes
1

answer
171

Views

Python bool conversion to C++ (pybind11)

I am attempting to convert a python bool to a C++ bool so that it can be used for comparison, but have not been able to successfully get it to. Here is the code that I have so far (commented out previous trials kept for reference). Any help would be greatly appreciated. So far it prints 'True' and '...
Markyroson
1

votes
1

answer
132

Views

PyBind11: constructor that uses pointer to string

I succeeded in binding this constructor .def(py::init()) My problem is when I have to use an array of strings, if I do like this alph2=['x','y'] z=Dfa(3,2,alph2) it fails saying: TypeError: __init__(): incompatible constructor arguments. The following argument types are supported: gi_gipy.Dfa(arg0:...
1

votes
0

answer
346

Views

how to use pybind11 with cmake for linking 2 modules

I am trying to combine a few modules, created by pybind11 and unfortunately can't get it to work. Hopefully someone can help out. I have tried to simplify the problem as much as possible. Am trying to create the following two modules: point: can be called directly line: can be called directly and al...
Matthijs
1

votes
0

answer
74

Views

Dealing with opaque pointers in pybind11

I am moving a Python module written in C++ from Boost.Python to Pybind11. My C++ code relies on a C library that has opaque pointers. With Boost.Python, I used the documentation here to deal with these opaque pointers: https://www.boost.org/doc/libs/1_66_0/libs/python/doc/html/reference/to_from_pyth...
sunmat
1

votes
1

answer
241

Views

What is the proper syntax to bind a typedef type using pybind11?

I have a struct very similar to this: struct data_point { data_point() = delete; data_point(const int& data) : m_data(data) {} int m_data; }; I also have this type declared as such. typedef std::vector data_list; The binding for this struct is defined: PYBIND11_MODULE(data_lib, ref) { py::class_ dp(...
Aaron Cofield
1

votes
1

answer
229

Views

pybind11 vs numpy for a matrix product

Edit 2 (18 June 2018.) I used the Matrix class proposed in http://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html with a Matrix product that I implemented as follows : Matrix product3(const Matrix &s1, const Matrix &s2) // M = M1 x M2 { size_t rowsM1 = s1.rows(); size_t colsM1 = s1.cols(...
1

votes
0

answer
42

Views

Python bindings for node-based tree structure

Context I am developing a 2D animation system where the data is organized as a node-based tree structure with semantics and functionality very similar to XML DOM -- but not quite similar enough to just use an existing XML implementation. For the sake of clarity, let's simplify and assume that the da...
Boris
1

votes
1

answer
204

Views

Pybind: Is it possible to inform Python about the names of the arguments for constructors?

In Pybind it is possible to inform Python about the names of the arguments for functions: m.def('add', &add, 'A function which adds two numbers', py::arg('i'), py::arg('j')); (http://pybind11.readthedocs.io/en/stable/basics.html#keyword-arguments) Is there something similar for constructors? Spyder...
thzu
1

votes
1

answer
348

Views

pass image data from python to cv::mat in c++

I am reading an image as cv2.imread('abc.tiff',1) from my python interface, I want to pass this to c++ function which is binded by pybind11. The C++ function requires a cv::Mat as input. Now I learned that python transforms that to NumPY , a NxM 3D array I find the data height, width, channels as 5...
humble_aspirant
1

votes
0

answer
77

Views

pybind11 emulate python Enum behaviour

I'm using pybind11 to provide a Python interface to my C++ library. My library contains enums for which I've provided convenience functions to allow iterating through the enum values and conversion to string; as an example template struct iter_enum { struct iterator { using value_type = Enum; using...
Dominic Price
1

votes
0

answer
52

Views

How to catch the exit signal raised by Py_Finalize()?

I am using pybind11 to embed python interpreter into C++ with pybind11::scoped_interpreter guard{}. I import my own module, run some functions, then exit the program. When the software exits, pybind11 eventually calls Py_Finalize(). I want to catch the exit signal inside my python module to call som...
abyesilyurt
1

votes
0

answer
50

Views

Using pybind11 to wrap an stl container (map) that includes Eigen arrays

I know how to pass Eigen arrays from c++ to Python. However, in my c++ code, I have an stl::map of Eigen arrays. For example, it would look like this std::map myMap; and I would have add elements to this map by doing: myMap['key1'] = Eigen::MatrixXd::Constant(100, 1, 0.0).array() myMap['key2'] = E...
tryingtosolve
1

votes
1

answer
55

Views

Compile and execute AST using pybind11 or Python C API

I would like to translate this Python code into C++ code using Pybind11 or directly the Python C API: import ast code = 'print('Hello World!')' code_ast = ast.parse(code, mode='exec') # 'code' being a string containing code # ... perform some modifications on 'code_ast' exec(compile(code_ast, filena...
Martin Renou
1

votes
0

answer
93

Views

Linking GSL statically using Cmake

I'm linking GNU GSL with a pybind11 module. It works if GSL is linked as a shared library: cmake_minimum_required(VERSION 2.8.12) project(st) # Paths set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} '${CMAKE_SOURCE_DIR}/cmake/') # Packages add_subdirectory(pybind11) find_package(GSL REQUIRED) # Includes s...
Medical physicist
1

votes
0

answer
29

Views

Multidirectory makefile with pybind

I'm trying to create a pybind project with multiple subdirectories. I managed to get the example.cpp file working with the following command (link below): c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suf...
Ross
1

votes
1

answer
78

Views

pybind11: Python to C++ Datatype Conversion Not Working

Problem I am trying to convert a list of lists, returned by a python function called inside a C++ code. Though the pybind11 library allows type conversion from python data types to C++ data types, my attempt to convert a list of lists returned by python to a std::list of std::list of strings of C++,...
MAXEINSTEIN
1

votes
0

answer
50

Views

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 work-around it is necessary to explicitly delete the Python...
GoFaster
1

votes
1

answer
65

Views

How to pass Python's keyword arguments with pybind11?

Given this function: __init__(username, password, **kwargs) with these keyword arguments: auto_patch: Patch the api objects to match the public API. Default: False drop_incompat_key: Remove api object keys that is not in the public API. Default: False timeout: Timeout interval in seconds. Default:...
Spixmaster
1

votes
0

answer
239

Views

Where to put .py in the C++ project so that I have access to it with pybind11?

Case I have created a C++ project called 'EmbedPython' in Visual Studio. As result I want to embed a Python API in C++ with pybind11. My Visual Studio Project This is the API: https://github.com/ping/instagram_private_api This is where I put the Python ApI (instagram_private_api, instagram_web_api):...
Spixmaster
1

votes
0

answer
15

Views

What is the way to pass a custom made struct (vector of pointers) from C++ to python using pybind11?

For a project, I need to pass a C++ struct to python as an input. I am not a lot familiar with C++, so I find hard to understand how to do it. The error I get is: libc++abi.dylib: terminating with uncaught exception of type pybind11::cast_error: Unable to convert call argument 'variable' of type 'Ex...
Pantelis
1

votes
1

answer
160

Views

Interface C/C++ using different Python tools [closed]

So I am doing a project that requires me to write some python codes to interface C/C++ programs, but I haven't decided which tool I should use. Intuitively, I'd like to choose between pybind11 and Boost.Python. Programmers: what are the pro and con between the twos? How would you decide which one to...
Javaian
1

votes
1

answer
88

Views

How to cast/convert a python function to std::function<double(double*)> in pybind11?

I am using pybind11 to implement binds for my c++ project. So, my problem is basically how to define a python function in the interpreter and call it from the C++ code. The C++ interface passes data using a pointer (double*) and I don't know how to code the function in the interpreter and how to c...
1

votes
1

answer
476

Views

Mixing Python and C++ source files in pybind11

I am using PyBind11 to make a Python project. My directory structure looks a like this: ./ my_pkg/ __init__.py func1.py func2.py My C++ code looks like this: int myfunc(){ return 1; } PYBIND11_PLUGIN(cppmodule) { py::module m('cppmodule', 'My cpp module'); m.def('myfunc',&myfunc,'This does stuff');...
Richard
1

votes
1

answer
496

Views

Wrapping STL container return types using Pybind11

I'm tying to wrap a C++ function (using Pybind11) that returns an STL container which is itself wrapped in a smart pointer. An example is shown below. The function is overloaded so I have to specify the signature. #include #include #include 'my_class.h' typedef std::array ArrayComplex4; PYBIND11_M...
nalyd88
1

votes
1

answer
640

Views

PyBind11: binding a function that uses double pointers

I would like to bind a C++-function with PyBind11. The problem is that this functions has an argument with a double pointer and the compiler raises an error error: cannot initialize a parameter of type 'char **' with an rvalue of type 'typename make_caster::cast_op_type' (aka 'char *'). Specificall...
Quasar
1

votes
1

answer
640

Views

Pybind11: Passing a Python list to C-style array

I would like to pass a Python list to a constructor which takes C-style arrays. How should that work. The problem is that the C-style array is essentially a pointer. Furthermore, the array has dimension n x n, i.e. it is a multi-dimensional array. PYBIND11_MODULE(matrix_class_bind, m){ py::class_(m,...
Quasar
1

votes
1

answer
71

Views

`overload_cast` fails in a specific case

I have found a specific case in which the overload_cast fails, and I have no idea how to solve this. A minimal example that shows the behavior is #include // ---------------- template void absolute( const InputIterator first, const InputIterator last, const OutputIterator result ) { for ( auto it =...
Tom de Geus
1

votes
1

answer
147

Views

Using built-in “open” python method in pybind11 (C++)?

How do you use basic ('default'/'built-in'; don't need to be imported) python methods in pybind11?Lists, dictionaries, and some others do have built-in compatibility, but the method I am looking specifically for (open) is not included via an import. I know that a way around it would be to create a p...
Markyroson
1

votes
1

answer
543

Views

C++ application crashes when embedded Python interpreter tries to import external module a second time

If I import an external module twice in different pybind11::scoped_interpreter sessions, the application crashes in eval.h in function eval at following line: PyObject *result = PyRun_String(buffer.c_str(), start, global.ptr(), local.ptr()); with Exception thrown at 0x00007FFD710C4E0C (multiarray.cp...
pseudomarvin
1

votes
1

answer
211

Views

Using scipy from C++ via pybind11

I would like to have the possibility to use python modules like numpy, scipy, etc. from C++. The following code tries to call scipy.optimize.curve_fit to fit a parabolic function. A problem occures when calling curve_fit. Here, an exception is thrown. #include #include #include #include...
Dragoner
1

votes
1

answer
44

Views

Address changes upon transferring Eigen array from c++ to python

I make a c++ function to create numpy arrays with the correct memory alignment. Somehow, python and c++ print two different addresses for the 'same' array. Does that means pybind11 still copy the array? If that is the case, how to avoid copying? The pybind11 is from: https://github.com/pybind/pybind...
R zu
1

votes
1

answer
119

Views

pybind11: using setuptools to link

I'm trying to use Python's setuptools to build a variant of this example. In the example, there is only one file, main.cpp. However, in my version, I'm adding in another class. Therefore, there are a total of three files: main.cpp #include #include 'myClass.h' namespace py = pybind11; PYBIND11_MODU...
tryingtosolve
1

votes
1

answer
98

Views

error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Dp> class std::unique_ptr’

Before we start, I am a complete noob in C++11, had used C several years ago. I am trying to write a python binding of a C++11 code using pybind11 and is getting a subjected error. I am basically following this guide from Nvidia people and is stuck at this error. Can any nice soul point me to the r...
Khurram Amin
1

votes
1

answer
211

Views

correct setup.py for mixing Python and C++

I'm trying to mix both languages and I'm following the nice example provided by pybind here. I actually checked this post to improve on it so I can fall back to Python functions whenever a compiled function doesn't exist. The problem I have now is that my configure.py is not building the correct pac...
aaragon
1

votes
1

answer
99

Views

How to use Eigen::Ref with pybind11?

I'm using pybind to wrap C++ code. Given the following function, which is originally part of a library and should not be modified: void manipulate(Eigen::MatrixXd& data) { data = data*2; } Using pybind, I can wrap it as: #include #include #include namespace py = pybind11; void wrap1(Eigen::Ref...
ilciavo

View additional questions