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

Return to Answer

added 260 characters in body
Source Link

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 (tested on Python 3.8):

import importlib.util, traceback, sys, gc, pickle
# Example data
import pickle
something = pickle.loads(pickle.dumps([123]))
if __name__ == '__main__':
 try:
 mod_path = sys.argv[1]
 except IndexError:
 print('Usage: python3', sys.argv[0], 'PATH_TO_SCRIPT')
 exit(1)
 modules_before = list(sys.modules.keys())
while True:
 argv #= Changesys.argv[1:]
 according the file namewhile True:
 mod_name, mod_path MOD_NAME = '__main__', 'some_file.py'
 spec = importlib.util.spec_from_file_location(mod_nameMOD_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]modules[MOD_NAME] = mod
 sys.argv = argv
 try:
 spec.loader.exec_module(mod)
 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]
 gc.collect()
 # 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 🌝

UPD Add a possibility to accept script name with command line arguments

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 (tested on Python 3.8):

import importlib.util, traceback, sys, gc, 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 = '__main__', '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:
 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]
 gc.collect()
 # 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 🌝

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 (tested on Python 3.8):

import importlib.util, traceback, sys, gc
# Example data
import pickle
something = pickle.loads(pickle.dumps([123]))
if __name__ == '__main__':
 try:
 mod_path = sys.argv[1]
 except IndexError:
 print('Usage: python3', sys.argv[0], 'PATH_TO_SCRIPT')
 exit(1)
 modules_before = list(sys.modules.keys())
 argv = sys.argv[1:]
 while True:
 MOD_NAME = '__main__'
 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
 sys.argv = argv
 try:
 spec.loader.exec_module(mod)
 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]
 gc.collect()
 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 🌝

UPD Add a possibility to accept script name with command line arguments

perfection
Source Link

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 (tested on Python 3.8):

import importlib.util, traceback, sys, gc, 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''__main__', '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:
 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]
 gc.collect()
 # Press enter to restart
 print('Press enter to re-run, CTRL-C to exit')
 try:
 sys.stdin.readline()
 except KeyboardInterrupt:
 raise

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 🌝

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 (tested on Python 3.8):

import importlib.util, traceback, sys, gc, 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:
 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]
 gc.collect()
 # Press enter to restart
 print('Press enter to re-run, CTRL-C to exit')
 try:
 sys.stdin.readline()
 except KeyboardInterrupt:
 raise

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 🌝

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 (tested on Python 3.8):

import importlib.util, traceback, sys, gc, 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 = '__main__', '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:
 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]
 gc.collect()
 # 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 🌝

added 14 characters in body
Source Link

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 (tested on Python 3.8):

import importlib.util, traceback, sys, gc, 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]
 gc.collect()
 # Press enter to restart
 print('Press enter to re-run, CTRL-C to exit')
 try:
  sys.stdin.readline()
 except KeyboardInterrupt:
 raise

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 🌝

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 (tested on Python 3.8):

import importlib.util, traceback, sys, gc, 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]
 gc.collect()
 # 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 🌝

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 (tested on Python 3.8):

import importlib.util, traceback, sys, gc, 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:
 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]
 gc.collect()
 # Press enter to restart
 print('Press enter to re-run, CTRL-C to exit')
 try:
  sys.stdin.readline()
 except KeyboardInterrupt:
 raise

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 🌝

added 22 characters in body
Source Link
Loading
added 23 characters in body
Source Link
Loading
added 78 characters in body
Source Link
Loading
added 36 characters in body
Source Link
Loading
added 65 characters in body
Source Link
Loading
added 307 characters in body
Source Link
Loading
deleted 111 characters in body
Source Link
Loading
Source Link
Loading
lang-py

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