pallets / flask

The Python micro framework for building web applications.

Home Page:https://flask.palletsprojects.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cli::ScriptInfo in versions >2.2.0 does not read FLASK_APP environment variable when invoked from command line

csaska opened this issue · comments

99fa3c3#diff-fed8939d1905b99bba605ad91e9ccdf5ede6223e03ccbc3f0121853035051e62L343 removed the ability to invoke CLI commands directly using python -m by setting FLASK_APP

Pre 2.2.0 this works

$ FLASK_APP=myapp.commands.foo python3 -m myapp.commands.foo US

-->

After 2.2.0 it fails like

$ FLASK_APP=myapp.commands.foo python3 -m myapp.commands.foo US

'FLASK_ENV' is deprecated and will not be used in Flask 2.3. Use 'FLASK_DEBUG' instead.
Usage: python -m myapp.commands.foo 
           [OPTIONS] COUNTRY_CODE
Try 'python -m mayapp.commands.foo --help' for help.

Error: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.

This is especially confusing because Flask was able to parse CLI options.

E.g. of a file myapp.commands.foo.py

import click
from flask import Flask

app = Flask(__name__)

@app.cli.command("foo")
@click.argument("country_code")
def main(country_code: str, **kwargs):
    print("I want this to work so bad")


if __name__ == "__main__":
    main()

Expected behavior:
ScriptInfo.app_import_path defaults to reading FLASK_APP environment variable if app_import_path is None when ScriptInfo.__init__ is invoked.

Environment:

  • Python version:N/A
  • Flask version:>2.2.0

You'll want to invoke your commands through the flask command. If you want a custom entry point you can define your own FlaskGroup, but that's not really a public API.