Flask: cannot import name 'app'
15,878
It's a circular dependency, as you import updater in your __init__.py file. In my Flask setup, app is created in app.py.
Author by
DeadCake
Updated on June 04, 2022Comments
-
DeadCake about 1 year
Trying to run my python file
updater.pyto SSH to a server and run some commands every few set intervals or so. I'm using APScheduler to run the functionupdate_printer()from__init__.py. Initially I got aworking outside of application context errorbut someone suggested that I just import app from__init__.py. However it isn't working out so well. I keep getting acannot 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.pyError
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?