README.rst 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. **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 (2.7 or 3.5+, or PyPy) and the C++
  29. standard library. This compact implementation was possible thanks to
  30. some of the new C++11 language features (specifically: tuples, lambda
  31. functions and variadic templates). Since its creation, this library has
  32. grown beyond Boost.Python in many ways, leading to dramatically simpler
  33. binding code in many 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. Goodies
  60. -------
  61. In addition to the core functionality, pybind11 provides some extra
  62. goodies:
  63. - Python 2.7, 3.5+, and PyPy/PyPy3 7.3 are supported with an
  64. implementation-agnostic interface.
  65. - It is possible to bind C++11 lambda functions with captured
  66. variables. The lambda capture data is stored inside the resulting
  67. Python function object.
  68. - pybind11 uses C++11 move constructors and move assignment operators
  69. whenever possible to efficiently transfer custom data types.
  70. - It’s easy to expose the internal storage of custom data types through
  71. Pythons’ buffer protocols. This is handy e.g. for fast conversion
  72. between C++ matrix classes like Eigen and NumPy without expensive
  73. copy operations.
  74. - pybind11 can automatically vectorize functions so that they are
  75. transparently applied to all entries of one or more NumPy array
  76. arguments.
  77. - Python's slice-based access and assignment operations can be
  78. supported with just a few lines of code.
  79. - Everything is contained in just a few header files; there is no need
  80. to link against any additional libraries.
  81. - Binaries are generally smaller by a factor of at least 2 compared to
  82. equivalent bindings generated by Boost.Python. A recent pybind11
  83. conversion of PyRosetta, an enormous Boost.Python binding project,
  84. `reported <http://graylab.jhu.edu/RosettaCon2016/PyRosetta-4.pdf>`_
  85. a binary size reduction of **5.4x** and compile time reduction by
  86. **5.8x**.
  87. - Function signatures are precomputed at compile time (using
  88. ``constexpr``), leading to smaller binaries.
  89. - With little extra effort, C++ types can be pickled and unpickled
  90. similar to regular Python objects.
  91. Supported compilers
  92. -------------------
  93. 1. Clang/LLVM 3.3 or newer (for Apple Xcode’s clang, this is 5.0.0 or
  94. newer)
  95. 2. GCC 4.8 or newer
  96. 3. Microsoft Visual Studio 2015 Update 3 or newer
  97. 4. Intel classic C++ compiler 18 or newer (ICC 20.2 tested in CI)
  98. 5. Cygwin/GCC (previously tested on 2.5.1)
  99. 6. NVCC (CUDA 11.0 tested in CI)
  100. 7. NVIDIA PGI (20.9 tested in CI)
  101. About
  102. -----
  103. This project was created by `Wenzel
  104. Jakob <http://rgl.epfl.ch/people/wjakob>`_. Significant features and/or
  105. improvements to the code were contributed by Jonas Adler, Lori A. Burns,
  106. Sylvain Corlay, Eric Cousineau, Ralf Grosse-Kunstleve, Trent Houliston, Axel
  107. Huebl, @hulucc, Yannick Jadoul, Sergey Lyskov Johan Mabille, Tomasz Miąsko,
  108. Dean Moldovan, Ben Pritchard, Jason Rhinelander, Boris Schäling, Pim
  109. Schellart, Henry Schreiner, Ivan Smirnov, Boris Staletic, and Patrick Stewart.
  110. We thank Google for a generous financial contribution to the continuous
  111. integration infrastructure used by this project.
  112. Contributing
  113. ~~~~~~~~~~~~
  114. See the `contributing
  115. guide <https://github.com/pybind/pybind11/blob/master/.github/CONTRIBUTING.md>`_
  116. for information on building and contributing to pybind11.
  117. License
  118. ~~~~~~~
  119. pybind11 is provided under a BSD-style license that can be found in the
  120. `LICENSE <https://github.com/pybind/pybind11/blob/master/LICENSE>`_
  121. file. By using, distributing, or contributing to this project, you agree
  122. to the terms and conditions of this license.
  123. .. |Latest Documentation Status| image:: https://readthedocs.org/projects/pybind11/badge?version=latest
  124. :target: http://pybind11.readthedocs.org/en/latest
  125. .. |Stable Documentation Status| image:: https://img.shields.io/badge/docs-stable-blue.svg
  126. :target: http://pybind11.readthedocs.org/en/stable
  127. .. |Gitter chat| image:: https://img.shields.io/gitter/room/gitterHQ/gitter.svg
  128. :target: https://gitter.im/pybind/Lobby
  129. .. |CI| image:: https://github.com/pybind/pybind11/workflows/CI/badge.svg
  130. :target: https://github.com/pybind/pybind11/actions
  131. .. |Build status| image:: https://ci.appveyor.com/api/projects/status/riaj54pn4h08xy40?svg=true
  132. :target: https://ci.appveyor.com/project/wjakob/pybind11
  133. .. |PyPI package| image:: https://img.shields.io/pypi/v/pybind11.svg
  134. :target: https://pypi.org/project/pybind11/
  135. .. |Conda-forge| image:: https://img.shields.io/conda/vn/conda-forge/pybind11.svg
  136. :target: https://github.com/conda-forge/pybind11-feedstock
  137. .. |Repology| image:: https://repology.org/badge/latest-versions/python:pybind11.svg
  138. :target: https://repology.org/project/python:pybind11/versions
  139. .. |Python Versions| image:: https://img.shields.io/pypi/pyversions/pybind11.svg
  140. :target: https://pypi.org/project/pybind11/