Flask: cannot import name 'app'
Question: Trying to run my python file `updater.py` to SSH to a server and run some
commands every few set intervals or so. I'm using APScheduler to run the
function `update_printer()` from `__init__.py`. Initially I got a `working
outside of application context error` but someone suggested that I just import
app from `__init__`.py. However it isn't working out so well. I keep getting a
`cannot import name 'app'` error.

**app.py**

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

**__init__.py**

    
    
    from flask import Flask, render_template
    from apscheduler.schedulers.background import BackgroundScheduler
    from queue_app.updater import update_printer
    app = Flask(__name__)
    app.config.from_object('config')
    
    @app.before_first_request
    def init():
        sched = BackgroundScheduler()
        sched.start()
        sched.add_job(update_printer, 'interval', seconds=10)
    
    @app.route('/')
    def index():
        return render_template('index.html')
    

**updater.py**

    
    
    import paramiko
    import json
    from queue_app import app
    
    def update_printer():
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(app.config['SSH_SERVER'], username = app.config['SSH_USERNAME'], password = app.config['SSH_PASSWORD'])
    
    ...
    

**File Structure**

    
    
    queue/
       app.py
       config.py
       queue_app/
          __init__.py
          updater.py
    

**Error**

    
    
    Traceback (most recent call last):
      File "app.py", line 1, in <module>
        from queue_app import app
      File "/Users/name/queue/queue_app/__init__.py", line 3, in <module>
        from queue_app.updater import update_printer
      File "/Users/name/queue/queue_app/updater.py", line 3, in <module>
        from queue_app import app
    ImportError: cannot import name 'app'
    

What do I need to do be able to get to the app.config from updater.py and
avoid a "working outside of application context error" if ran from
APScheduler?


Answer: It's a circular dependency, as you import `updater` in your `__init__.py`
file. In my Flask setup, `app` is created in `app.py`.

