我想用 Django 代替 Django 的默认模板语言来尝试 Mako。但是当我尝试按照手册中的Template
说明导入 Mako 的课程时遇到了问题:
from mako.template import Template
mytemplate = Template("hello world!")
print mytemplate.render()
我在 Windows cmd 中执行此操作并收到这样的错误:
C:\Documents and Settings\User>cd C:\py\project\vendor\template\Mako_73 // cd to where I unpacked Mako
C:\py\project\vendor\template\Mako_73>python // run Python interpreter
>>> from mako.template import Template // trying to import and getting an error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".\mako\template.py", line 622
exec code in module.__dict__, module.__dict__
^
SyntaxError: invalid syntax
该部分的代码:
def _compile_text(template, text, filename):
identifier = template.module_id
source, lexer = _compile(template, text, filename,
generate_magic_comment=template.disable_unicode)
cid = identifier
if not util.py3k and isinstance(cid, unicode):
cid = cid.encode()
module = types.ModuleType(cid)
code = compile(source, cid, 'exec')
exec code in module.__dict__, module.__dict__
return (source, module)
会是什么?我在 Google 中找不到有关此错误的任何信息。
我正在使用 Python 3.3。
我已将 Mako-0.7.3 下载为 tar.gz 文件,然后将其解压缩为
C:\py\poject\vendor\template\Mako_73
. 我在 PYTHONPATH 或 paths.pth 中没有这个目录。C:\py\poject
是我的 Django 项目所在的目录,我\vendor\template
决定放入 Mako 并从那里导入它。
UPD
我找到了解决方案。我已经安装了 Pyramid Framework 并从那里获取了 Mako,因为 Mako 是其中的默认模板系统。Pyramid 的版本运行良好。