flask 数据库迁移报错(pymysql报错)

flask 数据库迁移报错(pymysql报错)

Scroll Down

flask 数据库迁移报错(pymysql报错)

flask项目中执行数据库迁移命令发生如下报错:

$ python manage.py db migrate
Traceback (most recent call last):
  File "manage.py", line 25, in <module>
    manager.run()
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/flask_script/__init__.py", line 417, in run
    result = self.handle(argv[0], argv[1:])
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/flask_script/__init__.py", line 386, in handle
    res = handle(*args, **config)
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/flask_migrate/__init__.py", line 96, in wrapped
    f(*args, **kwargs)
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/flask_migrate/__init__.py", line 212, in migrate
    version_path=version_path, rev_id=rev_id)
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/alembic/command.py", line 214, in revision
    script_directory.run_env()
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/alembic/script/base.py", line 489, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/alembic/util/pyfiles.py", line 98, in load_python_file
    module = load_module_py(module_id, path)
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/alembic/util/compat.py", line 184, in load_module_py
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 697, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "migrations/env.py", line 25, in <module>
    str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%'))
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 998, in engine
    return self.get_engine()
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 1017, in get_engine
    return connector.get_engine()
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 594, in get_engine
    self._engine = rv = self._sa.create_engine(sa_url, options)
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 1027, in create_engine
    return sqlalchemy.create_engine(sa_url, **engine_opts)
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/sqlalchemy/engine/__init__.py", line 525, in create_engine
    return strategy.create(*args, **kwargs)
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/sqlalchemy/engine/strategies.py", line 87, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/sqlalchemy/dialects/mysql/pymysql.py", line 62, in dbapi
    return __import__("pymysql")
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/pymysql/__init__.py", line 59, in <module>
    from . import connections  # noqa: E402
  File "/home/ubuntu/anaconda3/envs/tf_gpu1.9/lib/python3.5/site-packages/pymysql/connections.py", line 206
    ):
    ^
SyntaxError: invalid syntax

这是因为pymysql版本过高引起的报错,具体原因忘了,貌似是pymysql新版本不支持mysql 5.7版本数据库,使用pip install默认安装最新版本的pymysql,也就是1.0.0版本。

解决办法:
将pymysql降级到0.10.1

pip install pymysql==0.10.1