bohnsix
bohnsix

Reputation: 121

cannot import name "app" of a circular import issue

I have suffered from some problems with my code for a some hours.

I'm sure that it is caused by a circular import issue, but I have another question.

I have two blueprint, auth and main. I just return a render_template without any data passing, and things all go good. But when I try to shwo something in my main.index, suddenly an error occured.

I wonder why it works fine just in auth.views while there does have a circular import issue?

my tree struct

app
  |- auth 
       |- __init__.py
       |- forms.py
       |- views.py
  |- main
       |- __init__.py
       |- forms.py
       |- views.py
  |- template
  |- __init__.py
  |- models.py
config.py
manage.py
datta.sqlite

In auth.__init__.py:

from flask import Blueprint

auth = Blueprint("auth", __name__)

from . import views

and in auth.views.py

from app.auth import auth
from app.models import *
from manage import app


@auth.route('/')
def index():
    page = request.args.get("page", 1, type=int)
    articles = Article.query.order_by(Article.update_time.desc()).paginate(
        page, app.config["ARTICLES_PER_PAGE"], False)

    next_url = url_for('auth.index', page=articles.next_num if articles.has_next else None)
    prev_url = url_for('auth.index', page=articles.prev_num if articles.has_prev else None)
    return render_template('index.html', articles=articles.items,
                           next_url=next_url, prev_url=prev_url)

In main.__init__.py:

from flask import Blueprint

main = Blueprint("main", __name__)

from . import views

In main.views.py:

from app.main import main
from app.models import *
from manage import app

@main.route('/')
def index():
    page = request.args.get("page", 1, type=int)
    articles = Article.query.order_by(Article.update_time.desc()).paginate(
        page, app.config["ARTICLES_PER_PAGE"], False)

    next_url = url_for('main.index', page=articles.next_num if articles.has_next else None)
    prev_url = url_for('main.index', page=articles.prev_num if articles.has_prev else None)
    return render_template('index.html', articles=articles.items,
                           next_url=next_url, prev_url=prev_url)

In app.__init__.py:

def create_app():
    app = Flask(__name__)
    app.config.from_object(Config)
    Config.init_app(app)
    ...
    from app.main import main
    app.register_blueprint(main)

    from app.auth import auth
    app.register_blueprint(auth, url_prefix='/auth')

    return app

in manage.py

from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager, Shell

from app import create_app, db
from app.models import *

app = create_app()
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)


def make_shell_context():
    return dict(db=db, ArticleType=ArticleType, Source=Source,
                Article=Article, User=User, Menu=Menu,
                ArticleTypeSetting=ArticleTypeSetting)


manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command("db", MigrateCommand)


if __name__ == '__main__':
    app.run(debug=True)

And my traceback are as follow:

Traceback (most recent call last):
  File "C:/Users/bohn/Desktop/1pycharm workspace/BlogPoweredByFlask/manage.py", line 7, in <module>
    app = create_app()
  File "C:\Users\bohn\Desktop\1pycharm workspace\BlogPoweredByFlask\app\__init__.py", line 30, in create_app
    from app.main import main
  File "C:\Users\bohn\Desktop\1pycharm workspace\BlogPoweredByFlask\app\main\__init__.py", line 5, in <module>
    from . import views
  File "C:\Users\bohn\Desktop\1pycharm workspace\BlogPoweredByFlask\app\main\views.py", line 5, in <module>
    from manage import app
  File "C:\Users\bohn\Desktop\1pycharm workspace\BlogPoweredByFlask\manage.py", line 7, in <module>
    app = create_app()
  File "C:\Users\bohn\Desktop\1pycharm workspace\BlogPoweredByFlask\app\__init__.py", line 33, in create_app
    from app.auth import auth
  File "C:\Users\bohn\Desktop\1pycharm workspace\BlogPoweredByFlask\app\auth\__init__.py", line 5, in <module>
    from . import views
  File "C:\Users\bohn\Desktop\1pycharm workspace\BlogPoweredByFlask\app\auth\views.py", line 9, in <module>
    from manage import app
ImportError: cannot import name 'app'

Upvotes: 5

Views: 10268

Answers (1)

Dave W. Smith
Dave W. Smith

Reputation: 24966

The way past this is to use flask.current_app instead of trying to import app.

In main.views, replace

from manage import app

with

from flask import current_app

then, instead of app.config[...], use current_app.config[...]

Upvotes: 10

Related Questions