Circular imports when creating database

Paul Clauss

First time ever using Flask to create a web app., and I'm running into issues creating a database. I get the error:

ImportError: cannot import 'app' from partially initialized module 'website' (most likely due to circular import). (In other words, I can't even run the application so that I can get to creating the database; my issue lies primarily in getting the app to run).

It's probably true that it's due to circular imports, but I've been following along with a tutorial, and mine doesn't seem to work, even after a full project reorganization. For curiosity, this is what it looks like now:

C:\...flaskProject
     static/
     templates/
          about-page.html
          blog-page.html
          home-page.html
          photography-page.html
     website/
          __init__.py
          modules.py
          routes.py
     app.py

The problem code likely lies in app.py and __init__.py, so, in that order:

from website import app

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

and

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from website import routes

app = Flask(__name__)
# *!* app.config['SECRET_KEY'] =
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite///:database.db'
# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

I've been running the commands python app.py, set FLASK_APP=app.py -> flask run, and set FLASK_APP=__init__.py -> flask run. I've seen lots on here (and in python documentation) about circular imports, and it makes some sense to me. I just don't think it's happening here, but it very well could be without my knowing. I've also looked into the static/ file that was included when I started this project in PyCharm once I selected Flask from the menu, but I believe that's irrelevant to me, for now. Any ideas on the circular import claim? How I could fix it, and any information which would help me in this field in the future? Any help is appreciated. Thanks so much in advance.

Amit Pathak

You are getting the circular import error because of partially initialized module app (circular imports). From the app.py when it calls from website import app, it initializes the website module i.e, the __init__.py. In __init__.py, the 3rd line from website import routes is causing the trouble. Your website module has not completed it's import and you are trying to import routes from this partially initialized module website.

The 3rd line in your __init__.py should be replaced by something like -

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from website.routes import route1, route2 # This is how it should be


app = Flask(__name__)
# *!* app.config['SECRET_KEY'] =
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite///:database.db'
# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

Now, it will not try to initialize the website module again. But, if you have from website import app in the routes.py file then I would suggest to move this import to the app.py file.

from website import app
from website.routes import route1, route2

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related