Automatic reload()?

Michael Hudson mwh21 at cam.ac.uk
Sat Jun 3 18:25:47 EDT 2000


"Darrell Gallion" <darrell at dorb.com> writes:

> Michael Hudson wrote:
> 
> > def rr(keepem=sys.modules.keys()):
> >     for i in sys.modules.keys():
> >         if type(sys.modules[i]) is types.ModuleType and i not in keepem:
> >             print "reloading",i
> >             reload(sys.modules[i])
> >
> > I can't remember quite what the "keepem" bit's for, but I presume
> > there was some reason I put it in...
> >
> Just guessing..
> keepem will have the current list of modules, when this module is run. So
> new modules will be reloaded because they aren't in that list.

That's what it does, yes.  But ISTR something broke if it wasn't
there... ah yes, this is it:

>>> def rr(keepem=[]):
...     for i in sys.modules.keys():
...         if type(sys.modules[i]) is types.ModuleType and i not in keepem:
...             print "reloading",i
...             reload(sys.modules[i])
... 
>>> rr()
[snip]
reloading __main__
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 5, in rr
ImportError: Cannot re-init internal module __main__

Cheers,
M.

-- 
42. You can measure a programmer's perspective by noting his 
    attitude on the continuing vitality of FORTRAN.
  -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html



More information about the Python-list mailing list