Skip to content

Bug: Misleading and unhelpful error message upon ImportError with --app argument to CLI #4129

@burnpanck

Description

@burnpanck

Description

When using litestar run with application lookup through a directory (e.g. the CWD), and an ImportError happens, you get a normal traceback, showing exactly what is going on. However, if you instead import the app through the environment, as litestar --app "app_module:app" run, and any kind of ImportError happens, the only thing you get is this:

Using Litestar app from env: 'app_module:app'
 Invalid argument passed --app 'app_module:app': Module not found

In particular, this misleadingly seems to suggests that app_module cannot be found. In reality, you get the same error message no matter which module caused the ImportError. This issue is exacerbated by the fact that these errors typically happen due to a packaging problem, which often is only identified in CI testing or the staging environment, where direct analysis of the issue is not possible.

URL to code causing the issue

No response

MCVE

from litestar import Litestar, get

# the following module does not exist, highlighting an ImportError with a dependency.
import does_not_exist 

@get("/")
async def hello_world() -> dict[str, str]:
    """Handler function that returns a greeting dictionary."""
    return {"hello": "world"}


app = Litestar(route_handlers=[hello_world])

Steps to reproduce

  1. Put the above MCVE into a file app_module.py
  2. cd into that directory.
  3. Run litestar --app app_module:app run (ensure that litestar is installed).
  4. See error

Screenshots

No response

Logs

Using Litestar app from env: 'app_module:app'
 Invalid argument passed --app 'app_module:app': Module not found

Litestar Version

2.15.2

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🐛This is something that is not working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions