README.rst 7.6 KB

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