Kolben: cannot import name 'app'
Versuchen, um mein python-Datei updater.py
SSH auf einen server, und führen Sie einige Befehle, die alle paar festgelegten Intervallen oder so. Ich bin mit APScheduler, um die Funktion auszuführen update_printer()
aus __init__.py
. Zunächst bekam ich einen working outside of application context error
aber jemand schlug vor, dass ich einfach importieren app von __init__
.py. Aber es funktioniert nicht so gut. Ich erhalte eine cannot import name 'app'
Fehler.
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'])
...
Datei-Struktur
queue/
app.py
config.py
queue_app/
__init__.py
updater.py
Fehler
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'
Was muss ich tun in der Lage sein, die app.config aus updater.py und vermeiden Sie "arbeiten außerhalb der Anwendungs-Kontext-Fehler", wenn ran von APScheduler?
Gleicher Fehler:
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 . import app ImportError: cannot import name 'app'
Oh..du hast einen Zirkelbezug... - app.py->__init__.py->updater.py->__init__.py
InformationsquelleAutor DeadCake | 2016-03-11
Du musst angemeldet sein, um einen Kommentar abzugeben.
Es ist eine zirkuläre Abhängigkeit, da Sie den import
updater
in Ihrem__init__.py
- Datei. In meinem Kolben-setupapp
erstellt inapp.py
.InformationsquelleAutor fodma1