Python 谷歌应用引擎-连接到MySql数据库-意外的关键字参数';用户';

Python 谷歌应用引擎-连接到MySql数据库-意外的关键字参数';用户';,python,google-app-engine,webapp2,Python,Google App Engine,Webapp2,我将在本页上浏览谷歌开发者学院的应用程序引擎教程: 它是关于用本地MySQL实例构建一个留言簿应用程序。我运行教程末尾给出的dev\u appserver命令$app\u engine\u sdk\u path/dev\u appserver.py--mysql\u socket=mysql\u socket\u path。将这两个路径替换为来自我的计算机的实际路径。之后,终端输出看起来良好: INFO 2013-10-16 05:30:54,815 sdk_update_checker

我将在本页上浏览谷歌开发者学院的应用程序引擎教程:

它是关于用本地MySQL实例构建一个留言簿应用程序。我运行教程末尾给出的dev\u appserver命令
$app\u engine\u sdk\u path/dev\u appserver.py--mysql\u socket=mysql\u socket\u path。
将这两个路径替换为来自我的计算机的实际路径。之后,终端输出看起来良好:

INFO     2013-10-16 05:30:54,815 sdk_update_checker.py:245] Checking for updates to the     SDK.
INFO     2013-10-16 05:30:55,025 sdk_update_checker.py:273] The SDK is up to date.
WARNING  2013-10-16 05:30:55,066 api_server.py:332] Could not initialize images API; you are likely missing the Python "PIL" module.
INFO     2013-10-16 05:30:55,072 api_server.py:139] Starting API server at: http://localhost:50138
INFO     2013-10-16 05:30:55,076 dispatcher.py:171] Starting module "default" running at: http://localhost:8080
INFO     2013-10-16 05:30:55,081 admin_server.py:117] Starting admin server at: http://localhost:8000
但是在我的浏览器中转到localhost:8080之后,我在终端中收到一条很大的错误消息,其中许多行引用了
webapp2.py

ERROR    2013-10-16 05:31:04,828 webapp2.py:1528] connect() got an unexpected keyword argument 'user'
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine
default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/Users/patneedham/Documents/AppEngine/mysql-demo/main.py", line 29, in get
conn = get_connection()
File "/Users/patneedham/Documents/AppEngine/mysql-demo/main.py", line 23, in get_connection
user=USER_NAME, password=PASSWORD, charset='utf8')
TypeError: connect() got an unexpected keyword argument 'user'
ERROR    2013-10-16 05:31:04,830 wsgi.py:278] 
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 266, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1519, in __call__
response = self._internal_error(e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/Users/patneedham/Documents/AppEngine/mysql-demo/main.py", line 29, in get
conn = get_connection()
File "/Users/patneedham/Documents/AppEngine/mysql-demo/main.py", line 23, in get_connection
user=USER_NAME, password=PASSWORD, charset='utf8')
TypeError: connect() got an unexpected keyword argument 'user'
INFO     2013-10-16 05:31:04,837 module.py:608] default: "GET / HTTP/1.1" 500 -
"""Relational database API for production.

Note that rdbms_mysqldb is the module used in dev_appserver.
"""
try:
    import google
    import MySQLdb

    from MySQLdb import *



    __import__('MySQLdb.constants', globals(), locals(), ['*'])
except ImportError:

    def connect(instance=None, database=None):
        logging.error('The rdbms API (Google Cloud SQL) is not available because '
                      'the MySQLdb library could not be loaded. Please see the SDK '
                      'documentation for installation instructions.')

        raise NotImplementedError('Unable to find the MySQLdb library')

else:


    def connect(instance=None, database=None, **kwargs):
最后一条错误消息行没有任何意义:
TypeError:connect()得到一个意外的关键字参数“user”

它在抱怨这一点:

return rdbms.connect(instance=CLOUDSQL_INSTANCE, database=DATABASE_NAME,
                     user=USER_NAME, password=PASSWORD, charset='utf8')
rdbms
来自google.appengine.api导入rdbms的导入语句
。我在这个网站上找到了谷歌托管的rdbms代码:

其中的连接方法是:

def connect(instance=None, database=None, **kwargs):
  global _instance
  if not instance and _instance:
    instance = _instance

  if 'db' in kwargs and not database:
    database = kwargs.pop('db')

  user = None
  if 'user' in kwargs:
    user = kwargs.pop('user')

  password = None
  if 'password' in kwargs:
    password = kwargs.pop('password')

  if kwargs:
    logging.info('Ignoring extra kwargs to connect(): %r', kwargs)

  return rdbms_apiproxy.connect('unused_address',
                                instance,
                                database=database,
                                user=user,
                                password=password)
它肯定期待着关键字参数“user”,这正是让整个事情变得非常令人沮丧的原因。我还检查了一下,以确保本地的rdbms文件是相同的,而且是相同的,所以这不是因为它的旧版本造成的


我在这里发现了另一个有同样问题的人的案例:(),但没有答案。

当我尝试复制您看到的症状时,请尝试一下
MySQLdb
。请参见
rdbms.py

查看链接到的代码:

ERROR    2013-10-16 05:31:04,828 webapp2.py:1528] connect() got an unexpected keyword argument 'user'
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine
default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/Users/patneedham/Documents/AppEngine/mysql-demo/main.py", line 29, in get
conn = get_connection()
File "/Users/patneedham/Documents/AppEngine/mysql-demo/main.py", line 23, in get_connection
user=USER_NAME, password=PASSWORD, charset='utf8')
TypeError: connect() got an unexpected keyword argument 'user'
ERROR    2013-10-16 05:31:04,830 wsgi.py:278] 
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 266, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1519, in __call__
response = self._internal_error(e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/Users/patneedham/Documents/AppEngine/mysql-demo/main.py", line 29, in get
conn = get_connection()
File "/Users/patneedham/Documents/AppEngine/mysql-demo/main.py", line 23, in get_connection
user=USER_NAME, password=PASSWORD, charset='utf8')
TypeError: connect() got an unexpected keyword argument 'user'
INFO     2013-10-16 05:31:04,837 module.py:608] default: "GET / HTTP/1.1" 500 -
"""Relational database API for production.

Note that rdbms_mysqldb is the module used in dev_appserver.
"""
try:
    import google
    import MySQLdb

    from MySQLdb import *



    __import__('MySQLdb.constants', globals(), locals(), ['*'])
except ImportError:

    def connect(instance=None, database=None):
        logging.error('The rdbms API (Google Cloud SQL) is not available because '
                      'the MySQLdb library could not be loaded. Please see the SDK '
                      'documentation for installation instructions.')

        raise NotImplementedError('Unable to find the MySQLdb library')

else:


    def connect(instance=None, database=None, **kwargs):
然后,在
rbdms_mysqldb.py
中:

ERROR    2013-10-16 05:31:04,828 webapp2.py:1528] connect() got an unexpected keyword argument 'user'
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine
default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/Users/patneedham/Documents/AppEngine/mysql-demo/main.py", line 29, in get
conn = get_connection()
File "/Users/patneedham/Documents/AppEngine/mysql-demo/main.py", line 23, in get_connection
user=USER_NAME, password=PASSWORD, charset='utf8')
TypeError: connect() got an unexpected keyword argument 'user'
ERROR    2013-10-16 05:31:04,830 wsgi.py:278] 
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 266, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1519, in __call__
response = self._internal_error(e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/Users/patneedham/Documents/AppEngine/mysql-demo/main.py", line 29, in get
conn = get_connection()
File "/Users/patneedham/Documents/AppEngine/mysql-demo/main.py", line 23, in get_connection
user=USER_NAME, password=PASSWORD, charset='utf8')
TypeError: connect() got an unexpected keyword argument 'user'
INFO     2013-10-16 05:31:04,837 module.py:608] default: "GET / HTTP/1.1" 500 -
"""Relational database API for production.

Note that rdbms_mysqldb is the module used in dev_appserver.
"""
try:
    import google
    import MySQLdb

    from MySQLdb import *



    __import__('MySQLdb.constants', globals(), locals(), ['*'])
except ImportError:

    def connect(instance=None, database=None):
        logging.error('The rdbms API (Google Cloud SQL) is not available because '
                      'the MySQLdb library could not be loaded. Please see the SDK '
                      'documentation for installation instructions.')

        raise NotImplementedError('Unable to find the MySQLdb library')

else:


    def connect(instance=None, database=None, **kwargs):
因此,如果没有安装
MySQLdb
,那么在没有
user
**kwargs
参数的情况下定义
connect
似乎是唯一的方法

pip install mysql-python

我想这份文件可能过时了

通过删除
connect
中的用户和密码参数,并在启动dev_appserver时将其移动,将更容易解决此问题。因为rdbms在运行时并不真正使用这两个参数,所以最好让它们只存在于开发环境中

def get_connection():
    # remove user / password
    return rdbms.connect(instance=CLOUDSQL_INSTANCE, database=DATABASE_NAME)

# start the dev server with user/password
$ app_engine_sdk_path/dev_appserver.py --mysql_socket=mysql_socket_path --mysql_user=[MYSQL_USER] --mysql_password=[MYSQL_PASSWORD] . 

# some other variables available.
[--mysql_host MYSQL_HOST] [--mysql_port MYSQL_PORT]
[--mysql_user MYSQL_USER]
[--mysql_password MYSQL_PASSWORD]
[--mysql_socket MYSQL_SOCKET]
谷歌应用引擎还提供了一个MySQLdb接口


您是否尝试查看系统上安装的文件的源代码?根据示例中的路径,它应该位于
/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine default.bundle/Contents/Resources/google\u appengine/google/appengine/api/rdbms.py
。您在网上找到的版本可能与您系统上的版本不同。@Amber这是我最初的想法,但我计算机上的版本是相同的。我会在我的问题中加上这一点来说明这一点。你看到里面的评论了吗
请注意,rdbms_mysqldb是dev_appserver中使用的模块。
rdbms_mysqldb中的代码没有用户弹出位(尽管这不重要,因为mysqldb.connect需要一个用户arg…),这肯定很奇怪。这几乎就像您正在访问其他
connect()
。如果您愿意编辑SDK源代码,请查找rdbms.py并在connect()的顶部添加logging.info('在右侧连接中')。@DaveW.Smith您是对的,我必须访问其他
connect()
。我在
rdbms.py
中添加了logging.info和print语句,但在再次运行后,没有看到任何消息。我创建了云SQL实例并启用了计费,但仪表板中的SQL提示符在哪里?根据“使用SQL提示符”页面(),它说在仪表板窗格的顶部有一个SQL提示符选项卡,但我找不到。我能够找到Data Store viewer及其查询页面,但我相信这与云SQL不同。我一直被困在一个
ImportError上:没有名为MySQLdb的模块,这让我感到沮丧。明天我将再次尝试。Maybe将有所帮助。我运行了该命令并返回:
已经满足的要求(使用--升级升级):mysql-python in/usr/local/lib/python2.7/site-packages/mysql_-python-1.2.4b4-py2.7-macosx-10.8-x86_64.egg清理…
。我使用
--upgrade
选项运行它,并说已成功安装。但是在启动
dev_appserver
并转到项目的localhost页面后,我仍然发现
无法找到MySQLdb库
错误