Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

6 of 11
added 78 characters in body

As I understood:

  • something is needed to be loaded
  • it is needed to be loaded often, because file with code which uses this something is edited often
  • you don't want to wait until it will be loaded every time

Maybe such solution will be okay for you.

You can write script loader file in such way:

import importlib.util, traceback, sys, pickle
# Example data
something = pickle.loads(pickle.dumps([123]))
modules_before = list(sys.modules.keys())
while True:
 # Change according the file name
 mod_name, mod_path = 'some_file', 'some_file.py'
 spec = importlib.util.spec_from_file_location(mod_name, mod_path)
 mod = importlib.util.module_from_spec(spec)
 # Change to needed global name in the target module
 mod.something = something
 sys.modules[mod_name] = mod
 try:
 spec.loader.exec_module(mod)
 except KeyboardInterrupt:
 raise
 except:
 traceback.print_exc()
 del mod, spec
 modules_after = list(sys.modules.keys())
 for k in modules_after:
 if k not in modules_before:
 del sys.modules[k]
 # Press enter to restart
 print('Press enter to re-run, CTRL-C to exit')
 sys.stdin.readline()

Example of module:

# Change 1 to some different number when first script is running and press enter
something[0] += 1 
print(something)

Should work. And should reduce the reload time of pickle close to zero 🌝

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /