【问题标题】:Google App Engine - connecting to MySql database - unexpected keyword argument 'user'Google App Engine - 连接到 MySql 数据库 - 意外的关键字参数“用户”
【发布时间】:2013-10-24 03:43:41
【问题描述】:

我正在阅读 Google Developers Academy 在此页面上的 App Engine 教程:https://developers.google.com/appengine/training/cloud-sql/application_with_local_mysql

这是关于使用本地 MySQL 实例构建留言簿应用程序。我运行教程$ app_engine_sdk_path/dev_appserver.py --mysql_socket=mysql_socket_path . 末尾给出的 dev_appserver 命令,将这两个路径替换为我计算机中的实际路径。之后,终端输出看起来不错:

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 -

最后的错误信息行没有任何意义:TypeError: connect() got an unexpected keyword argument 'user'

它在抱怨这个:

return rdbms.connect(instance=CLOUDSQL_INSTANCE, database=DATABASE_NAME,
                     user=USER_NAME, password=PASSWORD, charset='utf8')

rdbms 来自导入语句from google.appengine.api import rdbms。我在本站找到了谷歌托管的rdbms代码:https://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/api/rdbms.py

其中的连接方法是:

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)

它肯定期待关键字参数“用户”,这让整个事情变得非常令人沮丧。我还检查以确保我在本地拥有的 rdbms 文件是相同的,而且确实如此,所以这不是由旧版本引起的。

我在这里找到了其他人遇到同样问题的另一个案例:(https://code.google.com/p/googleappengine/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Component%20Status%20Stars%20Summary%20Language%20Priority%20Owner%20Log&groupby=&sort=&id=10102),但没有得到答复。

【问题讨论】:

  • 您是否尝试过查看系统上安装的文件的源代码?根据您示例中的路径,它应该位于/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/rdbms.py。有可能你在网上找到的版本和你系统上的版本不一样。
  • @Amber 我一开始也是这么想的,但是我电脑上的版本是一样的。我会将其添加到我的问题中以明确说明。
  • 嗯。你看到里面的评论了吗? Note that rdbms_mysqldb is the module used in dev_appserver. rdbms_mysqldb 中的代码没有用户弹出位(尽管这无关紧要,因为 MySQLdb.connect 需要一个用户参数......)
  • 这绝对是奇怪的。几乎就像您要访问其他 connect()。如果您愿意编辑 SDK 源代码,请找到 rdbms.py 并在 connect() 的顶部添加一个 logging.info('in the right connect')。
  • @DaveW.Smith 你是对的,我一定要去其他的connect()。我在 rdbms.py 中添加了 logging.info 和 print 语句,但再次运行后没有看到任何一条消息。

标签: python google-app-engine webapp2


【解决方案1】:

我认为该文件可能已过时。

通过删除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]

Google App Engine 还提供了 MySQLdb 接口。

https://developers.google.com/appengine/docs/python/cloud-sql/#Python_complete_python_example

if (os.getenv('SERVER_SOFTWARE') and
  os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
  db = MySQLdb.connect(unix_socket='/cloudsql/your-project-id:your-instance-name', user='root')
else:
  db = MySQLdb.connect(host='localhost', user='root')

【讨论】:

    【解决方案2】:

    当我尝试复制您看到的症状时,请尝试尝试MySQLdb。见https://developers.google.com/appengine/docs/python/cloud-sql/#Python_Connect_to_your_database

    【讨论】:

    • 我创建了 Cloud SQL 实例并启用了计费,但是仪表板中的 SQL 提示在哪里?根据“使用 SQL 提示”页面 (developers.google.com/cloud-sql/docs/sql_prompt),它说仪表板窗格顶部有一个 SQL 提示选项卡,但我找不到。我能够找到 Data Store 查看器及其查询页面,但我认为这与 Cloud SQL 不同。
    • 我一直被困在ImportError: No module named MySQLdb 上,这让我很沮丧。我明天再试一次
    • 也许stackoverflow.com/questions/5531958/… 会有所帮助。
    【解决方案3】:

    查看您在rdbms.py 中链接到的代码:

    """Relational database API for production.
    
    Note that rdbms_mysqldb is the module used in dev_appserver.
    """
    

    然后,在rbdms_mysqldb.py

    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
    

    【讨论】:

    • 我运行该命令并返回:Requirement already satisfied (use --upgrade to upgrade): mysql-python in /usr/local/lib/python2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-x86_64.egg Cleaning up...。我使用--upgrade 选项运行它,并说安装成功。但是在启动dev_appserver 并转到项目的本地主机页面后,我仍然收到Unable to find the MySQLdb library 错误
    猜你喜欢
    • 1970-01-01
    • 2021-01-02
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 2020-12-25
    相关资源
    最近更新 更多