在GAE上使用Beaker

3 投票
2 回答
634 浏览
提问于 2025-04-17 00:30

我正在尝试把一个本地运行的应用程序移植到谷歌应用引擎(GAE)。这个应用使用了Bottle.py框架。我用Beaker来管理会话。因为我还是个新手,所以在正确导入Beaker时遇到了一些麻烦。非常感谢大家的帮助。

我在Mac OS X 10.6.7上使用GoogleAppEngineLauncher.app来运行移植后的应用。这是在我自己的机器上模拟运行应用,而不是在谷歌的服务器上。

为了在GAE上移植,我把Bottle.py放在一个叫做'framework'的文件夹里。这个文件夹里有一个空的__init__.py文件。Bottle运行得很好,可以正常显示'hello world'。

Beaker则放在我的应用根目录下的一个单独文件夹里(journal/beaker)。Beaker也有一个空的__init__.py

相关代码:

from framework import bottle
from beaker import SessionMiddleware
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

@bottle.route('/')
def index():
    return "hello, world"

def main():
    bottle.debug(True)
    run_wsgi_app(bottle.default_app())

if __name__ == '__main__':
    main()

我收到这样的错误信息:

File "/Users/mscantland/code/journal/main.py", line 19, in <module>
    from beaker import SessionMiddleware
ImportError: cannot import name SessionMiddleware

到目前为止,我尝试过以下方法来解决这个问题:

  • 检查了/beaker文件夹下所有文件的权限,确保它们是可执行的。

  • 直接运行Beaker,并且重写了所有的导入语句,像这样:

    from beaker.x import y

变成了:

from x import y
  • 添加了'pkg_resources.py',这个文件在GAE使用的Python版本的标准库里是没有的。

2 个回答

0

我通过重新审视这个问题,使用了webapp和谷歌的用户服务,因为它们在使用GAE时有更好的文档说明。

0

SessionMiddleware 在 middleware.py 文件里。你可以试试:

from beaker.middleware import SessionMiddleware

撰写回答