iScrE4m iScrE4m - 1 year ago 69
Python Question

Reloading main script on if statement in Python

I have an IRC bot working on twisted. I want him to reload itself (disconnect, reopen his main .py file with all changes made while he was running and start again) through external input

The if statement would be something like:

if msg.startswith(".restart"):
if user in admin:
msg = "Restarting"
SendMsg(self, channel, msg)

The only answers I found were how to reload modules.

edit: Best answer to do kinda what I wanted to do is in the comments below accepted answer

Answer Source

A script reloading itself would not make sense. Where would the execution resume? The only possibility is to restart from the beginning, which would be the same as killing and starting the process again.

What you can do is put as much code as you can in a different module (but not the main loop) and call that module from the main script. And, when the main script sees the .reload command, reload the module.