1

我的 Pyramid 应用程序在粘贴 httpserver 中运行时运行良好,但在 google 应用程序引擎中引发异常。

/hello/{name} url 在粘贴服务器和应用引擎中都可以正常工作

/test url 在粘贴服务器中工作正常,但在应用程序引擎中我得到了下面的 Traceback。

有任何想法吗?

from pyramid.config import Configurator
from pyramid.response import Response

def hello_world(request):
    return Response('Hello %(name)s!' % request.matchdict)

def test(request):
    return dict()

settings = {'mako.directories':'mako_templates'}
config = Configurator(settings=settings)
config.add_route('hello', '/hello/{name}')
config.add_route('test', '/test', view=test, renderer='test.mako')
config.add_view(hello_world, route_name='hello')

wsgi_app = config.make_wsgi_app()

来自谷歌应用引擎的追溯:

ERROR    2013-01-17 02:48:08,893 wsgi.py:235] 
Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 223, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/Users/mnaber/Documents/workspace/UnifiedClassified/site-packages/pyramid/router.py", line 251, in __call__
    response = self.invoke_subrequest(request, use_tweens=True)
  File "/Users/mnaber/Documents/workspace/UnifiedClassified/site-packages/pyramid/router.py", line 227, in invoke_subrequest
    response = handle_request(request)
  File "/Users/mnaber/Documents/workspace/UnifiedClassified/site-packages/pyramid/tweens.py", line 21, in excview_tween
    response = handler(request)
  File "/Users/mnaber/Documents/workspace/UnifiedClassified/site-packages/pyramid/router.py", line 161, in handle_request
    response = view_callable(context, request)
  File "/Users/mnaber/Documents/workspace/UnifiedClassified/site-packages/pyramid/config/views.py", line 367, in rendered_view
    context)
  File "/Users/mnaber/Documents/workspace/UnifiedClassified/site-packages/pyramid/renderers.py", line 531, in render_view
    return self.render_to_response(response, system, request=request)
  File "/Users/mnaber/Documents/workspace/UnifiedClassified/site-packages/pyramid/renderers.py", line 561, in render_to_response
    result = self.render(value, system_values, request=request)
  File "/Users/mnaber/Documents/workspace/UnifiedClassified/site-packages/pyramid/renderers.py", line 534, in render
    renderer = self.renderer
  File "/Users/mnaber/Documents/workspace/UnifiedClassified/site-packages/pyramid/decorator.py", line 39, in __get__
    val = self.wrapped(inst)
  File "/Users/mnaber/Documents/workspace/UnifiedClassified/site-packages/pyramid/renderers.py", line 518, in renderer
    return factory(self)
  File "/Users/mnaber/Documents/workspace/UnifiedClassified/site-packages/pyramid/mako_templating.py", line 129, in __call__
    directories = [ abspath_from_asset_spec(d) for d in directories ]
  File "/Users/mnaber/Documents/workspace/UnifiedClassified/site-packages/pyramid/asset.py", line 43, in abspath_from_asset_spec
    return pkg_resources.resource_filename(pname, filename)
  File "/Users/mnaber/Documents/workspace/UnifiedClassified/site-packages/pkg_resources.py", line 840, in resource_filename
    return get_provider(package_or_requirement).get_resource_filename(
  File "/Users/mnaber/Documents/workspace/UnifiedClassified/site-packages/pkg_resources.py", line 160, in get_provider
    __import__(moduleOrReq)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 692, 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 1766, 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 692, 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 1630, 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 692, 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 1577, in LoadModuleRestricted
    description)
ImportError: Cannot re-init internal module __main__
INFO     2013-01-17 02:48:08,909 dev_appserver.py:3103] "GET /test HTTP/1.1" 500 -
4

2 回答 2

1

我在这方面的背景很浅,所以这可能是错误的。

这是我的看法:要将 mako 与金字塔一起使用,您需要为配置器提供一个设置字典,其中包含一个名为“mako.directories”的键,该键指向您的 mako 模板目录。该键可以是相对路径或绝对路径。

pyramid/mako_templating.py 文件在这一行中将相对路径转换为绝对路径:

directories = [ abspath_from_asset_spec(d) for d in directories ]

此转换在 gae 中不起作用(请参阅堆栈跟踪)。为了解决这个问题,我使用了 Pyramid AssetResolver,如下所示:

from pyramid.path import AssetResolver
r = AssetResolver('appname')
full_mako_path = r.resolve('http/mako_templates').abspath()

然后,当您创建 Pyramid 配置器时,您可以:

settings = {'mako.directories':full_mako_path}
config = Configurator(settings=settings)

在这种情况下,我的目录结构是:

/serve.py (contains wsgi app)
/app.yaml
/appname
/appname/http/mako_templates
...
于 2013-01-18T16:08:17.013 回答
0

尝试这个:

PyramidMakoframework文件夹放在app 目录中命名的目录中。然后将这些代码行添加到.py导入下的主文件中:

import sys, os
framework_dir = "framework"
framework_dir_path = os.path.join(os.path.dirname(__file__), framework_dir)
sys.path.insert(0, framework_dir_path)
于 2013-01-17T21:52:53.220 回答