Skip to content

Commit 080fb5e

Browse files
committed
py.reload now also reloads user modules
Some packages like numpy, torch, etc will fail if loaded a second time. Attempts to minimize damage by only reloading modules outside of site-packages.
1 parent 7d9c3f4 commit 080fb5e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

Source/UnrealEnginePython/Private/UnrealEnginePython.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,21 @@ static void setup_importlib()
271271
PyRun_SimpleString(code);
272272
}
273273

274+
static void setup_misc()
275+
{
276+
char const* code = "import sys\n"
277+
"import os\n"
278+
"def _reset_modules():\n"
279+
" for module_name, module in list(sys.modules.items()):\n"
280+
" if module_name not in _initial_module_names:\n"
281+
" module_path = os.path.dirname(getattr(module, '__file__', ''))\n"
282+
" if not module_path or any(module_path.startswith(s) for s in sys.path if not s.endswith('Python')):\n"
283+
" continue\n"
284+
" del sys.modules[module_name]\n"
285+
"_initial_module_names = list(sys.modules.keys())\n";
286+
PyRun_SimpleString(code);
287+
}
288+
274289
namespace
275290
{
276291
static void consoleExecScript(const TArray<FString>& Args)
@@ -322,7 +337,7 @@ namespace
322337
return;
323338
}
324339

325-
UPythonBlueprintFunctionLibrary::ExecutePythonString(TEXT("_clear_file_finders()"));
340+
UPythonBlueprintFunctionLibrary::ExecutePythonString(TEXT("_clear_file_finders(); _reset_modules()"));
326341
}
327342
}
328343

@@ -465,6 +480,7 @@ void FUnrealEnginePythonModule::StartupModule()
465480
local_dict = main_dict;// PyDict_New();
466481

467482
setup_stdout_stderr();
483+
setup_misc();
468484

469485
if (PyImport_ImportModule("ue_site"))
470486
{

0 commit comments

Comments
 (0)