Is it possible to change python source file while running an application and have this changes reflected immediately in the application?
Let say I have a foo.py file with a class Foo. In other module I’m calling functions on the Foo on user action. Now, I would like to change source of the Foo without restarting the application and on next user input see the results of the new code.
Would that be possible?
Yes, but it ill advised — code which changes itself (although necessary in some cases) is notoriously hard to debug. You’re looking for the
reloadmethod. The way that you would get it to work would be to modify the source file and then call reload on the module, then repeat.You do have the option of re-assigning and modifying the code within the application itself — something which does not risk race conditions and it will also allow for a more consistent and testable environment. For an example, I would recomment
Ignacio Vazquez-Abrams‘s answer.Have you tried to see what you could manage by merely changing a state (an external config file, database entry, etc)