gpt4 book ai didi

python - pyramid + jinja2 和新的 GAE 运行时

转载 作者:太空狗 更新时间:2023-10-30 01:17:04 26 4
gpt4 key购买 nike

我正在尝试在线程安全模式下使用新的 Python 2.7 运行时和 GAE 1.6.0 预发布 SDK 来运行带有 Jinja2 的 Pyramid。我已经按照概述对我的应用程序进行了修改 here ,即我在 app.yaml 中设置了 runtime: python27, threadsafe: true 并去掉了 main()功能。当我自己生成响应时它工作正常,但是当我尝试将 jinja2 带入等式时,我得到以下异常:

ERROR    2011-11-07 00:10:34,356 wsgi.py:170] 
Traceback (most recent call last):
File "/gae/google/appengine/runtime/wsgi.py", line 168, in Handle
[...]
File "/myapp/source/myapp-tip/main.py", line 29, in <module>
config.include('pyramid_jinja2')
File "/myapp/source/myapp-tip/lib/dist/pyramid/config/__init__.py", line 616, in include
c(configurator)
File "lib/dist/pyramid_jinja2/__init__.py", line 390, in includeme
_get_or_build_default_environment(config.registry)
File "/lib/dist/pyramid_jinja2/__init__.py", line 217, in _get_or_build_default_environment
_setup_environment(registry)
File "/lib/dist/pyramid_jinja2/__init__.py", line 253, in _setup_environment
package = _caller_package(('pyramid_jinja2', 'jinja2', 'pyramid.config'))
File "/lib/dist/pyramid_jinja2/__init__.py", line 136, in caller_package
for t in self.inspect.stack():
File "/usr/lib/python2.7/inspect.py", line 1056, in stack
return getouterframes(sys._getframe(1), context)
File "/usr/lib/python2.7/inspect.py", line 1034, in getouterframes
framelist.append((frame,) + getframeinfo(frame, context))
File "/usr/lib/python2.7/inspect.py", line 1009, in getframeinfo
lines, lnum = findsource(frame)
File "/usr/lib/python2.7/inspect.py", line 534, in findsource
module = getmodule(object, file)
File "/usr/lib/python2.7/inspect.py", line 506, in getmodule
main = sys.modules['__main__']
KeyError: '__main__'

我试图用 pyramid_jinja2 代码来解决这个问题,只是留下了另一个异常(exception):

ERROR    2011-11-04 12:06:38,720 wsgi.py:170] 
Traceback (most recent call last):
File "/gae/google/appengine/runtime/wsgi.py", line 168, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
[...]
File "/myapp/source/myapp-tip/main.py", line 29, in <module>
config.add_jinja2_search_path("templates")
File "/myapp/source/myapp-tip/lib/dist/pyramid/config/util.py", line 28, in wrapper
result = wrapped(self, *arg, **kw)
File "/lib/dist/pyramid_jinja2/__init__.py", line 311, in add_jinja2_search_path
env.loader.searchpath.append(abspath_from_resource_spec(d))
File "/myapp/source/myapp-tip/lib/dist/pyramid/asset.py", line 38, in abspath_from_asset_spec
return pkg_resources.resource_filename(pname, filename)
File "/myapp/source/myapp-tip/pkg_resources.py", line 840, in resource_filename
return get_provider(package_or_requirement).get_resource_filename(
File "/myapp/source/myapp-tip/pkg_resources.py", line 160, in get_provider
__import__(moduleOrReq)
File "/gae/google/appengine/tools/dev_appserver_import_hook.py", line 640, in Decorate
return func(self, *args, **kwargs)
File "/gae/google/appengine/tools/dev_appserver_import_hook.py", line 1756, in load_module
return self.FindAndLoadModule(submodule, fullname, search_path)
File "/gae/google/appengine/tools/dev_appserver_import_hook.py", line 640, in Decorate
return func(self, *args, **kwargs)
File "/gae/google/appengine/tools/dev_appserver_import_hook.py", line 1628, in FindAndLoadModule
description)
File "/gae/google/appengine/tools/dev_appserver_import_hook.py", line 640, in Decorate
return func(self, *args, **kwargs)
File "/gae/google/appengine/tools/dev_appserver_import_hook.py", line 1571, in LoadModuleRestricted
description)
ImportError: Cannot re-init internal module __main__

如果有人能阐明 pyramid 试图在幕后做什么,我会很高兴。从后面的堆栈跟踪来看,它似乎正在尝试解析 Assets ,但为什么要尝试重新加载 __main__?我什至不确定我的问题是由 Pyramid 或 GAE 引起的。

感谢您对此问题的任何见解。

最佳答案

我不熟悉 Pyramid ,但问题确实出在这一行:

config.include('pyramid_jinja2')

无论 config 是什么,它似乎都在执行一些动态导入魔法。

不要那样做。

App Engine 环境不会像在普通 Python 中那样处理导入。使用调试器单步执行该行,您将在导入系统的替换版本中结束,您很快就会看到,它只实现了真正 python 功能的一小部分。

如果可能,只需使用普通的导入语句...否则,您将不得不深入研究 config.include 并使其与 GAE 上的受限导入功能配合使用。

关于python - pyramid + jinja2 和新的 GAE 运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8031476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com