How to reload python module that is build with c++ source code using pybind11

Viewed 556

I am using the first step example in pybind11's documentation

#include <pybind11/pybind11.h>
int add(int i, int j)
{
    return i + j;
}
PYBIND11_MODULE(example, m)
{
    m.doc() = "pybind11 example plugin"; // optional module docstring
    m.def("add", &add, "A function which adds two numbers");
}

everything works fine, i can use it in python shell:

import  example
example.add(2, 3) #returns 5

Now i made a simple change to use float instead of int for input for add(), everything compiles. and i want to reload the module example so i can test the new float based add(). However, i can not figure out a way to reload the example module. importlib.reload does not work, %autorelaod 2 in IPython does not work either. both approached tested to work with pure python based modules, but not the c++ and pybind11 based modules.

Did I miss anything here? or it ought to be like this?

UPDATE: seems it is a known issue related to How to Reload a Python3 C extension module?

Python's import mechanism will never dlclose() a shared library. Once loaded, the library will stay until the process terminates.

pybind11 module and ctypes module seems to share the same traits here regarding how the module is loaded/imported. Also quote from https://github.com/pybind/pybind11/issues/2511:

The way C extensions are loaded by Python does not allow them to be reloaded (in contract to Python modules, where the Python code can just be reloaded and doesn't refer to a dynamically loaded library)

I now just wonder if there is a method to wrap this up in a more convenient way for reloading the module. E.g., spawn a subprocess for a new python shell that copies all C extensions related variable/module, and substitute the original one.

0 Answers
Related