You need to repeat the import after rebooting the submodule "topmost". For example, given:
$ mkdir myModule $ touch myModule/__init__.py $ cat >myModule/subModule.py class MyClass(object): kind='first'
and then
>>> from myModule.subModule import MyClass >>> MyClass.kind 'first'
and in another terminal
$ cat >myModule/subModule.py class MyClass(object): kind='second'
then ...
>>> import sys >>> reload(sys.modules['myModule.subModule']) <module 'myModule.subModule' from 'myModule/subModule.py'> >>> from myModule.subModule import MyClass >>> MyClass.kind 'second'
You need to go through sys.modules as you have no reference to the submodule and then you need to repeat from .
Life is much simpler if you agree with the wise advice that you always import a module, never load it from an INSIDE module, for example, a Python session (with changing the submodule before rebooting):
>>> from myModule import subModule as sm >>> sm.MyClass.kind 'first' >>> reload(sm) <module 'myModule.subModule' from 'myModule/subModule.py'> >>> sm.MyClass.kind 'second'
If you are used to using qualified names, for example, sm.MyClass , and not just barename MyClass , your life will be easier in many ways (restarting is easier - just one of them;).
source share