blob: 6c2a2551096a456036cced3c1caf70ed89d3dc4a [file] [log] [blame]
Henry Schreiner81555ce2020-09-17 15:40:09 -04001.. figure:: https://github.com/pybind/pybind11/raw/master/docs/pybind11-logo.png
2 :alt: pybind11 logo
3
Henry Schreiner0b9acc42020-10-18 14:31:28 -04004**pybind11 Seamless operability between C++11 and Python**
Henry Schreiner81555ce2020-09-17 15:40:09 -04005
Henry Schreinerd753b762020-09-17 17:53:35 -04006|Latest Documentation Status| |Stable Documentation Status| |Gitter chat| |CI| |Build status|
Henry Schreiner81555ce2020-09-17 15:40:09 -04007
Henry Schreinerde78bdd2020-11-15 12:23:33 -05008|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
Henry Schreiner79b0e2c2020-12-22 08:50:45 -050014.. start
15
Henry Schreiner2a263e02020-10-13 18:19:05 -040016.. warning::
17
Yannick Jadoul91a69722020-12-09 00:08:19 +010018 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
Henry Schreiner2a263e02020-10-13 18:19:05 -040021 warned.**)
22
Yannick Jadoul91a69722020-12-09 00:08:19 +010023 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.
Henry Schreiner2a263e02020-10-13 18:19:05 -040028
Henry Schreiner79b0e2c2020-12-22 08:50:45 -050029
Henry Schreiner81555ce2020-09-17 15:40:09 -040030**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.Pythonand the reason for creating such a
39similar projectis 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 isnt 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
Henry Schreiner993495c2020-10-12 16:31:44 -040058`pybind11.readthedocs.io <https://pybind11.readthedocs.io/en/latest>`_.
Henry Schreiner81555ce2020-09-17 15:40:09 -040059A PDF version of the manual is available
Dariusz Suchojadbed90802020-10-18 12:51:36 +020060`here <https://pybind11.readthedocs.io/_/downloads/en/latest/pdf/>`_.
Henry Schreiner81555ce2020-09-17 15:40:09 -040061And the source code is always available at
62`github.com/pybind/pybind11 <https://github.com/pybind/pybind11>`_.
63
Henry Schreiner0b9acc42020-10-18 14:31:28 -040064
Henry Schreiner81555ce2020-09-17 15:40:09 -040065Core features
66-------------
67
Henry Schreiner0b9acc42020-10-18 14:31:28 -040068
Henry Schreiner81555ce2020-09-17 15:40:09 -040069pybind11 can map the following core C++ features to Python:
70
Henry Schreiner0b9acc42020-10-18 14:31:28 -040071- 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
Henry Schreiner81555ce2020-09-17 15:40:09 -040087
88Goodies
89-------
90
91In addition to the core functionality, pybind11 provides some extra
92goodies:
93
Henry Schreiner0b9acc42020-10-18 14:31:28 -040094- Python 2.7, 3.5+, and PyPy/PyPy3 7.3 are supported with an
95 implementation-agnostic interface.
Henry Schreiner81555ce2020-09-17 15:40:09 -040096
Henry Schreiner0b9acc42020-10-18 14:31:28 -040097- 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.
Henry Schreiner81555ce2020-09-17 15:40:09 -0400100
Henry Schreiner0b9acc42020-10-18 14:31:28 -0400101- pybind11 uses C++11 move constructors and move assignment operators
102 whenever possible to efficiently transfer custom data types.
Henry Schreiner81555ce2020-09-17 15:40:09 -0400103
Henry Schreiner0b9acc42020-10-18 14:31:28 -0400104- Its easy to expose the internal storage of custom data types through
105 Pythons buffer protocols. This is handy e.gfor fast conversion
106 between C++ matrix classes like Eigen and NumPy without expensive
107 copy operations.
Henry Schreiner81555ce2020-09-17 15:40:09 -0400108
Henry Schreiner0b9acc42020-10-18 14:31:28 -0400109- pybind11 can automatically vectorize functions so that they are
110 transparently applied to all entries of one or more NumPy array
111 arguments.
Henry Schreiner81555ce2020-09-17 15:40:09 -0400112
Henry Schreiner0b9acc42020-10-18 14:31:28 -0400113- Pythons slice-based access and assignment operations can be
114 supported with just a few lines of code.
Henry Schreiner81555ce2020-09-17 15:40:09 -0400115
Henry Schreiner0b9acc42020-10-18 14:31:28 -0400116- Everything is contained in just a few header files; there is no need
117 to link against any additional libraries.
Henry Schreiner81555ce2020-09-17 15:40:09 -0400118
Henry Schreiner0b9acc42020-10-18 14:31:28 -0400119- 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**.
Henry Schreiner81555ce2020-09-17 15:40:09 -0400125
Henry Schreiner0b9acc42020-10-18 14:31:28 -0400126- Function signatures are precomputed at compile time (using
127 ``constexpr``), leading to smaller binaries.
Henry Schreiner81555ce2020-09-17 15:40:09 -0400128
Henry Schreiner0b9acc42020-10-18 14:31:28 -0400129- With little extra effort, C++ types can be pickled and unpickled
130 similar to regular Python objects.
Henry Schreiner81555ce2020-09-17 15:40:09 -0400131
132Supported compilers
133-------------------
134
1351. Clang/LLVM 3.3 or newer (for Apple Xcodes clang, this is 5.0.0 or
136 newer)
1372. GCC 4.8 or newer
1383. Microsoft Visual Studio 2015 Update 3 or newer
Michael Kuron48534082021-01-18 01:53:07 +01001394. Intel classic C++ compiler 18 or newer (ICC 20.2 tested in CI)
Henry Schreiner499fcd52020-12-15 21:07:41 -05001405. Cygwin/GCC (previously tested on 2.5.1)
Michael Kuron48534082021-01-18 01:53:07 +01001416. NVCC (CUDA 11.0 tested in CI)
1427. NVIDIA PGI (20.9 tested in CI)
Henry Schreiner81555ce2020-09-17 15:40:09 -0400143
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,
Wenzel Jakob2bc62dc2020-10-20 16:56:02 +0200150Sylvain 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.
Henry Schreiner81555ce2020-09-17 15:40:09 -0400154
Wenzel Jakob7f9445a2020-10-20 21:14:40 +0200155We thank Google for a generous financial contribution to the continuous
156integration infrastructure used by this project.
157
158
Henry Schreiner81555ce2020-09-17 15:40:09 -0400159Contributing
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
Henry Schreinerd753b762020-09-17 17:53:35 -0400174.. |Latest Documentation Status| image:: https://readthedocs.org/projects/pybind11/badge?version=latest
Henry Schreiner8fa70e72020-09-17 21:18:15 -0400175 :target: http://pybind11.readthedocs.org/en/latest
Henry Schreiner79b0e2c2020-12-22 08:50:45 -0500176.. |Stable Documentation Status| image:: https://img.shields.io/badge/docs-stable-blue.svg
Henry Schreiner8fa70e72020-09-17 21:18:15 -0400177 :target: http://pybind11.readthedocs.org/en/stable
Henry Schreiner81555ce2020-09-17 15:40:09 -0400178.. |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
Henry Schreiner79b0e2c2020-12-22 08:50:45 -0500184.. |PyPI package| image:: https://img.shields.io/pypi/v/pybind11.svg
Henry Schreinerde78bdd2020-11-15 12:23:33 -0500185 :target: https://pypi.org/project/pybind11/
Henry Schreiner79b0e2c2020-12-22 08:50:45 -0500186.. |Conda-forge| image:: https://img.shields.io/conda/vn/conda-forge/pybind11.svg
Henry Schreinerde78bdd2020-11-15 12:23:33 -0500187 :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
Henry Schreiner79b0e2c2020-12-22 08:50:45 -0500190.. |Python Versions| image:: https://img.shields.io/pypi/pyversions/pybind11.svg
Henry Schreinerde78bdd2020-11-15 12:23:33 -0500191 :target: https://pypi.org/project/pybind11/