1

我将 SQLAlchemy 0.8 与 Google App Engine 和 Cloud SQL 一起使用。我想在部署之前在本地进行完整的集成测试,但是我无法让 SQLAlchemy 使用 mysql+gaerdbms 方言连接到我的开发 MySQL 服务器

create_engine(
    'mysql+gaerdbms:///test?instance=homingbox:instance1'
)

我从命令行启动我的应用程序引擎,如下所示

python dev_appserver.py --mysql_user=username --mysql_password=root ../Source/HomingBox/gae

每当我尝试访问数据库时,我都会得到这个可爱的堆栈跟踪

ERROR    2013-04-22 21:35:39,987 webapp2.py:1528] (ImportError)  None None
Traceback (most recent call last):
  File "/home/leon/Development/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/home/leon/Development/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/home/leon/Development/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/home/leon/Development/google_appengine/lib/webapp2-2.3/webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "/home/leon/Development/google_appengine/lib/webapp2-2.3/webapp2.py", line 547, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/home/leon/Development/google_appengine/lib/webapp2-2.3/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "/home/leon/Development/Source/HomingBox/gae/rest.py", line 19, in get_list
    self.response.write(json.dumps(application.get_list()))
  File "/home/leon/Development/Source/HomingBox/gae/dal.py", line 98, in get_list
    return query.all()
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/orm/query.py", line 2140, in all
    return list(self)
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/orm/query.py", line 2252, in __iter__
    return self._execute_and_instances(context)
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/orm/query.py", line 2265, in _execute_and_instances
    close_with_result=True)
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/orm/query.py", line 2256, in _connection_from_session
    **kw)
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/orm/session.py", line 797, in connection
    close_with_result=close_with_result)
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/orm/session.py", line 801, in _connection_for_bind
    return self.transaction._connection_for_bind(engine)
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/orm/session.py", line 297, in _connection_for_bind
    conn = bind.contextual_connect()
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/engine/base.py", line 1669, in contextual_connect
    self.pool.connect(),
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/pool.py", line 272, in connect
    return _ConnectionFairy(self).checkout()
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/pool.py", line 425, in __init__
    rec = self._connection_record = pool._do_get()
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/pool.py", line 855, in _do_get
    return self._create_connection()
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/pool.py", line 225, in _create_connection
    return _ConnectionRecord(self)
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/pool.py", line 318, in __init__
    self.connection = self.__connect()
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/pool.py", line 368, in __connect
    connection = self.__pool._creator()
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/engine/strategies.py", line 80, in connect
    return dialect.connect(*cargs, **cparams)
  File "/home/leon/Development/Source/HomingBox/gae/sqlalchemy/engine/default.py", line 279, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/home/leon/Development/google_appengine/google/storage/speckle/python/api/rdbms_googleapi.py", line 183, in __init__
    super(GoogleApiConnection, self).__init__(*args, **kwargs)
  File "/home/leon/Development/google_appengine/google/storage/speckle/python/api/rdbms.py", line 810, in __init__
    self.OpenConnection()
  File "/home/leon/Development/google_appengine/google/storage/speckle/python/api/rdbms.py", line 832, in OpenConnection
    self.SetupClient()
  File "/home/leon/Development/google_appengine/google/storage/speckle/python/api/rdbms_googleapi.py", line 193, in SetupClient
    self._client = RdbmsGoogleApiClient(**kwargs)
  File "/home/leon/Development/google_appengine/google/storage/speckle/python/api/rdbms_googleapi.py", line 106, in __init__
    rdbms.OAUTH_CREDENTIALS_PATH)
  File "/usr/lib/python2.7/posixpath.py", line 268, in expanduser
    import pwd
  File "/home/leon/Development/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 827, in load_module
    raise ImportError

任何帮助深表感谢

4

1 回答 1

1

这是我的假设,基于堆栈跟踪——当应用程序找不到您的主目录时,它会尝试导入 pwd 并以这种方式获取主目录信息。应用程序试图在 posixpath.py 中调用这段代码:

if 'HOME' not in os.environ:
        import pwd
        userhome = pwd.getpwuid(os.getuid()).pw_dir
    else:
        userhome = os.environ['HOME']

除此之外,出于安全原因,我认为GAE 不会允许导入 pwd。沙盒环境不会将其包含在其允许模块的白名单中,ImportError如果模块不在该列表中,则会引发一个。

如果我试图只做import pwd一个常规处理程序模块的内部,GAE 不会允许它。

于 2013-04-23T14:35:16.090 回答