Python 运行时错误:在应用程序上下文之外工作。电子邮件

Python 运行时错误:在应用程序上下文之外工作。电子邮件,python,python-3.x,flask,flask-mail,Python,Python 3.x,Flask,Flask Mail,运行时错误:在应用程序上下文之外工作 这通常意味着您试图使用所需的功能 以某种方式与当前应用程序对象接口。解决 为此,请使用app.app_context()设置应用程序上下文。见 有关更多信息,请参阅文档 我以为我已经创建了app_上下文,但代码仍然显示运行时错误。请帮助,谢谢。app=current\u app.\u get\u current\u object() 使用app.app_context(): 通过 #在这里发送电子邮件 或 来自主导入应用程序 使用app.app_conte

运行时错误:在应用程序上下文之外工作

这通常意味着您试图使用所需的功能 以某种方式与当前应用程序对象接口。解决 为此,请使用app.app_context()设置应用程序上下文。见 有关更多信息,请参阅文档

我以为我已经创建了app_上下文,但代码仍然显示运行时错误。请帮助,谢谢。

app=current\u app.\u get\u current\u object()
使用app.app_context():
通过
#在这里发送电子邮件

来自主导入应用程序
使用app.app_context():
通过
#main.py
app=烧瓶(名称)
\u获取当前对象():

返回当前对象。如果出于性能原因或希望将真实对象传递到不同的上下文中,那么这非常有用

当前_应用程序是一个Flask代理对象:

current\u app=LocalProxy(\u find\u app)

您只能从当前线程中的当前应用程序获取应用程序对象

仍然显示错误,而且main是一个蓝图,并且应用程序没有从中导入
ImportError:无法从app.main(C:\Users\USER\Documents\Global\voting2\app\main\\ uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
from flask import current_app as app
from flask import render_template
from threading import Thread
from flask_mail import Message



def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)

def send_email(to, subject, template, **kwargs):
    msg = Message(app.config['MAIL_SUBJECT_PREFIX'] + subject,
                  sender=app.config['MAIL_SENDER'], recipients=[to])
    # msg.body = render_template(template + '.txt', **kwargs)
    msg.html = render_template(template + '.html', **kwargs)
    thr = Thread(target=send_async_email, args=(app,msg))
    thr.start()
    return thr
    #mail.send(msg)