>>> 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'>
但是这个方法在 Python 3.4+,就不推荐使用了+ G, x! K' A. l: J4 a5 l3 i
>>> 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'>
重载模块方法四* c# L9 E2 C! n4 A% ~
如果你对包的加载器有所了解,还可以使用下面的方法 2 J! o, o! R7 P* w# T
>>> 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'>
重载模块方法五* }# Y3 }+ M0 G' l
既然影响我们重复导入的是 sys.modules,那我们只要将已导入的包从其中移除是不是就好了呢? - ?, z, w# y- Z1 ?7 N