>>> from foo import bar
successful to be imported
>>> from foo import bar
>>>
>>> reload(bar)
successful to be imported
<module 'foo.bar' from 'foo/bar.pyc'>
如果你使用的 python3 那方法就多了,详细请看下面 & Q! Z4 V" C5 g# ^$ Q9 J7 e重载模块方法二 4 F1 k* L7 X. J* p 如果你使用 Python3.0 -> 3.3,那么可以使用 imp.reload 方法# h# B; E7 M+ ? M$ S" D2 x: N# O. _/ f
>>> from foo import bar
successful to be imported
>>> from foo import bar
>>>
>>> import imp
>>> imp.reload(bar)
successful to be imported
<module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'>
但是这个方法在 Python 3.4+,就不推荐使用了 s b1 v5 @: ]) _5 T6 O4 z
>>> from foo import bar
successful to be imported
>>> from foo import bar
>>>
>>> import importlib
>>> importlib.reload(bar)
successful to be imported
<module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'>
重载模块方法四/ y" R- m1 X) R9 n0 o
如果你对包的加载器有所了解,还可以使用下面的方法& E8 W1 Q* l6 x0 K: L' X
>>> from foo import bar
successful to be imported
>>> from foo import bar
>>>
>>> bar.__spec__.loader.load_module()
successful to be imported
<module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'>
重载模块方法五% u9 i3 p5 `8 U% t' K
既然影响我们重复导入的是 sys.modules,那我们只要将已导入的包从其中移除是不是就好了呢?, _5 Q$ i# M$ T( j/ ^0 i