我正在尝试在 Google App Engine 上部署 Python-Flask 应用程序。该应用程序无需 MySQL 即可正常运行。导入 fromflask_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 -
我该如何解决这个问题?或者,在我的 Flask 应用程序中使用 MySQL?该应用程序在我的系统上本地使用 MySQL 运行良好。
最佳答案
git rm -f <project-dir>/lib/MySQLdb
gcloud app deploy
删除了 lib/MysqlDB 并将其再次推送到谷歌云,它起作用了。显然,我的本地库与 AppEngine 的 native mysqldb 发生冲突。
现在我不再需要运行 dev_appserver.py app.yaml
关于python - 在 gcloud "ImportError: No module named MySQLdb._mysql"上运行 dev_appserver.py 时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54602360/