flask importerror:无法导入名称存储

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask importerror:无法导入名称存储相关的知识,希望对你有一定的参考价值。

我尝试使用flask-cloudy来上传文件,但是我无法导入存储(烧瓶阴天的模型)这是我配置它的方式:

class DevelopmentConfig():
    DEBUG = True
    SECRET_KEY = 'I_will_never_told_you'
    STORAGE_PROVIDER = "LOCAL"
    STORAGE_CONTAINER = "./data"
    STORAGE_KEY = ""
    STORAGE_SECRET = ""
    STORAGE_SERVER = True

我在app / init.py中初始化它:

from flask import Flask, redirect, request, url_for, g
from .main import main as main_blueprint
from .auth import auth as auth_blueprint
from config import DevelopmentConfig
from flask_login import LoginManager
from flask_cloudy import Storage     #import Storage

login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'auth.login'

def create_app():
    app = Flask(__name__)
    app.config.from_object(DevelopmentConfig)
    app.register_blueprint(main_blueprint)
    app.register_blueprint(auth_blueprint, url_prefix='/auth')
    login_manager.init_app(app)

    storage = Storage()     
    storage.init_app(app)        #initialise it 

    @login_manager.user_loader
    def load_user(user_id):
        from .models import User
        try:
            user = User.get(User.id == int(user_id))
        except User.DoesNotExist:
            user = None
        return user

    @login_manager.unauthorized_handler
    def unauthorized_callback():
        return redirect(url_for('auth.login'))

    return app

然后我尝试在app / main / views.py中导入存储:我试过了

from app import storage

from .. import storage

from ..__init__ import storage

但是当我使用from app import storagefrom .. import storage时,它仍然无法正常工作,它会像这样长大:

Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from app import create_app
  File "/home/lc/workspace/homeworkAdminSystem/app/__init__.py", line 2, in <module>
    from .main import main as main_blueprint
  File "/home/lc/workspace/homeworkAdminSystem/app/main/__init__.py", line 5, in <module>
    from . import views, errors
  File "/home/lc/workspace/homeworkAdminSystem/app/main/views.py", line 5, in <module>
    from .. import storage
ImportError: cannot import name storage

当我尝试from ..__init__ import storage时,它出现了:

Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from app import create_app
  File "/home/lc/workspace/homeworkAdminSystem/app/__init__.py", line 2, in <module>
    from .main import main as main_blueprint
  File "/home/lc/workspace/homeworkAdminSystem/app/main/__init__.py", line 5, in <module>
    from . import views, errors
  File "/home/lc/workspace/homeworkAdminSystem/app/main/views.py", line 6, in <module>
    from ..__init__ import storage
  File "/home/lc/workspace/homeworkAdminSystem/app/__init__.py", line 3, in <module>
    from .auth import auth as auth_blueprint
  File "/home/lc/workspace/homeworkAdminSystem/app/auth/__init__.py", line 5, in <module>
    from . import views
  File "/home/lc/workspace/homeworkAdminSystem/app/auth/views.py", line 4, in <module>
    from ..models import db, User, Homework, Score
  File "/home/lc/workspace/homeworkAdminSystem/app/models.py", line 3, in <module>
    from .__init__ import login_manager
ImportError: cannot import name login_manager

这里是我的模型(应用程序/模型),我使用from .__init__ import login_manager但效果很好,所以我很困惑,所以如何在views.py中导入存储?

答案

您需要将存储初始化置于create_app函数之外。

storage变量现在只能在create_app函数内访问。

以上是关于flask importerror:无法导入名称存储的主要内容,如果未能解决你的问题,请参考以下文章

flask importerror:无法导入名称存储

使用flask_sqlalchemy“ImportError:无法导入名称”

Flask,Pymongo和Mongoengine - ImportError:无法导入名称'app'

ImportError:无法从“IPython.core.debugger”导入名称“Pdb”

ImportError:在 windows7 32 位中运行 pip --version 命令时无法导入名称 main

我无法从另一个文件夹导入函数,它导致导入错误(ImportError:无法导入名称'token_required')