0

I am struggling to get my user blueprints to work. I am currently getting an error that it cannot import my users_blueprint. Any suggestions as to what I am missing

ERROR:
from users.views import users_blueprint ImportError: cannot import name 'users_blueprint'

.
├── app.py
├── models.py
└── users
    ├── forms.py
    └── views.py

app.py

from flask import Flask, render_template,request,redirect,url_for,session,flash
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func
from flask_login import LoginManager
from datetime import timedelta
import calendar
import os
from functools import wraps
from flask_bcrypt import Bcrypt


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] ='sqlite:///C:/Users/annie/PycharmProjects/####/#####'
app.config['SQALCHEMY_TRACK_MODIFICATIONS']=False
app.secret_key= os.urandom(24)

db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager()
login_manager.init_app(app)

from users.views import users_blueprint


# register our blueprints
app.register_blueprint(users_blueprint)

Views.py

from flask import flash, redirect, render_template, request, \
session, url_for, Blueprint
from functools import wraps
from users.forms import LoginForm
from models import User, bcrypt


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


class User(db.Model):

    __tablename__ = "users"

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

    def __init__(self, name, email, password):
        self.name = name
        self.email = email
        self.password = bcrypt.generate_password_hash(password)

    def __repr__(self):
        return '<name {}'.format(self.name)
slackmart
  • 4,754
  • 3
  • 25
  • 39

1 Answers1

1

I don't see something wrong with your code. The only issue I see is the folder structure. In order to import a package from another folder, then this folder should contain a __init__.py file. Just an empty file can do the trick. (more info can be found in the official manual of python here

Do you have the file __init__.py in the folder users?

Krypotos
  • 346
  • 2
  • 4
  • Your answer is actually a comment, but it's also the right answer (I think). Could you elaborate it a little bit more, please? So when Josh comes back he will mark it as the accepted answer. – slackmart Apr 05 '18 at 08:56
  • Truer words were never spoken @slackmart. I 've updated the answer. Thanks! – Krypotos Apr 11 '18 at 13:58