_module_timestamps = {}
_checking = False

def run_checker():
    global _checking
_checking = True
while _checking:
    for name, module in sys.modules.items():
        if hasattr(module, '__file__'):
            filename = module.__file__
if filename.endswith('.pyc'):
    filename = filename[:-1]
mtime = os.stat(filename).st_mtime
if name not in _module_timestamps:
    _module_timestamps[name] = mtime
elif mtime > _module_timestamps[name]:
    do_reload(name)
else:
    'module %r has no file attribute' % (name,)
time.sleep(1)

def do_reload(modname):
    print('I would reload now, because of %r' % (modname,))
check_thread = threading.Thread(target=run_checker)
check_thread.daemon = True
check_thread.start()
try:
    while 1:
        time.sleep(0.1)
except KeyboardInterrupt:
    print('\nexiting...')
How to monitor Python files for changes?