flask importerror:无法导入名称存储

时间:2016-10-10 10:21:14

标签: python flask flask-extensions

我尝试使用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中导入存储?

1 个答案:

答案 0 :(得分:0)

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

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