--help flag does not work with QUART_APP envvar and create_app factory

Open Beginner friendly
#483 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend, cli

Research direction

Start by reading the Quart CLI app-loading path in quart/cli.py, especially how it reads QUART_APP and handles the top-level --help option. Reproduce with a create_app factory selected through QUART_APP, then compare with --app. Done when QUART_APP=run quart --help shows help without the import error, while the reported subcommands continue to work.

Written by the indexing model from the issue text.

Description

When using a create_app() factory function, the --help flag does not work with QUART_APP envvar but does work with --app flag. All other quart subcommands work such as 'run'.

Minimal example

Layout

.
├── init.py
├── run.py
└── server.py

server.py
from quart import Quart

def create_app(mode='Development'):
    app = Quart(__name__)
    print("hello from create_app")
    return app
run.py
from .server import create_app

def main():
    app = create_app()
    app.run()


if __name__ == "__main__":
    app.run()

Expected behaviour

% quart --app=run --help
hello from create_app
Usage: quart [OPTIONS] COMMAND [ARGS]...

  A general utility script for Quart applications. An application to load must
  be given with the '--app' option, 'QUART_APP' environment variable, or with
  an 'app.py' file in the current directory.

Options:
  -e, --env-file FILE   Load environment variables from this file. python-
                        dotenv must be installed.
  -A, --app IMPORT      The QUART application or factory function to load, in
                        the form 'module:name'. Module can be a dotted import
                        or file path. Name is not required if it is 'app',
                        'application', 'create_app', or 'make_app', and can be
                        'name(args)' to pass arguments.
  --debug / --no-debug  Set 'app.debug' separately from '--env'.
  --version             Show the Quart version
  --help                Show this message and exit.

Commands:
  routes  Show the routes for the app.
  run     Run a development server.
  shell   Run a shell in the app context.

% quart --app=run run       
hello from create_app
 * Serving Quart app 'quarttest.server'
 * Debug mode: False
 * Please use an ASGI server (e.g. Hypercorn) directly in production
 * Running on http://127.0.0.1:5000 (CTRL + C to quit)
Incorrect behaviour
% QUART_APP=run quart --help
Error: While importing 'quarttest.app', an ImportError was raised:

Traceback (most recent call last):
  File "/Users/erikmelander/kod/quarttest/venv/lib/python3.11/site-packages/quart/cli.py", line 186, in locate_app
    module = import_module(module_name)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1140, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'quarttest.app'


Usage: quart [OPTIONS] COMMAND [ARGS]...

  A general utility script for Quart applications. An application to load must
  be given with the '--app' option, 'QUART_APP' environment variable, or with
  an 'app.py' file in the current directory.

Options:
  -e, --env-file FILE   Load environment variables from this file. python-
                        dotenv must be installed.
  -A, --app IMPORT      The QUART application or factory function to load, in
                        the form 'module:name'. Module can be a dotted import
                        or file path. Name is not required if it is 'app',
                        'application', 'create_app', or 'make_app', and can be
                        'name(args)' to pass arguments.
  --debug / --no-debug  Set 'app.debug' separately from '--env'.
  --version             Show the Quart version
  --help                Show this message and exit.

Commands:
  routes  Show the routes for the app.
  run     Run a development server.
  shell   Run a shell in the app context.

% QUART_APP=run quart run   
hello from create_app
 * Serving Quart app 'quarttest.server'
 * Debug mode: False
 * Please use an ASGI server (e.g. Hypercorn) directly in production
 * Running on http://127.0.0.1:5000 (CTRL + C to quit)

Environment:

  • Python version: 3.11
  • Quart version: 0.22.0
Dominant language
Python
Stars
3.7k
Forks
214
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pallets/quart

All issues in pallets/quart

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.