TLDR: I want to know in general terms, what can cause a module to reload and reset back to original state without me explicitly trying to get it to.
I have come to the conclusion that my main module is reloading and in the process all variables are re-initialized to their initial state.
I cannot produce a minimal reproducible example. After googling I cannot find anything else on the subject except that python3.x should not reload modules unless explicitly told to.
Using pygame 1.9.6.
I want to ask what usually causes this behavior. Example of my main module with my added debugging prints:
import pygame
from sys import exit
class Engine:
def __init__(self):
self.init = False
self.running = False
def start(self):
print("engine start called")
print("self.init state:", self.init)
from graphics import render
render.init()
from initializer import initializer
initializer.init_sprites()
if not self.init:
self.init = True
self.running = True
print("new self.init state:", self.init)
self.main_loop()
def update_input(self):
pass
def update_events(self):
pass
def update_graphics(self):
pass
def self.main_loop() # it's at the very end of the real Engine() aswell
while True:
self.update_input()
self.update_events()
self.update_graphics()
pygame.quit()
exit()
engine = Engine()
print("above engine start reached")
app_init = False # I added this purely for debugging reasons
if not app_init:
app_init = True
engine.start()
The app isn't crashing or throwing exceptions, because all other modules keep running as they should. For example, sprites retain their states and keep moving about as they should. Camera is still scrollable et c.
However, console gives:
above engine.start() reached
engine start called
self.init state: False
render.init called
new self.init state: True
#stuff happening in background in other modules, no methods changing self.running or explicitly reloading main module. However imports of engine object do happen
above engine.start() reached
engine start called
self.init state: False
render.init called
new self.init state: True
BUT:
1) main_loop() is never returned to
2) The prints proves that the end of the main module is reached somehow even though the While loop should be active, and the app should exit() if it moves past the while loop in main_loop()
3) The self.init and global app_init state is reset to hard coded value and engine.start() is called like it was the first time the module was initialized
Is this an importing issue?
Update:
Not solved. But I have removed all side-effects by preventing all other modules from being re-initialized. However, this seems like a dirty hack and I am afraid that the reloading of my main module will lead to consequences sooner or later.