Is there a way to import a modified script module twice?

Peter Otten __peter__ at web.de
Thu Oct 30 07:36:44 EST 2003


Phipps Xue wrote:

> I wanna reload a script module after it's modified so that I can use
> the new function immediately.

<original changeme.py>
def fun():
    print "original"
</original changeme.py>

Now on the command line:

>>> import changeme
>>> from changeme import fun
>>> fun()
original
>>> changeme.fun()
original

Now change changeme to
<modified changeme.py>
def fun():
    print changed"
</modified changeme.py>

Back to the commandline, without restarting the interpreter:

>>> reload(changeme)
<module 'changeme' from 'changeme.py'>
>>> fun()
original
>>> changeme.fun()
changed
>>>

Note that reload() has no effects on the function imported with

from module import *

General rule: You'd better not use reload(). 
The minimum precaution is to always use the qualified name.

Peter









More information about the Python-list mailing list