Reload All

From: Emmanuel (
Date: 04/21/04

Date: Wed, 21 Apr 2004 15:11:36 +0200


I use a 'reload all' feature in my app, that allow to reload every

I first try this version :

import sys
def Reload():
    for a in sys.modules.values():
        if ( type(a) == type(sys) ): # I can't remember why
            if ( '__file__' in dir ( a ) ): # to avoid reload of
extending C module
## print a.__name__
                    reload ( a )
                except ImportError:
                    Log ("Reload", "error importing module" +

but there are some cases where the order of the reload is important.
I came into this one :

class Base:
    def __init__(self):
        print "Base Init"
import Base
class Derived( Base.Base):
    def __init__(self):
        print "Derived additional init"

In order to reload all to work, Base _MUST_ be reloaded before Derived.

So I continue with this code, modified from something I get on the forum
import inspect

def reload_from_root(root):
    if(not inspect.ismodule(root)):
        print 'root is not a module!! Failing.'
        print type(root)

    #Keeping lists of pairs where pairs are (parent, child)
    #to establish module relationships and prevent
    #circular reloads
    reload_me = [(None, root)]
    reloaded = []

    while(len(reload_me) > 0):
        tmp = reload_me.pop()

        for x in inspect.getmembers(tmp[1]):
              if ( '__file__' in dir ( x[1] ) ):
                  if((tmp[1], getattr(tmp[1], x[0])) not in reloaded):
                      reload_me.append((tmp[1], getattr(tmp[1], x[0])))

## reload(tmp[1]) # If I use reload here, the child is
always reloaded before the parent
        reloaded.append((tmp[0], tmp[1]))

    TheReloaded = []
    for pair in reloaded:
        if (pair[1] not in TheReloaded ): # Don't reload twice
the Base, else it can reload in this order :
                                                                # Base -
Derived1 - Base- Derived2
                                                                # and
Derived1 init will fails
            reload (pair[1])
            TheReloaded.append( pair[1])

This code solves my current problems, but I can imagine some possible
issue with two levels hierarchies.

So is there a perfect solution ?

Thanks by advance,


Relevant Pages

  • Re: Re-running unittest
    ... def testUnTest: ... The code executes the tests from the class PruebasDePrueba, ... The code does not use reload, and has no loops, so it is executed only once. ... I run the script, the test fails; I "fix" testUnTest; I run the script again, it passes. ...
  • Re: Problem with Array#delete
    ... class Character < Mind ... def initialize name, acc ... This addition only works after I reload the character.rb file while the ...
  • *Error* def : function is write protected and cannot be redefined
    ... I am getting the following message in icfb CIW, if I try to reload any ... of my skill utilites. ... *Error* def: function is write protected and cannot be redefined ...
  • Re: Reload All
    ... > I use a 'reload all' feature in my app, ...
  • reloading modules
    ... from lib_mod import performAction ... def main: ... Now, if I modify and just try to reload, lib_mod is not ...