Skip to content

Commit 4adf78f

Browse files
committed
FIX: modules can be refreshed and imported now
1 parent 4fa9d32 commit 4adf78f

File tree

1 file changed

+13
-0
lines changed
  • GH/PyGH/components/scriptsynccpy

1 file changed

+13
-0
lines changed

GH/PyGH/components/scriptsynccpy/code.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import queue
2020
import json
2121

22+
import importlib
23+
import sys
24+
2225

2326
class GHThread(threading.Thread, metaclass=abc.ABCMeta):
2427
"""
@@ -275,6 +278,13 @@ def init_script_path(self, btn : bool = False):
275278
if not os.path.exists(self.path):
276279
raise Exception("script-sync::File does not exist")
277280

281+
def reload_all_modules(directory):
282+
for filename in os.listdir(directory):
283+
if filename.endswith('.py') and filename != '__init__.py':
284+
module_name = filename[:-3] # remove '.py' from filename
285+
if module_name in sys.modules:
286+
importlib.reload(sys.modules[module_name])
287+
278288
def safe_exec(self, path, globals, locals):
279289
"""
280290
Execute Python3 code safely. It redirects the output of the code
@@ -290,6 +300,9 @@ def safe_exec(self, path, globals, locals):
290300
# add the path of the file to use the modules
291301
path_dir = os.path.dirname(path)
292302
sys.path.insert(0, path_dir)
303+
reload_all_modules(path_dir)
304+
305+
self.reload_all_modules(path_dir)
293306

294307
# parse the code
295308
code = compile(f.read(), path, 'exec')

0 commit comments

Comments
 (0)