Blame external/pybind11/docs/advanced/embedding.rst

Packit 534379
.. _embedding:
Packit 534379
Packit 534379
Embedding the interpreter
Packit 534379
#########################
Packit 534379
Packit 534379
While pybind11 is mainly focused on extending Python using C++, it's also
Packit 534379
possible to do the reverse: embed the Python interpreter into a C++ program.
Packit 534379
All of the other documentation pages still apply here, so refer to them for
Packit 534379
general pybind11 usage. This section will cover a few extra things required
Packit 534379
for embedding.
Packit 534379
Packit 534379
Getting started
Packit 534379
===============
Packit 534379
Packit 534379
A basic executable with an embedded interpreter can be created with just a few
Packit 534379
lines of CMake and the ``pybind11::embed`` target, as shown below. For more
Packit 534379
information, see :doc:`/compiling`.
Packit 534379
Packit 534379
.. code-block:: cmake
Packit 534379
Packit 534379
    cmake_minimum_required(VERSION 3.0)
Packit 534379
    project(example)
Packit 534379
Packit 534379
    find_package(pybind11 REQUIRED)  # or `add_subdirectory(pybind11)`
Packit 534379
Packit 534379
    add_executable(example main.cpp)
Packit 534379
    target_link_libraries(example PRIVATE pybind11::embed)
Packit 534379
Packit 534379
The essential structure of the ``main.cpp`` file looks like this:
Packit 534379
Packit 534379
.. code-block:: cpp
Packit 534379
Packit 534379
    #include <pybind11/embed.h> // everything needed for embedding
Packit 534379
    namespace py = pybind11;
Packit 534379
Packit 534379
    int main() {
Packit 534379
        py::scoped_interpreter guard{}; // start the interpreter and keep it alive
Packit 534379
Packit 534379
        py::print("Hello, World!"); // use the Python API
Packit 534379
    }
Packit 534379
Packit 534379
The interpreter must be initialized before using any Python API, which includes
Packit 534379
all the functions and classes in pybind11. The RAII guard class `scoped_interpreter`
Packit 534379
takes care of the interpreter lifetime. After the guard is destroyed, the interpreter
Packit 534379
shuts down and clears its memory. No Python functions can be called after this.
Packit 534379
Packit 534379
Executing Python code
Packit 534379
=====================
Packit 534379
Packit 534379
There are a few different ways to run Python code. One option is to use `eval`,
Packit 534379
`exec` or `eval_file`, as explained in :ref:`eval`. Here is a quick example in
Packit 534379
the context of an executable with an embedded interpreter:
Packit 534379
Packit 534379
.. code-block:: cpp
Packit 534379
Packit 534379
    #include <pybind11/embed.h>
Packit 534379
    namespace py = pybind11;
Packit 534379
Packit 534379
    int main() {
Packit 534379
        py::scoped_interpreter guard{};
Packit 534379
Packit 534379
        py::exec(R"(
Packit 534379
            kwargs = dict(name="World", number=42)
Packit 534379
            message = "Hello, {name}! The answer is {number}".format(**kwargs)
Packit 534379
            print(message)
Packit 534379
        )");
Packit 534379
    }
Packit 534379
Packit 534379
Alternatively, similar results can be achieved using pybind11's API (see
Packit 534379
:doc:`/advanced/pycpp/index` for more details).
Packit 534379
Packit 534379
.. code-block:: cpp
Packit 534379
Packit 534379
    #include <pybind11/embed.h>
Packit 534379
    namespace py = pybind11;
Packit 534379
    using namespace py::literals;
Packit 534379
Packit 534379
    int main() {
Packit 534379
        py::scoped_interpreter guard{};
Packit 534379
Packit 534379
        auto kwargs = py::dict("name"_a="World", "number"_a=42);
Packit 534379
        auto message = "Hello, {name}! The answer is {number}"_s.format(**kwargs);
Packit 534379
        py::print(message);
Packit 534379
    }
Packit 534379
Packit 534379
The two approaches can also be combined:
Packit 534379
Packit 534379
.. code-block:: cpp
Packit 534379
Packit 534379
    #include <pybind11/embed.h>
Packit 534379
    #include <iostream>
Packit 534379
Packit 534379
    namespace py = pybind11;
Packit 534379
    using namespace py::literals;
Packit 534379
Packit 534379
    int main() {
Packit 534379
        py::scoped_interpreter guard{};
Packit 534379
Packit 534379
        auto locals = py::dict("name"_a="World", "number"_a=42);
Packit 534379
        py::exec(R"(
Packit 534379
            message = "Hello, {name}! The answer is {number}".format(**locals())
Packit 534379
        )", py::globals(), locals);
Packit 534379
Packit 534379
        auto message = locals["message"].cast<std::string>();
Packit 534379
        std::cout << message;
Packit 534379
    }
Packit 534379
Packit 534379
Importing modules
Packit 534379
=================
Packit 534379
Packit 534379
Python modules can be imported using `module::import()`:
Packit 534379
Packit 534379
.. code-block:: cpp
Packit 534379
Packit 534379
    py::module sys = py::module::import("sys");
Packit 534379
    py::print(sys.attr("path"));
Packit 534379
Packit 534379
For convenience, the current working directory is included in ``sys.path`` when
Packit 534379
embedding the interpreter. This makes it easy to import local Python files:
Packit 534379
Packit 534379
.. code-block:: python
Packit 534379
Packit 534379
    """calc.py located in the working directory"""
Packit 534379
Packit 534379
    def add(i, j):
Packit 534379
        return i + j
Packit 534379
Packit 534379
Packit 534379
.. code-block:: cpp
Packit 534379
Packit 534379
    py::module calc = py::module::import("calc");
Packit 534379
    py::object result = calc.attr("add")(1, 2);
Packit 534379
    int n = result.cast<int>();
Packit 534379
    assert(n == 3);
Packit 534379
Packit 534379
Modules can be reloaded using `module::reload()` if the source is modified e.g.
Packit 534379
by an external process. This can be useful in scenarios where the application
Packit 534379
imports a user defined data processing script which needs to be updated after
Packit 534379
changes by the user. Note that this function does not reload modules recursively.
Packit 534379
Packit 534379
.. _embedding_modules:
Packit 534379
Packit 534379
Adding embedded modules
Packit 534379
=======================
Packit 534379
Packit 534379
Embedded binary modules can be added using the `PYBIND11_EMBEDDED_MODULE` macro.
Packit 534379
Note that the definition must be placed at global scope. They can be imported
Packit 534379
like any other module.
Packit 534379
Packit 534379
.. code-block:: cpp
Packit 534379
Packit 534379
    #include <pybind11/embed.h>
Packit 534379
    namespace py = pybind11;
Packit 534379
Packit 534379
    PYBIND11_EMBEDDED_MODULE(fast_calc, m) {
Packit 534379
        // `m` is a `py::module` which is used to bind functions and classes
Packit 534379
        m.def("add", [](int i, int j) {
Packit 534379
            return i + j;
Packit 534379
        });
Packit 534379
    }
Packit 534379
Packit 534379
    int main() {
Packit 534379
        py::scoped_interpreter guard{};
Packit 534379
Packit 534379
        auto fast_calc = py::module::import("fast_calc");
Packit 534379
        auto result = fast_calc.attr("add")(1, 2).cast<int>();
Packit 534379
        assert(result == 3);
Packit 534379
    }
Packit 534379
Packit 534379
Unlike extension modules where only a single binary module can be created, on
Packit 534379
the embedded side an unlimited number of modules can be added using multiple
Packit 534379
`PYBIND11_EMBEDDED_MODULE` definitions (as long as they have unique names).
Packit 534379
Packit 534379
These modules are added to Python's list of builtins, so they can also be
Packit 534379
imported in pure Python files loaded by the interpreter. Everything interacts
Packit 534379
naturally:
Packit 534379
Packit 534379
.. code-block:: python
Packit 534379
Packit 534379
    """py_module.py located in the working directory"""
Packit 534379
    import cpp_module
Packit 534379
Packit 534379
    a = cpp_module.a
Packit 534379
    b = a + 1
Packit 534379
Packit 534379
Packit 534379
.. code-block:: cpp
Packit 534379
Packit 534379
    #include <pybind11/embed.h>
Packit 534379
    namespace py = pybind11;
Packit 534379
Packit 534379
    PYBIND11_EMBEDDED_MODULE(cpp_module, m) {
Packit 534379
        m.attr("a") = 1;
Packit 534379
    }
Packit 534379
Packit 534379
    int main() {
Packit 534379
        py::scoped_interpreter guard{};
Packit 534379
Packit 534379
        auto py_module = py::module::import("py_module");
Packit 534379
Packit 534379
        auto locals = py::dict("fmt"_a="{} + {} = {}", **py_module.attr("__dict__"));
Packit 534379
        assert(locals["a"].cast<int>() == 1);
Packit 534379
        assert(locals["b"].cast<int>() == 2);
Packit 534379
Packit 534379
        py::exec(R"(
Packit 534379
            c = a + b
Packit 534379
            message = fmt.format(a, b, c)
Packit 534379
        )", py::globals(), locals);
Packit 534379
Packit 534379
        assert(locals["c"].cast<int>() == 3);
Packit 534379
        assert(locals["message"].cast<std::string>() == "1 + 2 = 3");
Packit 534379
    }
Packit 534379
Packit 534379
Packit 534379
Interpreter lifetime
Packit 534379
====================
Packit 534379
Packit 534379
The Python interpreter shuts down when `scoped_interpreter` is destroyed. After
Packit 534379
this, creating a new instance will restart the interpreter. Alternatively, the
Packit 534379
`initialize_interpreter` / `finalize_interpreter` pair of functions can be used
Packit 534379
to directly set the state at any time.
Packit 534379
Packit 534379
Modules created with pybind11 can be safely re-initialized after the interpreter
Packit 534379
has been restarted. However, this may not apply to third-party extension modules.
Packit 534379
The issue is that Python itself cannot completely unload extension modules and
Packit 534379
there are several caveats with regard to interpreter restarting. In short, not
Packit 534379
all memory may be freed, either due to Python reference cycles or user-created
Packit 534379
global data. All the details can be found in the CPython documentation.
Packit 534379
Packit 534379
.. warning::
Packit 534379
Packit 534379
    Creating two concurrent `scoped_interpreter` guards is a fatal error. So is
Packit 534379
    calling `initialize_interpreter` for a second time after the interpreter
Packit 534379
    has already been initialized.
Packit 534379
Packit 534379
    Do not use the raw CPython API functions ``Py_Initialize`` and
Packit 534379
    ``Py_Finalize`` as these do not properly handle the lifetime of
Packit 534379
    pybind11's internal data.
Packit 534379
Packit 534379
Packit 534379
Sub-interpreter support
Packit 534379
=======================
Packit 534379
Packit 534379
Creating multiple copies of `scoped_interpreter` is not possible because it
Packit 534379
represents the main Python interpreter. Sub-interpreters are something different
Packit 534379
and they do permit the existence of multiple interpreters. This is an advanced
Packit 534379
feature of the CPython API and should be handled with care. pybind11 does not
Packit 534379
currently offer a C++ interface for sub-interpreters, so refer to the CPython
Packit 534379
documentation for all the details regarding this feature.
Packit 534379
Packit 534379
We'll just mention a couple of caveats the sub-interpreters support in pybind11:
Packit 534379
Packit 534379
 1. Sub-interpreters will not receive independent copies of embedded modules.
Packit 534379
    Instead, these are shared and modifications in one interpreter may be
Packit 534379
    reflected in another.
Packit 534379
Packit 534379
 2. Managing multiple threads, multiple interpreters and the GIL can be
Packit 534379
    challenging and there are several caveats here, even within the pure
Packit 534379
    CPython API (please refer to the Python docs for details). As for
Packit 534379
    pybind11, keep in mind that `gil_scoped_release` and `gil_scoped_acquire`
Packit 534379
    do not take sub-interpreters into account.