Skip to content

Commit 571e470

Browse files
[SofaPython3/Plugin] Restore the automatic reload of modules (#182)
1 parent 70fab1e commit 571e470

1 file changed

Lines changed: 6 additions & 13 deletions

File tree

Plugin/src/SofaPython3/PythonEnvironment.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,10 @@ SOFAPYTHON3_API py::module PythonEnvironment::importFromFile(const std::string&
131131
if (globals == nullptr)
132132
globals = &globs;
133133
py::eval<py::eval_statements>( // tell eval we're passing multiple statements
134-
"import imp\n"
135-
"new_module = imp.load_module(module_name, open(path), path, ('py', 'U', imp.PY_SOURCE))\n",
134+
"import importlib.util \n"
135+
"spec = importlib.util.spec_from_file_location(module_name, path) \n"
136+
"new_module = importlib.util.module_from_spec(spec) \n"
137+
"spec.loader.exec_module(new_module)",
136138
*globals,
137139
locals);
138140
py::module m = py::cast<py::module>(locals["new_module"]);
@@ -233,25 +235,16 @@ void PythonEnvironment::Init()
233235
// Lastly, we (try to) add modules from the root of SOFA
234236
addPythonModulePathsFromDirectory( Utils::getSofaPathPrefix() );
235237

236-
try
237-
{
238-
py::module::import("SofaRuntime");
239-
}
240-
catch (pybind11::error_already_set)
241-
{
242-
msg_error("SofaPython3") << "Could not import SofaRuntime module, initializing python3 for SOFA is not possible";
243-
return;
244-
}
245238
getStaticData()->m_sofamodule = py::module::import("Sofa");
246-
239+
PyRun_SimpleString("import SofaRuntime");
247240

248241
// python livecoding related
249242
PyRun_SimpleString("from Sofa.livecoding import onReimpAFile");
250243

251244
// general sofa-python stuff
252245

253246
// python modules are automatically reloaded at each scene loading
254-
//setAutomaticModuleReload( true );
247+
setAutomaticModuleReload( true );
255248

256249
// Initialize pluginLibraryPath by reading PluginManager's map
257250
std::map<std::string, Plugin>& map = PluginManager::getInstance().getPluginMap();

0 commit comments

Comments
 (0)