Tag Archives: peewee error

[Solved] peewee error: ImportError: No module named ‘MySQLdb’;pymysql error: from . import connections # noqa: E402

Error content:

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

The content of the error, the file

/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

reason

  1. The python version used is 3.5
  2. Flask-MySQL uses peewee; Flask-MySQL will install the latest version of PyMySQL
  3. Peewee uses MySQLdb or pymysql to connect to the MySQL database
  4. The latest version of PyMySQL does not support Python 2.7 and 3.5 (and I use version 3.5), so peewee reports an error

solve

The latest version of PyMySQL installed without Flask-MySQL, specify the PyMySQL version as 0.10.1, and write in the requirements.txt file:PyMySQL==0.10.1