Christoph Meyer
Christoph Meyer

Reputation: 1

How do I clear my Python interpreter cache while executing with pybind11?

I want to embed a Python interpreter in my C++ project using the pybind11 embedded interpreter.

During runtime, Python modules from different locations can be loaded and deleted.

To achieve this, I used importlib to refresh my Python cache. However, the cache remains unchanged, and if I try to load a module with the same name but from a different location, the old module is still loaded.

Thanks for your help.

i tried this small example

#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
namespace py = pybind11;

void addModulePath(const std::string &module) {
    auto sys = py::module::import("sys");
    auto importlib = py::module::import("importlib");
    auto path = sys.attr("path");
    py::print(path);

    path.attr("append")(module);
    importlib.attr("invalidate_caches")();
    py::print(path);
}

void deleteModulePath(std::string& module) {
    auto sys = py::module::import("sys");
    auto importlib = py::module::import("importlib");

    auto path = sys.attr("path");
    py::print(path);
    path.attr("remove")(module);
    importlib.attr("invalidate_caches")();
    py::print(path);

}

PYBIND11_EMBEDDED_MODULE(changeModule, m) {
    m.def("add_module", &addModulePath, "add module path");
    m.def("delete_module", &deleteModulePath, "delete module path");
}

int main() {

    constexpr auto test1Path = "/tmp/test1";
    constexpr auto test2Path = "/tmp/test2";

    py::scoped_interpreter guard{};
    py::module_ module = py::module_::import("changeModule");

    module.attr("add_module")(test1Path);

    auto test = py::module_::import("test");

    test.attr("test")();
    module.attr("delete_module")(test1Path);

    module.attr("add_module")(test2Path);

    test = py::module_::import("test");
    test.attr("test")();
    return 0;

}

the folders test1 and test2 does both contain a file called "test.py" which prints its folder name.

I don't want to restart my interpreter.

Upvotes: 0

Views: 54

Answers (0)

Related Questions