Let\'s face it, the whole business of reloading python code after changing it is a mess. I figured out awhile back that calling import
at the in
Ok, I'm not sure that qualifies as an answer without a change to the code, but... at least, that doesn't involve a change to module1
.
You can use some module wrapper class, that saves loaded modules before and after loading module1 and that provides a reload
method, something like that:
import sys
class Reloader(object):
def __init__(self, modulename):
before = sys.modules.keys()
__import__(modulename)
after = sys.modules.keys()
names = list(set(after) - set(before))
self._toreload = [sys.modules[name] for name in names]
def do_reload(self):
for i in self._toreload:
reload(i)
Then load module1 with:
reloader = Reloader('module1')
Atfer that you can modify module2 and reload it in interpreter with:
reloader.do_reload()