将对象正确导入烧瓶中? Flask无法导入姓名邮件 - python

我是Flask的新手,并开始创建此小应用程序,我想发送电子邮件,但是,在flask导入初始化文件的配置时,出现错误“ ImportError:无法导入名称邮件”。
我已经读过它与我的应用程序的结构有关,但是我无法容纳它,因此没有问题。谢谢你的帮助

这是我的应用程序的结构:

└── my-project
    ├── app
    │   ├── __init__.py
    │   ├── constants
    │   │   ├── __init__.py
    │   │   ├── constants.py
    │   │   
    │   ├── models
    │   │    └── __init__.py
    │   │    ├── conectionDB.py
    │   │    ├── Erros.py
    │   │    ├── Session.py
    │   │    ├── Users.py
    │   │
    │   ├── resources
    │   │   ├── __init__.py
    │   │   └── Mail.py
    │   │   └── Session.py 
    │   ├── utils
    │   │   ├── __init__.py
    │   │   └── api_v1.py      
    │   
    ├── database
    │      ├── init.sql
    │
    ├── config
    │   ├── __init__.py
    │   ├── default.py
    │   
    └── api.py

api.py

import os
from app import app

if __name__ == '__main__':
    app.run(host=os.environ["API_IP"], port=5000, debug=os.environ["API_DEBUG"])

app / __初始化__.py

from flask import Flask
from flask_mail import Mail
from app.apiL import blueprint as apiL

# logging.config.fileConfig('config/development-logging.conf')

app = Flask(__name__)
app.config.from_object('config.default')


# app.config.from_pyfile('config.py')
app.register_blueprint(api_v1)
mail = Mail(app)

资源/ Mail.py

from app import mail # you can now import the Mail() object
from flask_mail import Message


class EmailSend:

    def send_email(subject, sender, recipients, text_body):
        msg = Message(subject=subject, sender=sender, recipients=recipients)
        msg.body = text_body
        #msg.html = html_body
        mail.send(msg)

执行时出现以下消息:

| Traceback (most recent call last):
api_1  |   File "api.py", line 2, in <module>
api_1  |     from app import app
api_1  |   File "/usr/src/api/app/__init__.py", line 5, in <module>
api_1  |     from app.api_v1 import blueprint as api_v1
api_1  |   File "/usr/src/api/app/api_v1.py", line 4, in <module>
api_1  |     from app.resources.Session import nsLogIn, nsSignUP, nsVerifyEmail
api_1  |   File "/usr/src/api/app/resources/Session.py", line 19, in <module>
api_1  |     from app.resources.Mail import EmailSend
api_1  |   File "/usr/src/api/app/resources/Mail.py", line 1, in <module>
api_1  |     from app import mail  # you can now import the Mail() object
api_1  | ImportError: cannot import name 'mail'

谢谢你的帮助。

参考方案

您可能有循环依赖问题。在resources / Mail.py文件中,尝试在send_email函数内导入邮件模块。

您的resources / Mail.py文件将类似于以下内容:

from flask_mail import Message


class EmailSend:

    def send_email(subject, sender, recipients, text_body):
        from app import mail # you can now import the Mail() object
        msg = Message(subject=subject, sender=sender, recipients=recipients)
        msg.body = text_body
        #msg.html = html_body
        mail.send(msg)

请参阅ImportError: cannot import name mail in Flask和ImportError: cannot import name mail。

使用Google Analytics(分析)API及其客户端google-api-python-client创建一个Web应用程序? - python

我正在浏览googles的api python-client-library和google analytics api。我能够执行官方文档中提到的所有步骤,但是随后我有了一些疑问。由于我以前从未做过此类事情,因此,我需要您的宝贵建议/提示。我的目标:想要使用Python(使用django / flask)和google-api-python-client设计…

Flask-RESTful-返回自定义响应格式 - python

我已经按照以下Flask-RESTful文档定义了自定义响应格式。app = Flask(__name__) api = restful.Api(app) @api.representation('application/octet-stream') def binary(data, code, headers=None): resp =…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

WTForms Flask(wtf_flask)验证程序如何为RadioField包括“必需”属性 - python

我基本上在寻找的是WTForms的InputRequired()的RadioField等效验证器。我的意思是,当您尝试提交表单而不在具有InputRequired()验证器的StringField字段中输入任何文本时,用户会在文本字段上方看到提示,提示是"Please fill out this field"。我希望用户必须选择male或…

Flask_SQLAlchemy无法像SQLAlchemy那样反映数据库 - python

我正在尝试使用python-Flask将Foreigner Database连接到Flask_SQLALchemy应用我到处都看过,包括FLASK_SQLALCHEMY official doc 过去4天里,我一直在Internet上到处寻找FLASK_SQLALCHEMY中没有ORM库的任何教程,但运气不佳我一直在查看SQLAlchemy Reflecti…