Flask python app.py不运行但manage.py runserver确实运行

时间:2017-08-02 14:33:09

标签: python flask flask-sqlalchemy circular-dependency

我有一个烧瓶应用程序,我正在尝试运行。看来当我运行python app.py时,有一种循环导入。这是我的models.py和app.py:

的代码

models.py

import datetime
from app import bcrypt, db

class User(BaseModel, db.Model):
    __tablename__ = "users"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(255), unique=True, nullable=False)
    password = db.Column(db.String(255), nullable=False)
    registered_on = db.Column(db.DateTime, nullable=False)
    admin = db.Column(db.Boolean, nullable=False, default=False)

    def __init__(self, email, password, admin=False):
        self.email = email
        self.password = bcrypt.generate_password_hash(password)
        self.registered_on = datetime.datetime.now()
        self.admin = admin

app.py

from flask import Flask, render_template
from flask import request, jsonify, session
from flask.ext.bcrypt import Bcrypt

from flask_sqlalchemy import SQLAlchemy
from models import User

app = Flask(__name__)
db = SQLAlchemy()

POSTGRES = {
    'user': 'postgres',
    'db': 'postgres',
    'host': 'localhost',
    'port': '5432',
}
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://%(user)s@%(host)s:%(port)s/%(db)s' % POSTGRES
app.config['SECRET_KEY'] = 'lol'

bcrypt = Bcrypt(app)

db.init_app(app)

@app.route('/api/register', methods=['POST'])
def register():
    json_data = request.json
    user = User(
        email=json_data['email'],
        password=json_data['password']
    )
    try:
        db.session.add(user)
        db.session.commit()
        status = 'success'
    except:
        status = 'this user is already registered'
    db.session.close()
    return jsonify({'result': status})

manage.py

from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app import app, db
from models import User

migrate = Migrate(app, db)
manager = Manager(app)

manager.add_command('db', MigrateCommand)

@manager.command
def create_admin():
    """Creates the admin user."""
    db.session.add(User(email='admin@admin.com', password='admin', admin=True))
    db.session.commit()

if __name__ == '__main__':
    manager.run()

我玩过导入但似乎没什么用,我通常会得到一个像这样的循环导入错误:

Traceback (most recent call last):
  File "app.py", line 7, in <module>
    from models import User
  File "/Users/Rishub/Desktop/apps/topten/models.py", line 3, in <module>
    from app import bcrypt, db
  File "/Users/Rishub/Desktop/apps/topten/app.py", line 7, in <module>
    from models import User
ImportError: cannot import name User

有谁知道这个解决方案? 另外,如果我运行python manage.py runserver,我的应用运行正常,所以我很好奇为什么这个工作但python app.py没有。

1 个答案:

答案 0 :(得分:1)

你正在导入的models.py中的

from models import User
在app.py中

您正在导入模型。

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:////tmp/test.db', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
                                         autoflush=False,
                                         bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()

def init_db():
    import models
    Base.metadata.create_all(bind=engine)

要修复它,请重新构建程序,如下所示:http://flask.pocoo.org/docs/0.12/patterns/sqlalchemy/

使用数据库配置等创建一个database.py文件。

from database import Base
from sqlalchemy import Column, Integer, String

# instead of db.column directly use Column

class User(Base):

在models.py中使用它作为

from database import init_db
init_db()
from database import db_session
from models import User

@app.route('/api/register', methods=['POST'])
def register():
    json_data = request.json
    user = User(
        email=json_data['email'],
        password=json_data['password']
    )
    db_session.add(user)
    db_session.commit()
    return jsonify({'result': 'success'})

和app文件为:

const request = require('request-promise');

function main(params) {
    return request({
        url: "http://myhost.mybluemix.net/addtoimc",
        method: "POST",
        headers: {
            "Content-Type": "text/plain"
        }
    }).then(response => {
        if(response.success) {
            return Promise.resolved({message: "nice"});
        } else {
            return Promise.rejected({error: "it broke"});
        }
    });
}