Sorry, again: Module Loading Problem

fowlertrainer at anonym.hu fowlertrainer at anonym.hu
Mon Jan 5 11:03:45 EST 2004


 Hello !

 I have used mod_python. But it is don't reload my modules when I
 editing them.

 I try with this:
 --------------------
 # Module importer
 import os
 import traceback
 import kinterbasdb
 import sys
 import time
 import threading
 import imp
 import stat

 ImpLock=threading.Lock()
 IModuleDir="C:/bhaweb/modules"
 IModules={}

 def LoadModule(ModName):
     fp, pathname, description = imp.find_module(ModName)
     try:
       mod=imp.load_module(ModName, fp, pathname, description)
       print mod
       sys.modules[ModName]=mod
     finally:
       # Since we may exit via an exception, close fp explicitly.
       if fp: fp.close()

 def ImportModules(Modules):
     ImpLock.acquire()
     try:
         if not (IModuleDir in sys.path): sys.path.append(IModuleDir)
         # Do imports, if needed
         print Modules
         for m in Modules:
             sf=IModuleDir+"/"+m+".py"
             if sys.modules.has_key(m):
                sd=os.stat(sf)
                lm=sd[stat.ST_MTIME]
                if IModules.has_key(m):
                   dd=IModules[m]
                   if (dd<>lm):
                      print "ReLoad"
                      reload(sys.modules[m])
                IModules[m]=lm
             else:
                print "Load"
                LoadModule(m)
                sd=os.stat(sf)
                lm=sd[stat.ST_MTIME]
                IModules[m]=lm
             '''
             import s
             sf=IModuleDir+"/"+s


             '''
         print IModules
     finally:
         ImpLock.release()

 #for k in sys.modules.keys(): print k,sys.modules[k]
 '''
 f=file(IModuleDir+"/test.py","w")
 f.write("print 1")
 f.close()
 '''
 #print os.stat(IModuleDir+"/test.py")[stat.ST_MTIME]
 #sys.exit()
 '''
 ImportModules(["test"])
 f=file(IModuleDir+"/test.py","w")
 f.write("print 2")
 f.close()
 time.sleep(2)
 ImportModules(["test"])
 '''
 ImportModules(["test"])
 test.test()
 --------------------
 test.py is this:
 def test():
     print 1
 --------------------
 But python is say:

> Executing: C:\Program Files\ConTEXT\ConExec.exe
> "C:\Python\python.exe" "C:\bhaweb\modules\ModImport.py"

 ['test']
 Load
 <module 'test' from 'C:\bhaweb\modules\test.pyc'>
 {'test': 1073316581}
 Traceback (most recent call last):
   File "C:\bhaweb\modules\ModImport.py", line 75, in ?
     test.test()
 NameError: name 'test' is not defined
> Execution finished.

 I run this in native mode, but do this in under mod_apache also.

 Why ? What I do wrong ?
 Plase send the email in private also, because I'm in digest mode.

 Thanx:
  KK






-- 
Best regards,
 fowlertrainer                            mailto:fowlertrainer at anonym.hu





More information about the Python-list mailing list