Is automatic reload of a module available in Python?

Terry Reedy tjreedy at udel.edu
Wed Feb 17 15:21:51 EST 2010


On 2/17/2010 9:27 AM, R (Chandra) Chandrasekhar wrote:
> Dear Folks,
>
> I am currently developing a python program, let us call it "generic.py",
> and I am testing out the functions therein by testing them out
> interactively in the python interpreter by invoking python and doing
>
> import generic
>
> Once I hit an error, I need to revise my file and reload the module using
>
> reload(generic)

Reload is sufficiently flakey that it has been removed in 3.x. The 
problem is that it genearally only *partially* replaces the module, so 
that some code uses the old version and some the new. Guido tried to 
rewrite it but gave up and removed it. The most sensible way to 
completely remove a module is to shutdown and restart the interpreter.

> The difference in syntax between invoking import and reload is really
> costing me time and patience.
>
> Therefore, I would like to ask:
>
> 1. Is there a method of auto-reloading a module while developing it and
> testing it with the interactive python interpreter?
>
> 2. Is there a better way of developing a program?

This is what I now do.
Edit
# xxx/module.py

<incrementally written module code>

def _test():
   <incrementally added tests>

if __name__ == '__main__': _test()

with IDLE and hit F5 to run and test. IDLE runs the file in a *fresh* 
subinterpreter as the main module and then, whether or not an exception 
is raised, switches to interactive mode, so one can interactively test 
any objects created (before any exception). The switch to interactive 
mode after running a file can also be done with a command line switch 
when using CPython directly.

Terry Jan Reedy





More information about the Python-list mailing list