>>> 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 那方法就多了,详细请看下面 " S% {2 R5 b1 F重载模块方法二1 s. k2 i+ P j& @
如果你使用 Python3.0 -> 3.3,那么可以使用 imp.reload 方法: r: G p2 q0 j. Q
>>> 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'>
重载模块方法四 O% V; J0 S5 j- n* R% r1 v1 z 如果你对包的加载器有所了解,还可以使用下面的方法# _# u1 |/ M! {, w
>>> 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'>
重载模块方法五 4 C2 }, B7 z( C' U* V 既然影响我们重复导入的是 sys.modules,那我们只要将已导入的包从其中移除是不是就好了呢?2 I7 C6 L* I) y* P+ |' M% G