SQLAlchemy与Google Cloud SQL连接到开发服务器

时间:2013-04-22 21:41:22

标签: mysql google-app-engine python-2.7 sqlalchemy google-cloud-sql

我正在使用SQLAlchemy 0.8与Google App Engine和Cloud SQL。我想在部署之前在本地进行完整的集成测试,但我不能让SQLAlchemy使用mysql + gaerdbms dialect连接到我的开发MySQL服务器

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

我从命令行启动我的app引擎,如下所示

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

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

这是我的假设,基于堆栈跟踪 - 当应用程序找不到您的HOME目录时,它会尝试导入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将不允许它。