开发者社区> 问答> 正文

ipython NameError,找不到类

这就是遇到问题的app.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

mysql://username:password@server:portnumber/database
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@127.0.0.1:3306/flask_sql'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)

class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)

    us = db.relationship('User', backref='role')

    def __repr__(self):
        return 'Role:%s %s'% (self.name,self.id)

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True, index=True)
    email = db.Column(db.String(64),unique=True)
    password = db.Column(db.String(64))
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))

    def __repr__(self):
        return "<User:%s,%s,%s,%s>"%(self.id,self.name,self.email,self.password)

if __name__ == '__main__':
    db.drop_all()
    db.create_all()

    ro1 = Role(name='admin')
    db.session.add(ro1)
    db.session.commit()
    ro2 = Role(name='user')
    db.session.add(ro2)
    db.session.commit()

    us1 = User(name='wang', email='wang@163.com', password='123456', role_id=ro1.id)
    us2 = User(name='zhang', email='zhang@189.com', password='201512', role_id=ro2.id)
    us3 = User(name='chen', email='chen@126.com', password='987654', role_id=ro2.id)
    us4 = User(name='zhou', email='zhou@163.com', password='456789', role_id=ro1.id)
    us5 = User(name='tang', email='tang@itheima.com', password='158104', role_id=ro2.id)
    us6 = User(name='wu', email='wu@gmail.com', password='5623514', role_id=ro2.id)
    us7 = User(name='qian', email='qian@gmail.com', password='1543567', role_id=ro1.id)
    us8 = User(name='liu', email='liu@itheima.com', password='867322', role_id=ro1.id)
    us9 = User(name='li', email='li@163.com', password='4526342', role_id=ro2.id)
    us10 = User(name='sun', email='sun@163.com', password='235523', role_id=ro2.id)
    db.session.add_all([us1, us2, us3, us4, us5, us6, us7, us8, us9, us10])
    db.session.commit()

    db.session.delete(us2)
    db.session.commit()

    app.run(debug=True)

这是ipython错误:

(Flask_py) F:\Python\project>ipython
f:\python\python36_64\lib\site-packages\IPython\core\interactiveshell.py:925: UserWarning
: Attempting to work in a virtualenv. If you encounter problems, please install IPython i
nside the virtualenv.
  warn("Attempting to work in a virtualenv. If you encounter problems, please "
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.10.2 -- An enhanced Interactive Python. Type '?' for help.


In [1]: from Flask_SQLAlchemy import *

In [2]: role = Role(name='admin')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-1175ee0e3695> in <module>
----> 1 role = Role(name='admin')

NameError: name 'Role' is not defined

我可以在python代码中使用Role类,并且可以成功地查询数据,但是在ipython中,这个类是无法识别的。我在哪个目录下输入ipython很重要,还是在哪个virtualenv目录下输入ipython很重要?我所在的目录是forder,比项目forder高一层,而我所在的virtualenv是我用于项目的env。请帮助我。谢谢你! 问题来源StackOverflow 地址:/questions/59383665/an-ipython-nameerror-couldnt-find-a-class

展开
收起
kun坤 2019-12-27 10:12:15 451 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载