无法在webapp2/Google App Engine中加载jinja2

8
我正在使用GoogleAppEngineLauncher在我的Mac上进行GAE webapp2教程(运行时2.7),尽管我完全按照步骤操作,但在导入Jinja2模块的地方一直出现错误:使用模板 - Google应用引擎 错误信息如下:
Traceback (most recent call last): File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 168, in Handle handler = _config_handle.add_wsgi_middleware(self._LoadHandler()) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 206, in _LoadHandler handler = import(path[0]) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 640, in Decorate return func(self, *args, **kwargs) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1756, in load_module return self.FindAndLoadModule(submodule, fullname, search_path) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 640, in Decorate return func(self, *args, **kwargs) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1628, in FindAndLoadModule description) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 640, in Decorate return func(self, *args, **kwargs) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1571, in LoadModuleRestricted description) File "/Users/ykessler/Dropbox/appgrinders/gae_apps/helloworld2.7/helloworld.py", line 9, in import jinja2 ImportError: No module named jinja2
出现这种情况的原因是找不到该模块,尽管它应该作为webapp2的附加组件之一打包。当我在文件系统上进行搜索时,看起来它在那里:
/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2_extras/jinja2.py

如果我将应用程序部署到 GAE 上,它可以正常工作。为什么在本地会失败呢?

你为什么编辑了你的问题,删除了有用的细节? - Nick Johnson
3个回答

7

webapp2包含自己的jinja2 'glue'模块,这是你正在导入的内容,但它不包含jinja2本身。如果你想使用它,你需要使用easy_install在你的系统Python中安装它,或将其放在你应用程序的目录中。


1
谢谢 - 当我将模块添加到应用程序目录中时,它起作用了。那么“粘合”模块的目的是什么? - Yarin
1
@Yarin 请查看webapp2文档 - 它提供了有用的东西,使得从webapp2使用jinja2更加容易。 - Nick Johnson
最简单的安装jinja 2步骤可以在这里找到:http://jinja.pocoo.org/docs/intro/#from-the-tarball-release - Boinst
Giovanni的答案更好。 (肯定是因为最近) - Frederic

7
不要在app.yaml文件中使用latest参数,指定一个版本以防止高度不可能但仍然可能发生的不兼容灾难。
此外,请指定2.6版本,这是唯一支持的版本,根据文档the docs
libraries:
- name: jinja2
  version: "2.6"

然后,在您的脚本中,只需import jinja2,就像文档中所说的那样。


2
我也遇到了这个错误,为了解决它,我需要在我的app.yaml文件底部添加jinja2(正如教程开头所说..)

首先在helloworld/app.yaml的底部添加以下内容:

libraries:
- name: jinja2
  version: latest

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接