>>> 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'>
>>> 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'>
>>> 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'>
重载模块方法四3 L0 l7 Z, d4 J, m1 F7 x
如果你对包的加载器有所了解,还可以使用下面的方法- V9 z9 H& w' @4 T- W- p
>>> 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'>
重载模块方法五" n- z- I# P M8 C# b! w5 H
既然影响我们重复导入的是 sys.modules,那我们只要将已导入的包从其中移除是不是就好了呢?' A5 B# R- U) ]7 `' F2 Y