我正在尝试使用线程安全模式和GAE 1.6.0预发布SDK中的新Python 2.7运行时使用Jinja2运行Pyramid。我按照here概述对我的应用进行了修改,即我在runtime: python27
中设置了threadsafe: true
,app.yaml
并删除了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代码搞砸一下来解决这个问题,只留下另一个例外:
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__
如果有人能够了解金字塔在引擎盖下试图做些什么,我会很高兴。从后一个堆栈跟踪判断,它似乎正在尝试解析资产,但为什么它会尝试重新加载__main__
?我甚至不确定我的问题是由金字塔还是GAE造成的。
感谢您对此问题的任何见解。
答案 0 :(得分:2)
我不熟悉金字塔,但问题似乎确实存在于这一行:
config.include('pyramid_jinja2')
无论config
是什么,它似乎都在做一些动态导入魔术。
不要那样做。
应用引擎环境不像普通python那样处理导入。使用调试器逐步执行该行,您将最终看到导入系统的替换版本,您很快就会看到它,只实现了真正的python所做的一小部分。
如果可能的话,只需使用正常的导入语句......否则,您将不得不深入研究config.include
并使其与GAE上受限制的导入功能配合使用。
答案 1 :(得分:2)
我设法使用Pyramid 1.3的AssetResolver
使其工作。首次尝试是here。在这种情况下,我不确定旋转变压器的寿命/范围应该是什么,我稍后会详细说明。
答案 2 :(得分:0)
在pyramid_jinja2 / __ init__.py中,在_get_or_build_default_environment()之前添加以下代码
class VirtualModule(object):
def __init__(self,name):
import sys
sys.modules[name]=self
def __getattr__(self,name):
return globals()[name]
VirtualModule("__main__")
def _get_or_build_default_environment(registry):
(http://www.inductiveautomation.com/forum/viewtopic.php?f=70&p=36917)