如果我将我的应用程序工厂函数命名为 "create_app" 以外,flask 服务器将不会 运行

flask server won't run if I named my application factory function other than "create_app"

如果我将应用程序工厂函数命名为 create_app,我的服务器将 运行 如下所示:

def create_app():
    app = Flask(__name__)
    @app.route('/')
    def hello():
       return 'Hello, World!'

    return app

但将其命名为 create_app 以外的名称会引发错误 Failed to find Flask application or factory

def foo_app():
    app = Flask(__name__)
    @app.route('/')
    def hello():
        return 'Hello, World!'

    return app

改变大小写也会抛出同样的错误,像这样:

def Create_app():
    app = Flask(__name__)
    @app.route('/')
    def hello():
        return 'Hello, World!'

    return app

这种行为正常吗?还是我的设置有问题?

这是我的项目布局:

...\projects\mainfoo
|--packagefoo
|    |--__init__.py
|--venvfiles

在命令中,

...\projects\mainfoo>set FLASK_APP=packagefoo
...\projects\mainfoo>set FLASK_ENV=development
...\projects\mainfoo>flask run

我只是按照这个教程Project Layout & Application setup

来自flask documentation

$ export FLASK_APP=hello

$ flask run

While FLASK_APP supports a variety of options for specifying your application, most use cases should be simple. Here are the typical values:

(nothing)

The name “app” or “wsgi” is imported (as a “.py” file, or package), automatically detecting an app (app or application) or factory (create_app or make_app).

FLASK_APP=hello

The given name is imported, automatically detecting an app (app or application) or factory (create_app or make_app).

您无法自定义此行为,因为它是 flask 中要遵循的规范。