《Flask入门教程》输入>>> from app import db 报错

《Flask入门教程》第40页:输入>>> from app import db 报错:

from app import db
Traceback (most recent call last):
File “”, line 1, in
ImportError: cannot import name ‘db’
我前面是按照书上的要求补充了app.py

from flask_sqlalchemy import SQLAlchemy

from flask import Flask, render_template

import os, sys

app = Flask(name)

app.config[‘SQLALCHEMY_DATABASE_URI’] = ‘sqlite:///’ + os.path.join(app.root_path, ‘data.db’)

app.config[‘SQLALCHEMY_TRACK_MODIFICATION’] = False

db = SQLAlchemy(app)

class User(db.Model):

id = db.Column(db.Integer, primary_key = True)

name = db.Column(db.String(20))

class Movie(db.Model):

id = db.Column(db.Integer, primary_key = True)

title = db.Column(db.String(60))

year = db.Column(db.String(4))

name = ‘Grey Li’

movies = [

{'title': 'My Neighbor Totoro', 'year': '1988'},

{'title': 'Dead Poets Society', 'year': '1989'},

{'title': 'A Perfect World', 'year': '1993'},

{'title': 'Leon', 'year': '1994'},

{'title': 'Mahjong', 'year': '1996'},

{'title': 'Swallowtail Butterfly', 'year': '1996'},

{'title': 'King of Comedy', 'year': '1999'},

{'title': 'Devils on the Doorstep', 'year': '1999'},

{'title': 'WALL-E', 'year': '2008'},

{'title': 'The Pork of Music', 'year': '2012'},

]

@app.route(’/’)

def index():

return render_template('index.html', name=name, movies=movies)

我不清楚是你markdown写的有问题还是代码问题,这里的Flask对象初始化的时候,传进去的名字应该是一个字符串吧,name的话应该要用Flask(__name__),
The end, Markdown语法用 ` 符号写代码可以保留格式,多行代码可以在前后用 ```包裹

谢谢 Farmer-chong
问题的原因已经找到:我犯了一个小错误,我把flask-sqlalchemy安装到anaconda环境而不是项目虚拟环境下,所以导致报错。退出虚拟环境后,再运行就可以了。
谢谢