pymysql 报错:from . import connections # noqa: E402

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 
    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 
    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 
    from playhouse.flask_utils import FlaskDB
  File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/playhouse/flask_utils.py", line 7, in 
    from peewee import Database
  File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/peewee.py", line 52, in 
    import pymysql as mysql
  File "/data/app/abadmin/current/venv/lib/python3.5/site-packages/pymysql/__init__.py", line 59, in 
    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

原因

  1. 使用的python版本为3.5

  2. Flask-MySQL使用了peewee;Flask-MySQL 会安装PyMySQL的最新版

  3. peewee使用 MySQLdb 或 pymysql 来连接MySQL数据库

  4. PyMySQL的最新版,不支持Python 2.7 和 3.5版本了(而我使用的是3.5版本),所以peewee报错

解决

不使用 Flask-MySQL 安装的PyMySQL最新版,指定PyMySQL版本为0.10.1

sudo pip3 install 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.)







山水一程

peewee 报错:ImportError: No module named ‘MySQLdb’;pymysql 报错:from . import connections # noqa: E402

anker心态

pymysql 报错:from . import connections # noqa: E402

你可能感兴趣的:(#,python,python)