简体   繁体   中英

Flask shell failed to to find Flask application or factory in module “flask”

I am learning 'flask_web_development' in chapter 7 which encapsulate an app to a factory

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    bootstrap.init_app(app)
    mail.init_app(app)
    moment.init_app(app)
    db.init_app(app)

    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    return app

It report error when run flask shell

   In [21]: !flask shell                    
Usage: flask shell [OPTIONS] [IPYTHON_ARGS]...

Error: Failed to find Flask application or factory in module "flask". Use "FLASK_APP=flask:name to specify one.

flask run works properly

In [19]: !export FLASK_APP=flask.py; export FLASK_DEBUG=1                          

In [20]: !flask run &                    

In [21]:  * Serving Flask app "flask.py" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 301-086-427

flask shell function well in the proceeding chapters which I set it to ipython with `flask-shell-ipython'

The details

In [26]: !export FLASK_APP=flasky.py     
In [27]: !flask shell                    
Usage: flask shell [OPTIONS] [IPYTHON_ARGS]...
Error: Failed to find Flask application or factory in module "flask". Use "FLASK_APP=flask:name to specify one.

and the flasky.py

In [28]: !cat flasky.py                  
import os
import click
from flask_migrate import Migrate
from app import create_app, db
from app.models import User, Role

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
migrate = Migrate(app, db)


@app.shell_context_processor
def make_shell_context():
    return dict(db=db, User=User, Role=Role)


@app.cli.command()
@click.argument('test_names', nargs=-1)
def test(test_names):
    """Run the unit tests."""
    import unittest
    if test_names:
        tests = unittest.TestLoader().loadTestsFromNames(test_names)
    else:
        tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)

Before 'flask run &' you are exporting 'FLASK_APP= flask.py '

Before 'flask shell' you are exporting 'FLASK_APP= flasky.py '

There is a difference in both the names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM