From 4adf78f774ecc11305eb858e77ffe8a8655aa844 Mon Sep 17 00:00:00 2001 From: Andrea Settimi Date: Tue, 13 Feb 2024 09:35:13 +0100 Subject: [PATCH] FIX: modules can be refreshed and imported now --- GH/PyGH/components/scriptsynccpy/code.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/GH/PyGH/components/scriptsynccpy/code.py b/GH/PyGH/components/scriptsynccpy/code.py index 186047c..371bf0d 100644 --- a/GH/PyGH/components/scriptsynccpy/code.py +++ b/GH/PyGH/components/scriptsynccpy/code.py @@ -19,6 +19,9 @@ import queue import json +import importlib +import sys + class GHThread(threading.Thread, metaclass=abc.ABCMeta): """ @@ -275,6 +278,13 @@ def init_script_path(self, btn : bool = False): if not os.path.exists(self.path): raise Exception("script-sync::File does not exist") + def reload_all_modules(directory): + for filename in os.listdir(directory): + if filename.endswith('.py') and filename != '__init__.py': + module_name = filename[:-3] # remove '.py' from filename + if module_name in sys.modules: + importlib.reload(sys.modules[module_name]) + def safe_exec(self, path, globals, locals): """ Execute Python3 code safely. It redirects the output of the code @@ -290,6 +300,9 @@ def safe_exec(self, path, globals, locals): # add the path of the file to use the modules path_dir = os.path.dirname(path) sys.path.insert(0, path_dir) + reload_all_modules(path_dir) + + self.reload_all_modules(path_dir) # parse the code code = compile(f.read(), path, 'exec')