Amin
Amin

Reputation: 2043

Flask file structure

I am working on a medium size flask application. I have two main modules, one to creat databases lests call it schema.py and one to provide views, views.py. I am using the suggested structure in http://flask.pocoo.org/docs/patterns/packages/ I am defining both app and db=SQLAlchemy(app) in __init__.py. I get to the circular import issue here! The following is my file imports:

schema.py:

from myapp import db

views.py:

from myapp import app,db
import myapp.schema

__init__.py

import myapp.views

and therefore, I will receive the circular imports error when I am running the schema.py. How should I solve this? I would appreciate any suggestions.

Upvotes: 2

Views: 1816

Answers (3)

Auha
Auha

Reputation: 41

Oh yes, we've encountered circular imports a lot too, but a way to solve it is by creating an application factories found in the documentation.

http://flask.pocoo.org/docs/patterns/appfactories/

What you might also consider is create another module or another place to input components. A good example would be the following using SQLAlchemy as an example.

#: SQLalchemy object created without an attached app
db = SQLAlchemy()

def create_app():
    app = Flask(__name__)
    #: some configuration stuff

    #: Initializing app.
    db.init_app(app)

    return app

You might put the components in another module to keep them more separated if you want, but this is the simplest way that we've found to "help" prevent circular imports.

Upvotes: 3

Kishore Kumar
Kishore Kumar

Reputation: 176

From http://flask.pocoo.org/docs/patterns/packages/: (Added highlighting)

Every Python programmer hates them, and yet we just added some: circular imports (That’s when two modules depend on each other. In this case views.py depends on __init__.py). Be advised that this is a bad idea in general but here it is actually fine. The reason for this is that we are not actually using the views in __init__.py and just ensuring the module is imported and we are doing that at the bottom of the file.

Make sure that import myapp.views is at the bottom of __init__.py, after app and db are defined. It goes against pep8 guidelines but as the above quote says, here it is actually fine.

Upvotes: 1

Ignas Butėnas
Ignas Butėnas

Reputation: 6307

In flask there is an object called g and also there is a thing called current_app. current_app in simple language is the app you create in your init or any file (which initializes app) and later it represents you the app and you can play with if you import from flask import current_app. Of course there are some limitations, but for example if you need to reach settings for the app - this will help for sure.

About the g - this is very good place to keep the things like DB connection and have it everywhere around your app. If you in your __init__.py will have something like:

from flask import g
g.db = connect_to_some_db(...)

Then in other places where you need use db connection - simple:

from flask import g
# do something with your db
g.db.some_method_or_whatever(...)

Upvotes: 1

Related Questions