Reread Setting.py while the program is running.
-
Reread Setting.py while the program is running.
How can I reload a setting file which I read in by Import Setting at the beginning of the program without leaving the main.py or starting a reset?Problem: I have a setting file that I can update online. However, the new data is not taken over in the variables, but only when I reboot.
How can I do it without restarting.
I am using a While True loop in the main.py.Translated with www.DeepL.com/Translator
-
@robert-hh
That's how it works perfectly!
Thank you very much.import sys del sys.modules['settings'] time.sleep(1) import settings
-
@Wolli01 Yes. Obviously it is:
del sys.modules['settings']
since sys.modules is a dictionary. This del does not delete the old module, but only allows to reload it.
-
@robert-hh
import sys
del sys.modules('settings')
time.sleep(1)
import settingsFile "main.py", line 290, in <module>
SyntaxError: can't delete expression
-
@Wolli01
Preload:
from reload import *
then:
reload(Setting)Instead of this function, you can also simply use that sequence directly:
import sys del sys.modules['Setting'] import Setting
You have to take care, in which namespace you are calling it.
-
@robert-hh Wie rufe ich das auf?
-
@robert-hh Thank You
-
@Wolli01 I use this little function:
# # reload module: # def reload(mod): import sys mod_name = mod.__name__ del sys.modules[mod_name] return __import__(mod_name)
You would call it with:
reload(Setting)