Spaces:
Runtime error
Runtime error
fix(ui): use lazy imports to avoid RuntimeWarning on startup (#13)
Browse filesProblem:
When running `python -m stroke_deepisles_demo.ui.app`, Python imports
the ui package first, which triggered `ui/__init__.py` to import from
`ui.app`. This caused a RuntimeWarning:
"'stroke_deepisles_demo.ui.app' found in sys.modules after import
of package 'stroke_deepisles_demo.ui', but prior to execution"
Solution:
Use `__getattr__` for lazy imports in `ui/__init__.py`. This defers
the import of `create_app` and `get_demo` until they're actually
accessed, avoiding the circular import issue.
This is a Python best practice for packages with runnable modules.
src/stroke_deepisles_demo/ui/__init__.py
CHANGED
|
@@ -1,5 +1,20 @@
|
|
| 1 |
-
"""UI module for stroke-deepisles-demo.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
from stroke_deepisles_demo.ui.app import create_app, get_demo
|
| 4 |
|
| 5 |
__all__ = ["create_app", "get_demo"]
|
|
|
|
| 1 |
+
"""UI module for stroke-deepisles-demo.
|
| 2 |
+
|
| 3 |
+
Exports:
|
| 4 |
+
create_app: Create the Gradio application
|
| 5 |
+
get_demo: Get the global demo instance (lazy initialization)
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from typing import Any
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def __getattr__(name: str) -> Any:
|
| 12 |
+
"""Lazy import to avoid circular import when running as `python -m`."""
|
| 13 |
+
if name in ("create_app", "get_demo"):
|
| 14 |
+
from stroke_deepisles_demo.ui.app import create_app, get_demo
|
| 15 |
+
|
| 16 |
+
return {"create_app": create_app, "get_demo": get_demo}[name]
|
| 17 |
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
| 18 |
|
|
|
|
| 19 |
|
| 20 |
__all__ = ["create_app", "get_demo"]
|