在执行python models.py 中报错

来源:3-8 .管理员-登录日志-操作日志数据模型设计

JoannaMua

2017-08-18

Traceback (most recent call last):

  File "models.py", line 183, in <module>

    db.create_all()

  File "C:\Users\Administrator\movie_venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1007, in create_all

    self._execute_for_all_tables(app, bind, 'create_all')

  File "C:\Users\Administrator\movie_venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 999, in _execute_for_all_tables

    op(bind=self.get_engine(app, bind), **extra)

  File "C:\Users\Administrator\movie_venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 941, in get_engine

    return connector.get_engine()

  File "C:\Users\Administrator\movie_venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 543, in get_engine

    self._engine = rv = sqlalchemy.create_engine(info, **options)

  File "C:\Users\Administrator\movie_venv\lib\site-packages\sqlalchemy\engine\__init__.py", line 387, in create_engine

    return strategy.create(*args, **kwargs)

  File "C:\Users\Administrator\movie_venv\lib\site-packages\sqlalchemy\engine\strategies.py", line 80, in create

    dbapi = dialect_cls.dbapi(**dbapi_args)

  File "C:\Users\Administrator\movie_venv\lib\site-packages\sqlalchemy\dialects\mysql\mysqldb.py", line 110, in dbapi

    return __import__('MySQLdb')

ModuleNotFoundError: No module named 'MySQLdb'


老师如果需要更多代码,请提示我需要给出哪段代码,感谢!

写回答

1回答

rustgopy

2017-08-19

同学你好,默认的mysqldb不存在,使用pymysql模块,在头部这样引入:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import pymysql


app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:root@127.0.0.1:8889/movie"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
db = SQLAlchemy(app)


0
4
rustgopy
回复
JoannaMua
同学,你好,只要没有红色的波浪线说明已经下载了,原因是数据库链接里面要使用pymysql,所以必须要引入这个依赖包
2017-08-19
共4条回复

Python Flask 构建微电影视频网站

Python 最好用轻量级框架,让你轻松胜任 Python Web 工程师

683 学习 · 663 问题

查看课程