我正在尝试使用这个GAE Flask tutorial。我相信我完全遵循了它,我从Github下载了代码,所以没有错别字。
当我启动开发服务器(dev_appserver.py app.yaml
)并转到http://localhost:8080/form时,我收到此错误:
Traceback (most recent call last):
File "/Users/.../google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/Users/.../google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/Users/.../google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/Users/.../GIT/TEMP/main.py", line 19, in <module>
from flask import Flask, render_template, request
File "/Users/.../GIT/TEMP/lib/flask/__init__.py", line 17, in <module>
from werkzeug.exceptions import abort
File "/Users/.../GIT/TEMP/lib/werkzeug/__init__.py", line 151, in <module>
__import__('werkzeug.exceptions')
File "/Users/.../GIT/TEMP/lib/werkzeug/exceptions.py", line 71, in <module>
from werkzeug.wrappers import Response
File "/Users/.../GIT/TEMP/lib/werkzeug/wrappers.py", line 37, in <module>
from werkzeug.formparser import FormDataParser, default_stream_factory
File "/Users/.../GIT/TEMP/lib/werkzeug/formparser.py", line 14, in <module>
from tempfile import SpooledTemporaryFile
ImportError: cannot import name SpooledTemporaryFile
看起来它与GAE沙箱有关,但我无法弄明白。我尝试在virtualenv的内部和外部运行开发服务器。
当使用virtualenv和app引擎时,你有什么特别的事吗?
本教程使用Flask版本0.12.2指定。
Flask的setup.py需要0.7版之后的任何版本的werkzeug
install_requires=[
'Werkzeug>=0.7',
'Jinja2>=2.4',
'itsdangerous>=0.21',
'click>=2.0',
],
在werkzeug 0.13中,this commit与增加对分块传输编码的支持相关,增加了SpooledTemporaryFile
对werkzeug.formparser
的导入。
App Engine沙箱disables any imports from the tempfile
module apart from tempfile.TemporaryFile
,因此错误。
要解决此问题,请将werkzeug降级到版本0.12.2,该版本不包含导入。
pip install --target lib --upgrade werkzeug==0.12.2
(pip --upgrade
强制安装提供的版本,因此它将降级以及升级)
或修改您的供应商要求文件以固定werkzeug版本
Flask==0.12.2
werkzeug==0.12.2
这个问题现在已经在Google Samples Docs repo和Werkzeug v0.14中得到解决。
我最近也遇到了这个错误。 我的Python版本在展台环境中是2.7.14。 我相信几天前有一个更新,并干涉这个包。
对我来说,请参阅Github问题here中提到的解决方案。 我只是将其添加到appengine_config.py文件中:
import tempfile
tempfile.SpooledTemporaryFile = tempfile.TemporaryFile
然后我的项目就像魅力一样。链接还提到如果你使用早期版本的Python 2.7可能会解决问题,但我还没有尝试过解决方案。