Flask Shell无法在模块“ flask”中找到Flask应用程序或工厂

时间:2019-11-07 02:06:56

标签: python flask

我正在学习第7章中的'flask_web_development',它将应用程序封装到工厂中

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

运行烧瓶壳时报告错误

   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.

烧瓶运行正常

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在前面的章节中运行良好,我将其与`flask-shell-ipython'设置为ipython

详细信息

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.

和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)

1 个答案:

答案 0 :(得分:1)

在“烧瓶运行&”之前,您要导出“ FLASK_APP = flask.py

在“烧瓶外壳”之前,您正在导出“ FLASK_APP = flasky.py

两个名称有所不同。