Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reload the currently running python script

In python ,There is a reload method to reload an imported module , but is there a method to reload the currently running script without restarting it, this would be very helpful in debugging a script and changing the code live while the script is running. In visual basic I remember a similar functionality was called "apply code changes", but I need a similar functionality as a function call like "refresh()" which will apply the code changes instantly.

This would work smoothly when an independent function in the script is modified and we need to immediately apply the code change without restarting the script.

Inshort will:

reload(self) 

work?

like image 836
stackit Avatar asked Apr 30 '15 06:04

stackit


1 Answers

reload(self) will not work, because reload() works on modules, not live instances of classes. What you want is some logic external to your main application, which checks whether it has to be reloaded. You have to think about what is needed to re-create your application state upon reload.

Some hints in this direction: Guido van Rossum wrote once this: xreload.py; it does a bit more than reload() You would need a loop, which checks for changes every x seconds and applies this.

Also have a look at livecoding which basically does this. EDIT: I mistook this project for something else (which I didn't find now), sorry.

perhaps this SO question will help you

like image 130
knitti Avatar answered Nov 08 '22 22:11

knitti