[mod_python] Auto-reload

Shawn Harrison harrison at tbc.net
Fri Feb 4 09:45:56 EST 2005


Let's suppose I have the following four modules a.py, b.py, c.py, and 
util.py in a folder in sys.path, and in httpd.conf I have "PythonHandler 
a" for a given url space. If I make changes in a.py, b.py or c.py, these 
changes are immediately visible in the browser.

# a.py
def handler(req):
     import util
     b = util.import_('b', req)
     return apache.OK

# b.py
def print_(req):
     import util
     c = util.import_('c', req)

# c.py
def print_(req):
     req.write("Hello, folks.\nFine day, ain't it?\n")

# util.py
def import_(modname, req=None, pythonopt=None, reloadp=False):
     # shamelessly stolen from the Python documentation....
     mod = __import__(modname)
     components = modname.split('.')
     for comp in components[1:]:
         mod = getattr(mod, comp)

     # decide whether to reload()
     if req:
         if pythonopt and req.get_options().has_key(pythonopt):
             opt = req.get_options()[pythonopt]
             if opt == 'On' or opt == 'True' or opt == '1':
                 reloadp = True
         elif req.get_config().has_key('PythonDebug'):
             if req.get_config()['PythonDebug'] == '1':
                 reloadp = True
     if reloadp == True:
     return mod

If I haven't made changes, it reloads using the .pyc file that it 
generated on the first import. I don't see the flaws in this system, 
except the need to use a non-standard import syntax (but it's functional).
harrison at tbc.net

More information about the Mod_python mailing list