0

I've looked at help guides and so forth but I still cannot seem to get Flask Blueprints working.

Here is my structure

root /
config.py
   |app/
        | __init__.py
        | user_auth/

              | __init__.py
              | app.py
              | routes.py

In the app/init.py file, it is the following

from flask import Flask
from config import Config


app = Flask(__name__)
app.config.from_object(Config)

from .user_auth.routes import user_auth


app.register_blueprint(user_auth, url_prefix="")

From the user_auth init.py file is

from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = 'login'

app.config['SECRET_KEY'] = 'trial'

from user_auth import routes, models

This is the user_auth/routes.py file

from user_auth import app, db
from flask_login import login_required
from user_auth.forms import RegistrationForm, LoginForm
from flask import request, render_template, flash, redirect, url_for, Blueprint
from werkzeug.urls import url_parse
from flask_login import logout_user

from flask_login import current_user, login_user
from user_auth.models import User


from flask import Blueprint
user_auth = Blueprint('user_auth', __name__)

@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html', title='Welcome')


@app.route('/register', methods=["POST", "GET"])
def register():

... (Other paths)

I am running Windows and used the following command

set FLASK_APP=app

I then use

flask run

When I try to run, it says the following:

Error: While importing "app", an ImportError was raised:

Traceback (most recent call last):
  File "...\python38-32\lib\site-packages\flask\cli.py", line 240, in locate_app
    __import__(module_name)
  File "...\root\app\__init__.py", line 8, in <module>
    from .user_auth.routes import user_auth
  File "...\root\app\user_auth\__init__.py", line 16, in <module>
    from user_auth import routes, models
ModuleNotFoundError: No module named 'user_auth'

Thank you in advance for the help.

Daniel
  • 29
  • 3
  • Why is there 2 different Flask app defined ? One in `app/init.py` and ont in `app/user_auth/init.py` ? – azro Nov 08 '20 at 08:57

1 Answers1

0

For all files in app/user_auth/ like the init.py and routes.py that you mentionned up there, you nede either to access the package with

  • absolute import : from root.app.user_auth
  • relative import : from ..user_auth

Then i'd suggest to not use the same name for more than 1 think, here user_auth is both a package name, and the the variable name of you blueprint

Change like

 # routes.py
user_auth_bp = Blueprint('user_auth', __name__)

# app/__init__.py
from .user_auth.routes import user_auth_bp
app.register_blueprint(user_auth_bp, url_prefix="")
azro
  • 53,056
  • 7
  • 34
  • 70
  • Thanks! This helped but I have another issue. The question is linked here: https://stackoverflow.com/questions/64736778/how-to-use-flask-login-with-blueprints – Daniel Nov 08 '20 at 09:59