我正在从事一个烧瓶项目,其中包括发送确认电子邮件链接。但是,通过使用flask-restful,存在循环导入模块的问题。在我的app.py文件中,我导入了模块并使用了api.add_resource
# app.py file
from flask_restful import Api
from flask import Flask
from account import Register
from flask_mail import Mail
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = ''
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config.from_pyfile('core/config.py')
mail = Mail(app)
api = Api(app)
api.add_resource(Register, '/register')
if __name__ == '__main__':
app.run(debug=True)在我的account.py文件中,我需要为每个POST请求发送一封电子邮件,如下所示
from app import mail
class Register(Resource):
# init some msg and configuration
mail.send(msg)现在这是一个循环导入,因为app.py导入account.py和account.py导入app.py来使用mail。有没有人可以在不放弃使用flask-restful模块的情况下解决这个问题呢?
发布于 2021-11-22 18:37:05
最简单直接的答案是:导入模块,而不是从模块导入符号。
因此,先执行import account,然后执行api.add_resource(account.Register, '/register'),而不是from account import Register。
对于account.py,类似的方法也应该有效:
import app
class Register(Resource):
app.mail.send(msg)一些基本的解释可以在https://github.com/Khan/style-guides/blob/master/style/python.md#imports上找到
https://stackoverflow.com/questions/70070080
复制相似问题