@@ -36,8 +36,10 @@ namespace mo2::python {
3636 PythonRunner () = default ;
3737 ~PythonRunner () = default ;
3838
39- QList<QList<QObject*>> load (std::filesystem::path const & pythonModule) override ;
40- void unload (std::filesystem::path const & pythonModule) override ;
39+ QList<QList<QObject*>> load (std::string_view moduleName,
40+ std::filesystem::path const & modulePath) override ;
41+ void unload (std::string_view moduleName,
42+ std::filesystem::path const & modulePath) override ;
4143
4244 bool initialize (std::vector<std::filesystem::path> const & pythonPaths) override ;
4345 void addDllSearchPath (std::filesystem::path const & dllPath) override ;
@@ -150,7 +152,8 @@ namespace mo2::python {
150152 py::module_::import (" os" ).attr (" add_dll_directory" )(absolute (dllPath));
151153 }
152154
153- QList<QList<QObject*>> PythonRunner::load (const std::filesystem::path& pythonModule)
155+ QList<QList<QObject*>> PythonRunner::load (std::string_view name,
156+ const std::filesystem::path& pythonModule)
154157 {
155158 py::gil_scoped_acquire lock;
156159
@@ -160,24 +163,19 @@ namespace mo2::python {
160163 auto sys = py::module_::import (" sys" );
161164 auto importlib_util = py::module_::import (" importlib.util" );
162165
163- // check the file type
164- const auto moduleName =
165- pythonModule.filename () == " __init__.py"
166- ? pythonModule.parent_path ().filename ().u8string ()
167- : pythonModule.filename ().u8string ();
168-
169166 // check if the module is already loaded
170167 py::dict modules = sys.attr (" modules" );
171168 py::module_ pymodule;
172- if (modules.contains (moduleName )) {
173- pymodule = modules[py::str (moduleName )];
169+ if (modules.contains (name )) {
170+ pymodule = modules[py::str (name )];
174171 pymodule.reload ();
175172 }
176173 else {
177174 // load the module
178- auto spec = importlib_util.attr (" find_spec" )(moduleName, pythonModule);
179- pymodule = importlib_util.attr (" module_from_spec" )(spec);
180- sys.attr (" modules" )[py::str (moduleName)] = pymodule;
175+ auto spec =
176+ importlib_util.attr (" spec_from_file_location" )(name, pythonModule);
177+ pymodule = importlib_util.attr (" module_from_spec" )(spec);
178+ sys.attr (" modules" )[py::str (name)] = pymodule;
181179 spec.attr (" loader" ).attr (" exec_module" )(pymodule);
182180 }
183181
@@ -243,12 +241,13 @@ namespace mo2::python {
243241 }
244242 }
245243
246- void PythonRunner::unload (const std::filesystem::path& pythonModule)
244+ void PythonRunner::unload (std::string_view moduleName,
245+ std::filesystem::path const & modulePath)
247246 {
248247 py::gil_scoped_acquire lock;
249248
250249 // At this point, the identifier is the full path to the module.
251- QDir folder (pythonModule );
250+ QDir folder (modulePath );
252251
253252 // we want to "unload" (remove from sys.modules) modules that come
254253 // from this plugin (whose __path__ points under this module,
0 commit comments