这就是遇到问题的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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。