At my work place there is a script (kind of automation system) that loads and runs our application tests from an XML file.
In the middle of the process the script calls __import__(testModule)
which loads the module from its file.
The problem starts when I tried adding a feature by dynamically adding functions to the testModule
at runtime.
As expected, the __import__
gets the old version of the module which doesn't have the methods I just added at runtime.
Is it possible to make the __import__
calls import the newer vers开发者_C百科ion of the class (which includes the methods I added)?
Please note that I prefer keeping the automation system untouched (even when it would help solving the problem faster).
Thanks
Tal.
You need to be aware that reloading a module won't magically replace old instances. Even if you do reload
, only new objects will use the new code!
The only way to replace code during runtime is to wrap everything in a proxy object! You can sometimes do this, ie for specific, self-contained modules, but in most cases it's simply not a reasonable approach.
Quick demonstration:
>>> import asd
>>> asd.s
'old'
>>> t = asd.s
>>> reload(asd) # I edited asd.py before
<module 'asd' from 'asd.py'>
>>> asd.s # new module content
'new'
>>> t # but this is still old!
'old'
Most applications that looks like it reloads code actually just restart!
reload(testmodule)
might work.
精彩评论