Skip to content

App as module fails when reloading is enabled (with wrong error message) #6066

Description

@NichtJens

First Check

  • I added a very descriptive title here.
  • This is not a security issue (those should be reported via the security advisory instead).
  • This is not a Q&A. I am sure something is wrong with NiceGUI or its documentation.
  • I used the GitHub search to find a similar issue and came up empty.

Example Code

Description

  • Create folder called xxx.
  • Add empty file __init__.py into xxx.
  • Add file __main__.py into xxx with below contents.
  • From level above xxx run python -m xxx.

__main__.py:

from nicegui import ui
ui.label("xxx")
ui.run(reload=True)

This crashes with:

You must call ui.run() to start the server.
If ui.run() is behind a main guard
   if __name__ == "__main__":
remove the guard or replace it with
   if __name__ in {"__main__", "__mp_main__"}:
to allow for multiprocessing.

ERROR:    Application startup failed. Exiting.

After changing to ui.run(reload=False), it runs as expected. From this, I would conclude that the automatic reloading has problems reloading apps written as modules. Is this expected?

The error message is wrong since the code definitely has ui.run() and does not hide it behind a wrong guard.

NiceGUI Version

3.12.0

Python Version

3.13.13

Browser

Firefox

Operating System

Linux

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugType/scope: Incorrect behavior in existing functionalityreviewStatus: PR is open and needs review

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions