cannot import name ‘x‘ from partially initialized module ‘x‘ (most likely due to a circular import)

问题

在使用flask+celery+blueprint时,总是报重复引入circular import
cannot import name ‘create_app’ from partially initialized module ‘app’ (most likely due to a circular import

解决方案

修改import的位置

  • 修改前import在文件的最上面:
from xx.views import xx_blueprint # 注意import位置

def register_blueprints(app):
    app.register_blueprint(xx_blueprint)
  • 修改后import在注册函数中:
def register_blueprints(app):
    from xx.views import xx_blueprint # 注意import位置
    app.register_blueprint(xx_blueprint)

把所有blueprint的引用放在注册函数中即可。

原因分析

一般在blueprint模块的view中会引入celery模块使用delay函数等,在创建flask模块时修改前会import blueprint模块,然后引入celery模块,但celery模块又依赖于flask模块。

修改后,blueprint模块在register_blueprints中引入,这时flask的app模块已经创建好了,就不会有重复引入的问题。

欢迎关注

人工智能技术分析
人工智能 机器学习 深度学习
AI人工智能三要素:数据、算力和算法
计算机视觉(CV)任务介绍:分类、检测、分割、超分、关键点识别、图像生成、度量学习

公众号持续更新原创内容,欢迎订阅。

AI人工智能与大数据
Alt

猜你喜欢

转载自blog.csdn.net/guanxs/article/details/127506687