flask importerrorï¼ cannot import name storage
<p>I try to use flask-cloudy to uploads files, but I can't import storage (a model in flask-cloudy)
this is how I config it :</p>

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

<p>and I initialise it in app/<strong>init</strong>.py :</p>

<pre><code>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
</code></pre>

<p>then I try to import storage in app/main/views.py:
I had try </p>

<pre><code>from app import storage
</code></pre>

<p>and</p>

<pre><code>from .. import storage
</code></pre>

<p>and </p>

<pre><code>from ..__init__ import storage
</code></pre>

<p>but it still can't work well
when I use <code>from app import storage</code> and <code>from .. import storage</code>
it grow up like this : </p>

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

<p>and when I tryed <code>from ..__init__ import storage</code>
it appear :</p>

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

<p>and here is my models(app/models) in it I used <code>from .__init__ import login_manager</code> but it worked well, so I am confuse of it ,so how can I import storage in my views.py?</p>

<p>You need to put your storage initialisation outside of the <code>create_app</code> function.</p>

<p>The <code>storage</code> variable right now is only accessible inside the <code>create_app</code> function. </p>

python flask flask-extensions