xGlorify
xGlorify

Reputation: 119

Failing to import class from models.py

So I just migrated my little app to a blueprint with two blueprints defined.

I'm getting this error: ImportError: cannot import name User

I have no idea what's going wrong here. I'm relatively confident that there is no circular import, or if there is what exactly to do with it. I'm pretty new to both Python and programming in general.

Also if I change the core.py import line to: from users.models import * it works until I go to load the webpage, then I get a traceback saying 'global name User is not defined.' Same goes for the users\views.py import line.

The only two places where I'm trying to import it are here;

core.py:

from flask import Flask, g
from flask_sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager, current_user
from flask.ext.bcrypt import Bcrypt
from assets.forms import SearchAssetTag

app = Flask(__name__)
app.config.from_object('config')
login_manager = LoginManager()
login_manager.init_app(app)

bcrypt = Bcrypt(app)
db = SQLAlchemy(app)

from users.models import User

@login_manager.user_loader
def load_user(user_id):
    return User.query.filter(User.id == int(user_id)).first()

#Define headersearchform globally as it runs on every page with header.
@app.before_request 
def before_request():
    g.headersearchform = SearchAssetTag()
    g.current_user = current_user

from users.views import users_blueprint
from assets.views import assets_blueprint

app.register_blueprint(users_blueprint)
app.register_blueprint(assets_blueprint)

And in /users/views.py where I obviously have to reference the User object for logins and whatnot;

from flask import render_template, request, session, url_for, redirect, flash, Blueprint
from core import app, db
from sqlalchemy.exc import IntegrityError
from forms import SignupForm, LoginForm
from models import User
from flask.ext.login import login_user, login_required, current_user, logout_user

users_blueprint = Blueprint('users', __name__, template_folder = 'templates')

The folder structure looks like this;

core.py
__init__.py
config.py

     \users
          __init__.py
          views.py
          forms.py
          models.py
          \templates
               template.html
     \assets
          __init__.py
          views.py
          forms.py
          models.py
          \templates
               template.html

Lastly this is the model.py file where the User class is defined for SQLAlchemy:

from core import db, bcrypt

class User(db.Model):

    __tablename__ = "users"

    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String, nullable=False)
    last_name = db.Column(db.String, nullable=False)
    email = db.Column(db.String, nullable=False)
    title = db.Column(db.String, nullable=False)
    password = db.Column(db.String)

    def __init__(self, first_name, last_name, email, title, password):
        self.first_name = first_name
        self.last_name = last_name
        self.email = email
        self.title = title
        self.password = bcrypt.generate_password_hash(password)

Upvotes: 1

Views: 7228

Answers (2)

It works when you import user from .models in django with version 2 and above

Upvotes: 0

Gerrat
Gerrat

Reputation: 29680

You have a circular import:

models.py is importing db from core, and core.py is importing User from models

You should move this line:

from users.models import User

to the bottom of core.py. That way when models.py tries to import db from core, it will be defined (since it is past that point)

Upvotes: 3

Related Questions