• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. figure:: https://github.com/pybind/pybind11/raw/master/docs/pybind11-logo.png
2   :alt: pybind11 logo
3
4**pybind11 — Seamless operability between C++11 and Python**
5
6|Latest Documentation Status| |Stable Documentation Status| |Gitter chat| |CI| |Build status|
7
8|Repology| |PyPI package| |Conda-forge| |Python Versions|
9
10`Setuptools example <https://github.com/pybind/python_example>`_
11• `Scikit-build example <https://github.com/pybind/scikit_build_example>`_
12• `CMake example <https://github.com/pybind/cmake_example>`_
13
14.. start
15
16.. warning::
17
18   Combining older versions of pybind11 (< 2.6.0) with Python 3.9.0 will
19   trigger undefined behavior that typically manifests as crashes during
20   interpreter shutdown (but could also destroy your data. **You have been
21   warned.**)
22
23   We recommend that you update to the latest patch release of Python (3.9.1),
24   which includes a `fix <https://github.com/python/cpython/pull/22670>`_
25   that resolves this problem. If you do use Python 3.9.0, please update to
26   the latest version of pybind11 (2.6.0 or newer), which includes a temporary
27   workaround specifically when Python 3.9.0 is detected at runtime.
28
29
30**pybind11** is a lightweight header-only library that exposes C++ types
31in Python and vice versa, mainly to create Python bindings of existing
32C++ code. Its goals and syntax are similar to the excellent
33`Boost.Python <http://www.boost.org/doc/libs/1_58_0/libs/python/doc/>`_
34library by David Abrahams: to minimize boilerplate code in traditional
35extension modules by inferring type information using compile-time
36introspection.
37
38The main issue with Boost.Python—and the reason for creating such a
39similar project—is Boost. Boost is an enormously large and complex suite
40of utility libraries that works with almost every C++ compiler in
41existence. This compatibility has its cost: arcane template tricks and
42workarounds are necessary to support the oldest and buggiest of compiler
43specimens. Now that C++11-compatible compilers are widely available,
44this heavy machinery has become an excessively large and unnecessary
45dependency.
46
47Think of this library as a tiny self-contained version of Boost.Python
48with everything stripped away that isn’t relevant for binding
49generation. Without comments, the core header files only require ~4K
50lines of code and depend on Python (2.7 or 3.5+, or PyPy) and the C++
51standard library. This compact implementation was possible thanks to
52some of the new C++11 language features (specifically: tuples, lambda
53functions and variadic templates). Since its creation, this library has
54grown beyond Boost.Python in many ways, leading to dramatically simpler
55binding code in many common situations.
56
57Tutorial and reference documentation is provided at
58`pybind11.readthedocs.io <https://pybind11.readthedocs.io/en/latest>`_.
59A PDF version of the manual is available
60`here <https://pybind11.readthedocs.io/_/downloads/en/latest/pdf/>`_.
61And the source code is always available at
62`github.com/pybind/pybind11 <https://github.com/pybind/pybind11>`_.
63
64
65Core features
66-------------
67
68
69pybind11 can map the following core C++ features to Python:
70
71- Functions accepting and returning custom data structures per value,
72  reference, or pointer
73- Instance methods and static methods
74- Overloaded functions
75- Instance attributes and static attributes
76- Arbitrary exception types
77- Enumerations
78- Callbacks
79- Iterators and ranges
80- Custom operators
81- Single and multiple inheritance
82- STL data structures
83- Smart pointers with reference counting like ``std::shared_ptr``
84- Internal references with correct reference counting
85- C++ classes with virtual (and pure virtual) methods can be extended
86  in Python
87
88Goodies
89-------
90
91In addition to the core functionality, pybind11 provides some extra
92goodies:
93
94- Python 2.7, 3.5+, and PyPy/PyPy3 7.3 are supported with an
95  implementation-agnostic interface.
96
97- It is possible to bind C++11 lambda functions with captured
98  variables. The lambda capture data is stored inside the resulting
99  Python function object.
100
101- pybind11 uses C++11 move constructors and move assignment operators
102  whenever possible to efficiently transfer custom data types.
103
104- It’s easy to expose the internal storage of custom data types through
105  Pythons’ buffer protocols. This is handy e.g. for fast conversion
106  between C++ matrix classes like Eigen and NumPy without expensive
107  copy operations.
108
109- pybind11 can automatically vectorize functions so that they are
110  transparently applied to all entries of one or more NumPy array
111  arguments.
112
113- Python’s slice-based access and assignment operations can be
114  supported with just a few lines of code.
115
116- Everything is contained in just a few header files; there is no need
117  to link against any additional libraries.
118
119- Binaries are generally smaller by a factor of at least 2 compared to
120  equivalent bindings generated by Boost.Python. A recent pybind11
121  conversion of PyRosetta, an enormous Boost.Python binding project,
122  `reported <http://graylab.jhu.edu/RosettaCon2016/PyRosetta-4.pdf>`_
123  a binary size reduction of **5.4x** and compile time reduction by
124  **5.8x**.
125
126- Function signatures are precomputed at compile time (using
127  ``constexpr``), leading to smaller binaries.
128
129- With little extra effort, C++ types can be pickled and unpickled
130  similar to regular Python objects.
131
132Supported compilers
133-------------------
134
1351. Clang/LLVM 3.3 or newer (for Apple Xcode’s clang, this is 5.0.0 or
136   newer)
1372. GCC 4.8 or newer
1383. Microsoft Visual Studio 2015 Update 3 or newer
1394. Intel classic C++ compiler 18 or newer (ICC 20.2 tested in CI)
1405. Cygwin/GCC (previously tested on 2.5.1)
1416. NVCC (CUDA 11.0 tested in CI)
1427. NVIDIA PGI (20.9 tested in CI)
143
144About
145-----
146
147This project was created by `Wenzel
148Jakob <http://rgl.epfl.ch/people/wjakob>`_. Significant features and/or
149improvements to the code were contributed by Jonas Adler, Lori A. Burns,
150Sylvain Corlay, Eric Cousineau, Ralf Grosse-Kunstleve, Trent Houliston, Axel
151Huebl, @hulucc, Yannick Jadoul, Sergey Lyskov Johan Mabille, Tomasz Miąsko,
152Dean Moldovan, Ben Pritchard, Jason Rhinelander, Boris Schäling,  Pim
153Schellart, Henry Schreiner, Ivan Smirnov, Boris Staletic, and Patrick Stewart.
154
155We thank Google for a generous financial contribution to the continuous
156integration infrastructure used by this project.
157
158
159Contributing
160~~~~~~~~~~~~
161
162See the `contributing
163guide <https://github.com/pybind/pybind11/blob/master/.github/CONTRIBUTING.md>`_
164for information on building and contributing to pybind11.
165
166License
167~~~~~~~
168
169pybind11 is provided under a BSD-style license that can be found in the
170`LICENSE <https://github.com/pybind/pybind11/blob/master/LICENSE>`_
171file. By using, distributing, or contributing to this project, you agree
172to the terms and conditions of this license.
173
174.. |Latest Documentation Status| image:: https://readthedocs.org/projects/pybind11/badge?version=latest
175   :target: http://pybind11.readthedocs.org/en/latest
176.. |Stable Documentation Status| image:: https://img.shields.io/badge/docs-stable-blue.svg
177   :target: http://pybind11.readthedocs.org/en/stable
178.. |Gitter chat| image:: https://img.shields.io/gitter/room/gitterHQ/gitter.svg
179   :target: https://gitter.im/pybind/Lobby
180.. |CI| image:: https://github.com/pybind/pybind11/workflows/CI/badge.svg
181   :target: https://github.com/pybind/pybind11/actions
182.. |Build status| image:: https://ci.appveyor.com/api/projects/status/riaj54pn4h08xy40?svg=true
183   :target: https://ci.appveyor.com/project/wjakob/pybind11
184.. |PyPI package| image:: https://img.shields.io/pypi/v/pybind11.svg
185   :target: https://pypi.org/project/pybind11/
186.. |Conda-forge| image:: https://img.shields.io/conda/vn/conda-forge/pybind11.svg
187   :target: https://github.com/conda-forge/pybind11-feedstock
188.. |Repology| image:: https://repology.org/badge/latest-versions/python:pybind11.svg
189   :target: https://repology.org/project/python:pybind11/versions
190.. |Python Versions| image:: https://img.shields.io/pypi/pyversions/pybind11.svg
191   :target: https://pypi.org/project/pybind11/
192