README.rst 7.9 KB

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