python - Flask: App structure to avoid circular imports -


i following book mastering flask's recommended file structure.

(the name of project paw)

in paw/paw/__init__.py:

def create_app(object_name):   app = flask(__name__)   app.config.from_object(object_name)    db.init_app(app)   robot = loggingwerobot(token='banana', enable_session=true)   robot.init_app(app, endpoint='werobot', rule='/wechat')    attach_debugging_logger(app)    app.register_blueprint(poll_blueprint)   app.register_blueprint(wechat_blueprint)    return app 

note robot variable needed in blueprint, wechat, found in: paw/paw/controllers/wechat.py

@robot.handler def request_logging_middleware(message, session):   app.logger.debug("\n%s", request.data)   return false    # allows other handlers continue execution 

so problem blueprint has no access robot variable. however, robot variable should created in create_app in paw/paw/__init__.py because trying follow application factory pattern.

any recommendation on how fix this? project can found here , trying follow this application structure

simply use same pattern using db - create robot elsewhere , import paw/paw/__init__.py file, db:

import db models import robot wechat_setup # wechat_setup invoke # robot = loggingwerobot(token='banana', enable_session=true)  def create_app(object_name):   app = flask(__name__)   app.config.from_object(object_name)    db.init_app(app)   robot.init_app(app, endpoint='werobot', rule='/wechat')