flask连接mysql数据库提示ModuleNotFoundError: No module named 'MySQLdb'

输入python manage.py db migrate -m 'init tables'提示如下错误

Traceback (most recent call last):
  File "manage.py", line 15, in 
    manager.run()
  File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask_script/__init__.py", line 417, in run
    result = self.handle(argv[0], argv[1:])
  File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask_script/__init__.py", line 386, in handle
    res = handle(*args, **config)
  File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask_migrate/__init__.py", line 95, in wrapped
    f(*args, **kwargs)
  File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask_migrate/__init__.py", line 215, in migrate
    version_path=version_path, rev_id=rev_id)
  File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/alembic/command.py", line 197, in revision
    script_directory.run_env()
  File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/alembic/script/base.py", line 475, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/alembic/util/pyfiles.py", line 90, in load_python_file
    module = load_module_py(module_id, path)
  File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/alembic/util/compat.py", line 156, in load_module_py
    spec.loader.exec_module(module)
  File "", line 728, in exec_module
  File "", line 219, in _call_with_frames_removed
  File "migrations/env.py", line 95, in 
    run_migrations_online()
  File "migrations/env.py", line 77, in run_migrations_online
    poolclass=pool.NullPool,
  File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py", line 428, in engine_from_config
    return create_engine(url, **options)
  File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py", line 387, in create_engine
    return strategy.create(*args, **kwargs)
  File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 80, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 110, in dbapi
    return __import__('MySQLdb')
ModuleNotFoundError: No module named 'MySQLdb'

问题原因:

配置SQLALCHEMY_DATABASE_URI时,mysql少写了+pymysql
SQLALCHEMY_DATABASE_URI = "mysql://root:[email protected]:3306/ihome_python04"

解决办法

SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:[email protected]:3306/ihome_python04"

你可能感兴趣的:(flask连接mysql数据库提示ModuleNotFoundError: No module named 'MySQLdb')