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.

253 lines
7.9 KiB

3 months ago
  1. Embedding the interpreter
  2. #########################
  3. While pybind11 is mainly focused on extending Python using C++, it's also
  4. possible to do the reverse: embed the Python interpreter into a C++ program.
  5. All of the other documentation pages still apply here, so refer to them for
  6. general pybind11 usage. This section will cover a few extra things required
  7. for embedding.
  8. Getting started
  9. ===============
  10. A basic executable with an embedded interpreter can be created with just a few
  11. lines of CMake and the ``pybind11::embed`` target, as shown below. For more
  12. information, see :doc:`/compiling`.
  13. .. code-block:: cmake
  14. cmake_minimum_required(VERSION 3.0)
  15. project(example)
  16. find_package(pybind11 REQUIRED) # or `add_subdirectory(pybind11)`
  17. add_executable(example main.cpp)
  18. target_link_libraries(example PRIVATE pybind11::embed)
  19. The essential structure of the ``main.cpp`` file looks like this:
  20. .. code-block:: cpp
  21. #include <pybind11/embed.h> // everything needed for embedding
  22. namespace py = pybind11;
  23. int main() {
  24. py::scoped_interpreter guard{}; // start the interpreter and keep it alive
  25. py::print("Hello, World!"); // use the Python API
  26. }
  27. The interpreter must be initialized before using any Python API, which includes
  28. all the functions and classes in pybind11. The RAII guard class `scoped_interpreter`
  29. takes care of the interpreter lifetime. After the guard is destroyed, the interpreter
  30. shuts down and clears its memory. No Python functions can be called after this.
  31. Executing Python code
  32. =====================
  33. There are a few different ways to run Python code. One option is to use `eval`,
  34. `exec` or `eval_file`, as explained in :ref:`eval`. Here is a quick example in
  35. the context of an executable with an embedded interpreter:
  36. .. code-block:: cpp
  37. #include <pybind11/embed.h>
  38. namespace py = pybind11;
  39. int main() {
  40. py::scoped_interpreter guard{};
  41. py::exec(R"(
  42. kwargs = dict(name="World", number=42)
  43. message = "Hello, {name}! The answer is {number}".format(**kwargs)
  44. print(message)
  45. )");
  46. }
  47. Alternatively, similar results can be achieved using pybind11's API (see
  48. :doc:`/advanced/pycpp/index` for more details).
  49. .. code-block:: cpp
  50. #include <pybind11/embed.h>
  51. namespace py = pybind11;
  52. using namespace py::literals;
  53. int main() {
  54. py::scoped_interpreter guard{};
  55. auto kwargs = py::dict("name"_a="World", "number"_a=42);
  56. auto message = "Hello, {name}! The answer is {number}"_s.format(**kwargs);
  57. py::print(message);
  58. }
  59. The two approaches can also be combined:
  60. .. code-block:: cpp
  61. #include <pybind11/embed.h>
  62. #include <iostream>
  63. namespace py = pybind11;
  64. using namespace py::literals;
  65. int main() {
  66. py::scoped_interpreter guard{};
  67. auto locals = py::dict("name"_a="World", "number"_a=42);
  68. py::exec(R"(
  69. message = "Hello, {name}! The answer is {number}".format(**locals())
  70. )", py::globals(), locals);
  71. auto message = locals["message"].cast<std::string>();
  72. std::cout << message;
  73. }
  74. Importing modules
  75. =================
  76. Python modules can be imported using `module::import()`:
  77. .. code-block:: cpp
  78. py::module sys = py::module::import("sys");
  79. py::print(sys.attr("path"));
  80. For convenience, the current working directory is included in ``sys.path`` when
  81. embedding the interpreter. This makes it easy to import local Python files:
  82. .. code-block:: python
  83. """calc.py located in the working directory"""
  84. def add(i, j):
  85. return i + j
  86. .. code-block:: cpp
  87. py::module calc = py::module::import("calc");
  88. py::object result = calc.attr("add")(1, 2);
  89. int n = result.cast<int>();
  90. assert(n == 3);
  91. Adding embedded modules
  92. =======================
  93. Embedded binary modules can be added using the `PYBIND11_EMBEDDED_MODULE` macro.
  94. Note that the definition must be placed at global scope. They can be imported
  95. like any other module.
  96. .. code-block:: cpp
  97. #include <pybind11/embed.h>
  98. namespace py = pybind11;
  99. PYBIND11_EMBEDDED_MODULE(fast_calc, m) {
  100. // `m` is a `py::module` which is used to bind functions and classes
  101. m.def("add", [](int i, int j) {
  102. return i + j;
  103. });
  104. }
  105. int main() {
  106. py::scoped_interpreter guard{};
  107. auto fast_calc = py::module::import("fast_calc");
  108. auto result = fast_calc.attr("add")(1, 2).cast<int>();
  109. assert(result == 3);
  110. }
  111. Unlike extension modules where only a single binary module can be created, on
  112. the embedded side an unlimited number of modules can be added using multiple
  113. `PYBIND11_EMBEDDED_MODULE` definitions (as long as they have unique names).
  114. These modules are added to Python's list of builtins, so they can also be
  115. imported in pure Python files loaded by the interpreter. Everything interacts
  116. naturally:
  117. .. code-block:: python
  118. """py_module.py located in the working directory"""
  119. import cpp_module
  120. a = cpp_module.a
  121. b = a + 1
  122. .. code-block:: cpp
  123. #include <pybind11/embed.h>
  124. namespace py = pybind11;
  125. PYBIND11_EMBEDDED_MODULE(cpp_module, m) {
  126. m.attr("a") = 1
  127. }
  128. int main() {
  129. py::scoped_interpreter guard{};
  130. auto py_module = py::module::import("py_module");
  131. auto locals = py::dict("fmt"_a="{} + {} = {}", **py_module.attr("__dict__"));
  132. assert(locals["a"].cast<int>() == 1);
  133. assert(locals["b"].cast<int>() == 2);
  134. py::exec(R"(
  135. c = a + b
  136. message = fmt.format(a, b, c)
  137. )", py::globals(), locals);
  138. assert(locals["c"].cast<int>() == 3);
  139. assert(locals["message"].cast<std::string>() == "1 + 2 = 3");
  140. }
  141. Interpreter lifetime
  142. ====================
  143. The Python interpreter shuts down when `scoped_interpreter` is destroyed. After
  144. this, creating a new instance will restart the interpreter. Alternatively, the
  145. `initialize_interpreter` / `finalize_interpreter` pair of functions can be used
  146. to directly set the state at any time.
  147. Modules created with pybind11 can be safely re-initialized after the interpreter
  148. has been restarted. However, this may not apply to third-party extension modules.
  149. The issue is that Python itself cannot completely unload extension modules and
  150. there are several caveats with regard to interpreter restarting. In short, not
  151. all memory may be freed, either due to Python reference cycles or user-created
  152. global data. All the details can be found in the CPython documentation.
  153. .. warning::
  154. Creating two concurrent `scoped_interpreter` guards is a fatal error. So is
  155. calling `initialize_interpreter` for a second time after the interpreter
  156. has already been initialized.
  157. Do not use the raw CPython API functions ``Py_Initialize`` and
  158. ``Py_Finalize`` as these do not properly handle the lifetime of
  159. pybind11's internal data.
  160. Sub-interpreter support
  161. =======================
  162. Creating multiple copies of `scoped_interpreter` is not possible because it
  163. represents the main Python interpreter. Sub-interpreters are something different
  164. and they do permit the existence of multiple interpreters. This is an advanced
  165. feature of the CPython API and should be handled with care. pybind11 does not
  166. currently offer a C++ interface for sub-interpreters, so refer to the CPython
  167. documentation for all the details regarding this feature.
  168. We'll just mention a couple of caveats the sub-interpreters support in pybind11:
  169. 1. Sub-interpreters will not receive independent copies of embedded modules.
  170. Instead, these are shared and modifications in one interpreter may be
  171. reflected in another.
  172. 2. Managing multiple threads, multiple interpreters and the GIL can be
  173. challenging and there are several caveats here, even within the pure
  174. CPython API (please refer to the Python docs for details). As for
  175. pybind11, keep in mind that `gil_scoped_release` and `gil_scoped_acquire`
  176. do not take sub-interpreters into account.