25

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?

Miroslav Zadravec
  • 3,510
  • 5
  • 24
  • 35
  • 8
    What is your reason for wanting to do this? What is the use case? – jhocking Sep 27 '11 at 18:00
  • http://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module – jterrace Sep 27 '11 at 18:00
  • 2
    @jhocking: just testing something while being lazy to write proper tests – Miroslav Zadravec Sep 27 '11 at 18:04
  • 5
    @Miroslav, the lazy thing to do is to *write the tests*. Trying to do it some weird, crazy way will only serve to introduce bugs (silent and otherwise) which will make your testing harder or even wrong. – Mike Graham Sep 27 '11 at 20:13
  • 3
    @MikeGraham This is an excellent exercise to help learn how the language works. After one understands how it works, it stops being weird and crazy. – Evgeni Sergeev Aug 10 '16 at 03:07

2 Answers2

12

Yes, but it ill advised — code which changes itself (although necessary in some cases) is notoriously hard to debug. You're looking for the reload method. 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)

mbomb007
  • 3,788
  • 3
  • 39
  • 68
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • 2
    "Hopelessly ill advised" is more like it. Using `reload` is fundamentally buggy, and not the right tool for OP's actual problem. – Mike Graham Sep 27 '11 at 20:12
9

The following module can probably change whatever you need, not that it's a good idea to use, other than to muck around : )

Note that it will not change your source, which would probably be a disaster, especially if you make a coding error. The 'safer' option would be to play with byteplay

http://wiki.python.org/moin/ByteplayDoc

Ok, now let's play! Say we want to change the function, to print its arguments in reverse order. To do this, we will add a ROT_TWO opcode after the two arguments were loaded to the stack. See how simple it is:

>>> from byteplay import *
>>> from pprint import pprint
>>> def f(a, b):
...     print (a, b)
...
>>> f(3, 5)
(3, 5)
>>> c = Code.from_code(f.func_code)
>>> c.code[3:3] = [(ROT_TWO, None)]
>>> f.func_code = c.to_code()
>>> f(3, 5)
(5, 3)
>>> f(3, 5)
(5, 3)

If you are using a defined set of options but want to preserve the same function call, you can also do something like the following

class Foo(object):
     def fn(self):
         pass
     def op1(self):
         print "HELLO"
     #etc

>>> a = Foo()
>>> a.fn()
>>> a.fn = a.op1
>>> a.fn()
HELLO
pyInTheSky
  • 1,459
  • 1
  • 9
  • 24