
Python binding for C++ (pybind11)1 2
Luka Leskovec
University of Ljubljana, Faculty of Mathematics and Physics
Research software often sits at the boundary between two worlds: Python is excellent for rapid prototyping and data analysis, while C++ is well suited for performance-critical numerical kernels and efficient use of hardware resources. This workshop shows how to connect both worlds in a simple manner using pybind11.
Through practical examples, participants build Python extension modules from C++ code, work with type conversion, function and class bindings, STL containers, and introductory linear algebra examples with Eigen and GSL. A strong focus is placed on interface design between Python and C++, as well as common build, import, and performance pitfalls.
Schedule
The workshop is organized in modules. Participants move step by step from a minimal binding to more advanced topics (functions, types, classes, numerics, and best practices).
Materials
Workshop materials are provided in the following repositories:
Clone them with:
git clone https://github.com/leskovec/pyC_intro.git
git clone https://github.com/leskovec/pyC_part.I.git
git clone https://github.com/leskovec/pyC_part.II.git
Audience
The workshop is intended for researchers, engineers, students, and other users who combine Python workflows with high-performance C++ code.
Recommended prior knowledge
- basic Python programming,
- basic C++ programming,
- basic Linux command-line usage.
Learning outcomes
- building Python extensions from C++ with pybind11,
- understanding type conversion and function signature design across the Python/C++ boundary,
- binding C++ classes, methods, enums, and STL containers,
- connecting numerical C++ code (Eigen) with a Python interface,
- debugging import and linking problems in compiled modules,
-
The material is published under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license. ↩
-
This workshop was prepared within the framework of the EuroCC 2 project, aimed at establishing national competence centers for high-performance computing. More information about EuroCC 2 is available on the SLING website.
The EuroCC 2 project is funded by the European Union. It is funded by the EuroHPC Joint Undertaking and by Germany, Bulgaria, Austria, Croatia, Cyprus, Czech Republic, Denmark, Estonia, Finland, Greece, Hungary, Ireland, Italy, Lithuania, Latvia, Poland, Portugal, Romania, Slovenia, Spain, Sweden, France, the Netherlands, Belgium, Luxembourg, Slovakia, Norway, Türkiye, the Republic of North Macedonia, Iceland, Montenegro, and Serbia under grant agreement No. 101101903. ↩