一个程序执行的小问题,请教

一个程序执行的小问题,请教

如 tpl.py 文件为一个模板处理程序

[Copy to clipboard] [ - ]
CODE:
import os
import types
def get_page(path,filename,vardict):
     """get a dynamic page from a file

     and will fix the var with dict, then return page string
     and status"""

     if (type(path)!=types.StringType) | (type(filename)!=types.StringType) | (type(vardict)!=types.DictType):
           apache.log_error('[pytemp]: args type error')
           raise apache.SERVER_RETURN,apache.HTTP_INTERNAL_SERVER_ERROR
     fullpath=os.path.join(path,filename)
     try:
           file=open(fullpath,'r')
           filestr=file.read()
           file.close()
     except:
           apache.log_error("[pytemp]: can't open & read file")
           raise apache.SERVER_RETURN,apache.HTTP_NOT_FOUND
     try:      #by gashero
           filestr=filestr % vardict
     except KeyError,keyname:
           apache.log_error('[pytemp]: vardict not match KEY='+keyname)
           raise apache.SERVER_RETURN,apache.HTTP_INTERNAL_SERVER_ERROR
     return filestr
   
#print get_page('tpl','tpl.htm',{'aa':'dfgdfgdfg'})

test.py

[Copy to clipboard] [ - ]
CODE:
import tpl
a=tpl.get_page('tpl','tpl.htm',{'aa':'45454545'})

执行 test.py 后出现错误:

Traceback (most recent call last):
  File "Z:\root\py\test.py", line 2, in <module>
    a=tpl.get_page('tpl','tpl.htm',{'aa':'45454545'})
  File "Z:\root\py\tpl.py", line 7, in get_page
    and status"""
NameError: global name 'types' is not defined



看不出问题,不过你的程序无法运行。
sorry, tpl目录下还有一个 'tpl.htm' 模板文件
内容大概是 :

模板内容标记测试:%(aa)s