2

I am trying to get my login page working with blueprints. Before I separated my python package into separate packages, the app was successfully running with just app.py. However, now I am trying to make it more modular, I am having trouble running the app and it does not let me import the blueprint I created.

My file structure is like this:

myapp/
run.py
myapp/
    __init.py__
    app.py
    models.py
...

Here is my run.py:

from myapp import app
app.run(host= '0.0.0.0', debug=True)

I am initializing my blueprint in app.py like this:

.... 
from flask import Blueprint

bp = Blueprint('bp', __name__)

@bp.route('/')
@bp.route('/home')
....

I am calling it from __init.py__ like this:

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

from . import bp             # line 35
app.register_blueprint(bp)

However, no matter what I change the import to, it keeps complaining that it cannot import

# python3 run.py
Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from usb import app
  File "/my/path/to/myapp/myapp/__init__.py", line 35, in <module>
    from . import bp
ImportError: cannot import name 'bp'

I even tried changing from . import bp to from myapp.app import bp, and then it throws a different error AttributeError: module 'myapp.app' has no attribute 'register_blueprint'

Here is the full error:

  Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from myapp import app
  File "/my/path/to/myapp/myapp/__init__.py", line 33, in <module>
    app.register_blueprint(bp)
AttributeError: module 'myapp.app' has no attribute 'register_blueprint'

Does anyone know what I am doing wrong here?

tiger_groove
  • 956
  • 2
  • 17
  • 46

2 Answers2

0

As far as i understand, you need to replace from from . import bp to from myapp.app import bp. This happens because of __init__.py, which makes package from 'myapp' dir, and python wait import path with package name.

Here is a little exmaple: Project Structure:

myapp\
  myapp\
    __init__.py
    app.py
  run.py

run.py listing:

from myapp import app
app.run(host='0.0.0.0', debug=True)

myapp\__init__.py listing:

from flask import Flask
from myapp.app import bp

app = Flask(__name__)
app.register_blueprint(bp)

myapp\app.py listing:

from flask import Blueprint

@bp.route('/')
@bp.route('/home')
def home():
   return '<html><body><h1>Hello, World!</h1></body></html>'
bychok.vl
  • 91
  • 2
  • 5
  • In my post, I did indicate, that I tried changing it `from . import bp` to `from myapp.app import bp`, but after changing it I get this error `AttributeError: module 'myapp.app' has no attribute 'register_blueprint'`. Have you seen this before? (i updated the full error output when i tried what you suggested in my post) – tiger_groove Jul 24 '18 at 17:13
  • 1
    So I renamed my files and changed my files structure a bit and all of a sudden it started working. I'm not sure why though. – tiger_groove Jul 24 '18 at 20:41
  • Yes, I have seen your comment, but I have no enough repatation to comment. What kind of changes you made to solve your problem? – bychok.vl Jul 30 '18 at 16:51
  • I changed two things, first I changed `my_app` folder name it used to be called just `app`, and then I changed `app.py` to `auth.py` and put it in an auth directory. Not sure which step fixed the issue. – tiger_groove Jul 30 '18 at 18:02
-1

This error occurs because you are calling the bp instance without first loading it. When the python interpreter tries to load the route, for example:

# app/catalog/routes.py
from app.catalog import bp

@bp.route('/')
def hello():
   return "hello world"

and verifies that the bp instance has not yet been loaded, it returns the error, describing that it has not found the bp module.

so to resolve this, in the blueprint configuration file, it will be mandatory to call the route after registering the blueprint. example:

# app/catalog/__init__.py
from flask import Blueprint
bp = Blueprint('main', __name__, template_folder='templates')
from app.catalog import routes
rmachado
  • 1
  • 2