flask连接mysql老出错呢,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:zhangbin@127.0.0.1:3306/ihome_python04"

解决办法

SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:zhangbin@127.0.0.1:3306/ihome_python04"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值