Reboot the module, do you need to recompile the submodules?

Sorry, I'm sure they will ask a bunch, but I could not find it.

in myModule.py:

from myModule.subModule import myClass 

I am working on myClass and want to stay in my ipython session and test it. reload(myModule) does not recompile myClass.

How can i do this?

+4
source share
1 answer

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;).

+2
source

Source: https://habr.com/ru/post/1309479/


All Articles