Circular import

Asked

Viewed 149 times

1

To doc of Flask, despite using in its examples, warns at the end of the page about the bad practice of using circular imports. Another thing that bothers me is creating "global objects" inside a file __init__.py.

What would be the other solution?

Filing cabinet __init__.py of the main module, that is, the main folder of the application:

from flask import Flask
app = Flask(__name__)

import yourapplication.views

The views.py file (The application view):

from yourapplication import app

@app.route('/')
def index():
    return 'Hello World!'

The structure of the project:

/yourapplication
    setup.py
    /yourapplication
        __init__.py
        views.py
        /static
            style.css
        /templates
            layout.html
            index.html
            login.html
            ...

Credits from the sample code: http://flask.pocoo.org/docs/1.0/patterns/packages/#simple-Packages

  • And why the object app accurate be defined in __init__.py?

  • Also wanted to know. I’m starting in the development of Flask. Looking fast it seems to me it’s by the fact that the __init__.py is called at the time the module is imported then the object would be created.

1 answer

1

Well, flask is intended to be completely modularized and free so that you feel free to define the best structure. One of the framework’s best practices is to work with the so-called "Application Factory", or "Application factory" (http://flask.pocoo.org/docs/1.0/tutorial/factory/).

When you create a "Factory app", you don’t need to have global objects and the entire setup and initialization process is carried out within the app factory function. For example:

def create_app(test_config=None):

    # cria e configura a aplicacao
    app = Flask(__name__, instance_relative_config=True)

    app.config.from_object(DevelopmentConfig)

    from .blueprint_um import views
    app.register_blueprint(views.bp)

    db.init_app(app)
    CORS(app)

    return app

In this example I create a factory with the function create_app and I do all my necessary initializations, including the registration of my Blueprints, which is very important. This function can be inside __init__.py that is within the main application package of your project. This is an important practice so that you can import your app creation function without difficulty. For example, when using waitress to run your application, just run waitress-serve --call --listen=0.0.0.0:5000 app:create_app so that the waitress understand that within your package app there is a function create_app that can be called.

Anyway, there are many different ways to do this and that’s why flask is so cool. Check out the documentation and different implementations so you know the possibilities (I’m also learning a lot still about flask). I have a small project available there in gitlab, if you want you can take a look and even improve something (has a lot to be improved) https://gitlab.com/cavalcantigor/my-flaskr

Browser other questions tagged

You are not signed in. Login or sign up in order to post.