Do I have to quit python to load a module?

Peter Otten __peter__ at web.de
Sat May 5 02:12:49 EDT 2007


wang frank wrote:

> When I edit a module, I have to quit python and then restart python and
> then import the module. Are there any way to avoid quit python to load an
> updated module? When I am debugging a module code, I need to constantly
> make changes. It is not convenient to quit and reload.

There is the reload() function, but it has pitfalls. Objects referenced from
without the module are not updated:

>>> open("tmp.py", "w").write("""
... def f(): print "version one"
... """)
>>> import tmp
>>> tmp.f()
version one
>>> g = tmp.f
>>> open("tmp.py", "w").write("""
... def f(): print "version two"
... """)
>>> reload(tmp)
<module 'tmp' from 'tmp.py'>
>>> tmp.f()
version two
>>> g()
version one

Peter



More information about the Python-list mailing list