peewee 报错:ImportError: No module named 'MySQLdb';pymysql 报错:from . import connections # noqa: E402
报错内容:
Traceback (most recent call last):
File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/peewee.py", line 49, in <module>
import MySQLdb as mysql # prefer the C module.
ImportError: No module named 'MySQLdb'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/app/abadmin/current/venv/bin/flask", line 8, in <module>
sys.exit(main())
File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/flask/cli.py", line 891, in main
cli.main(args=args, prog_name=name)
File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/flask/cli.py", line 557, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/click/core.py", line 1061, in invoke
cmd_name, cmd, args = self.resolve_command(ctx, args)
File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/click/core.py", line 1100, in resolve_command
cmd = self.get_command(ctx, cmd_name)
File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/flask/cli.py", line 515, in get_command
rv = info.load_app().cli.get_command(ctx, name)
File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/flask/cli.py", line 377, in load_app
raise_if_not_found=False)
File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/flask/cli.py", line 235, in locate_app
__import__(module_name)
File "/data/app/abadmin/releases/abadmin_release_5.3.27/app/__init__.py", line 8, in <module>
from playhouse.flask_utils import FlaskDB
File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/playhouse/flask_utils.py", line 7, in <module>
from peewee import Database
File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/peewee.py", line 52, in <module>
import pymysql as mysql
File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/pymysql/__init__.py", line 59, in <module>
from . import connections # noqa: E402
File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/pymysql/connections.py", line 206
):
^
SyntaxError: invalid syntax
报错内容,所在文件
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/peewee.py
try:
import MySQLdb as mysql # prefer the C module.
except ImportError:
try:
import pymysql as mysql
except ImportError:
mysql = None
venv/lib/python3.6/site-packages/pymysql/init.py
from . import connections as _orig_conn
原因
- 使用的python版本为3.5
- Flask-MySQL使用了peewee;Flask-MySQL 会安装PyMySQL的最新版
- peewee使用 MySQLdb 或 pymysql 来连接MySQL数据库
- PyMySQL的最新版,不支持Python 2.7 和 3.5版本了(而我使用的是3.5版本),所以peewee报错
解决
不使用 Flask-MySQL 安装的PyMySQL最新版,指定PyMySQL版本为0.10.1,requirements.txt文件中写入:PyMySQL==0.10.1
资料
资料 | 网址 |
---|---|
Flask-MySQL | https://github.com/cyberdelia/flask-mysql/blob/master/setup.py (会安装PyMySQL的最新版) |
PyMySQL 版本历史 | https://github.com/PyMySQL/PyMySQL/blob/master/CHANGELOG.md (v1.0.0:Python 2.7 and 3.5 are not supported.) |