Rohit Dalal
Rohit Dalal

Reputation: 135

How can I import app from __init__.py and why I'm getting this error?

I want to import db from __init__.py int the model.py file but I can't import, an error appears that says:

Traceback (most recent call last):
  File "/home/rohit/Desktop/flask_app/run.py", line 1, in <module>
    from bytewar import create_app
  File "/home/rohit/Desktop/flask_app/bytewar/__init__.py", line 5, in <module>
    from bytewar.user.views import user_blueprint
  File "/home/rohit/Desktop/flask_app/bytewar/user/views.py", line 9, in <module>
    from bytewar.model import Login_form, Login, Post, Contacts, Signup_form, Contacts
  File "/home/rohit/Desktop/flask_app/bytewar/model.py", line 5, in <module>
    from bytewar import db
ImportError: cannot import name 'db'

My application tree:

___ run.py     # <<------- ImportError in this file.
___ bytewar/
  |__ __init__.py
  |__ config.py
  |__ model.py       
  |__ user/
  |   |__ __init__.py
  |   |__ views.py
  |   |__ static/
  |   |    |__ img/
  |   |    |__ ....
  |   |__ template/
  |       |__ index.html
  |       |__ about.html
  |
  |__ admin/
      |__ __init__.py
      |__ views.py
      |__ template/
          |__ index.html
          |__ about.html

__init__.py:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bootstrap import Bootstrap 
from flask_login import LoginManager
from bytewar.user.views import user_blueprint
from bytewar.admin.views import admin_blueprint

db = SQLAlchemy()

def create_app():
    app = Flask('bytewar')
    app = Flask(__name__.split('.')[0])
    app.config.from_pyfile('config.py')
    app.config['DEBUG'] = True

    app.register_blueprint(user_blueprint)
    app.register_blueprint(admin_blueprint)

    login_manager = LoginManager()
    bootstrap = Bootstrap(app)

    db.init_app(app)
    login_manager.init_app(app)
    login_manager.session_protection = 'strong'

    return app

Here you can see I've db.init_app(app) but still I'm getting ImportError.

After adding __init__.py file inside the bytewar.user/ and bytewar.admin/(beside views.py), I'm getting error in run.py:

Traceback (most recent call last):
  File "/home/rohit/Desktop/flask_app/run.py", line 1, in <module>
    from bytewar import app
  File "/home/rohit/Desktop/flask_app/bytewar/__init__.py", line 5, in <module>
    from bytewar.user.views import user_blueprint
  File "/home/rohit/Desktop/flask_app/bytewar/user/views.py", line 9, in <module>
    from bytewar.model import Login_form, Login, Post, Contacts, Signup_form, Contacts
  File "/home/rohit/Desktop/flask_app/bytewar/model.py", line 5, in <module>
    from run import db
  File "/home/rohit/Desktop/flask_app/run.py", line 1, in <module>
    from bytewar import app
ImportError: cannot import name 'app'

run.py:

from bytewar import app


if __name__ == "__main__":
    app.run()

My bytewar/user/__init__.py and as it is bytewar/admin/__int__.py:

from flask import Blueprint

user_blueprint = Blueprint(
    'user', __name__,
    static_folder='static',
    template_folder='template',
 )

Why is this happening?

Upvotes: 2

Views: 1466

Answers (1)

Menno H&#246;lscher
Menno H&#246;lscher

Reputation: 585

In bytewar.user.views you import db from your __init__.py. That however is only defined a few lines below the import. So at the time the import is done, db does not exist yet, which the error confirms. When you look at the example code for application factory 1 you see that the app variable is only used after it is defined, your code should do that too. Move your imports to the bottom and you are set.

As an aside: Why do you create your app in the factory and immediately overwrite it? Leave the second creation out.

Upvotes: 2

Related Questions