在gcloud上运行dev_appserver.py时发生错误“导入错误:没有名为MySQLdb._mysql的模块”?

时间:2019-02-09 01:36:03

标签: python google-app-engine flask google-cloud-platform mysql-python

我正在尝试在Google App Engine上部署Python-Flask应用程序。没有MySQL,该应用程序可以正常运行。导入from flask_mysqldb import MySQL dev_appserver.py 时,返回错误ImportError: No module named MySQLdb._mysql

我已经更新了app.yaml和requirements.txt(也尝试过MySQL-python,PyMySQL)

libraries:
- name: MySQLdb
  version: latest
Flask-MySQLdb==0.2.0
mysqlclient==1.4.1

main.py的前几行

from flask import Flask, render_template, flash, redirect, url_for, session, logging, request
from flask_mysqldb import MySQL

app = Flask(__name__)

# Config MySQL
app.config['MYSQL_HOST'] = '35.236.72.247'
app.config['MYSQL_USER'] = <removed>
app.config['MYSQL_PASSWORD'] = <removed>
app.config['MYSQL_DB'] = 'myFlaskApp'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'

# Initialize MySQL
mysql = MySQL(app)

dev_appserver.py app.yaml的终端转储

INFO     2019-02-09 01:13:00,098 devappserver2.py:278] Skipping SDK update check.
INFO     2019-02-09 01:13:00,172 api_server.py:275] Starting API server at: http://localhost:52922
INFO     2019-02-09 01:13:00,178 dispatcher.py:256] Starting module "default" running at: http://localhost:8080
INFO     2019-02-09 01:13:00,179 admin_server.py:150] Starting admin server at: http://localhost:8000
INFO     2019-02-09 01:13:02,374 instance.py:294] Instance PID: 28866
ERROR    2019-02-09 01:13:04,295 wsgi.py:263]
Traceback (most recent call last):
  File "/Users/zq/Desktop/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/Users/zq/Desktop/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/Users/zq/Desktop/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/Users/zq/Desktop/Python-Flask/main.py", line 3, in <module>
    from flask_mysqldb import MySQL
  File "/Users/zq/Desktop/Python-Flask/lib/flask_mysqldb/__init__.py", line 1, in <module>
    import MySQLdb
  File "/Users/zq/Desktop/Python-Flask/lib/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
  File "/Users/zq/Desktop/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime/sandbox.py", line 1097, in load_module
    raise ImportError('No module named %s' % fullname)
ImportError: No module named MySQLdb._mysql
INFO     2019-02-09 01:13:04,301 module.py:861] default: "GET / HTTP/1.1" 500 -

该如何解决?还是在我的烧瓶应用程序中使用MySQL?该应用程序在我的系统上本地使用MySQL 正常运行。

2 个答案:

答案 0 :(得分:1)

git rm -f <project-dir>/lib/MySQLdb

gcloud app deploy

删除了lib / MysqlDB并将其再次推送到Google Cloud,并且可以正常工作。显然,我的本地库与AppEngine的本地mysqldb冲突。

现在我只需要运行dev_appserver.py app.yaml

答案 1 :(得分:1)

这只是您在回答中提到的冲突的一种解释。

您在GAE应用程序中混合了使用第三方库的2种不同过程:

答案中描述的操作删除了与您的应用捆绑在一起的库的副本,这足以使部署GAE的应用正常运行。请注意,您没有提及:

  • requirements.txt文件中删除该库,如果未完成,则意味着如果/当使用该文件重新执行lib安装时,您将再次遇到问题
  • 如果在本地运行是可以的-即,如果在本地系统上实际完成了所需的mysqldb安装