You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
8.1 KiB

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