Skip to content

Commit ca69825

Browse files
ethanwharrislexierule
authored andcommitted
[App] Fix local app run with relative import (#16835)
1 parent 1b18c4d commit ca69825

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/lightning_app/utilities/load_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def load_app_from_file(filepath: str, raise_exception: bool = False, mock_import
112112
)
113113

114114
# TODO: Remove this, downstream code shouldn't depend on side-effects here but it does
115-
_patch_sys_path(os.path.dirname(os.path.abspath(filepath))).__enter__()
115+
sys.path.append(os.path.dirname(os.path.abspath(filepath)))
116116
sys.modules["__main__"] = main_module
117117

118118
if len(apps) > 1:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from app_metadata import RootFlow
2+
3+
from lightning_app.core.app import LightningApp
4+
5+
app = LightningApp(RootFlow())

tests/tests_app/utilities/test_load_app.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import os
2+
import sys
23
from unittest.mock import ANY
34

45
import pytest
5-
import tests_app.core.scripts
66

77
from lightning_app.utilities.exceptions import MisconfigurationException
88
from lightning_app.utilities.load_app import extract_metadata_from_app, load_app_from_file
99

1010

11-
def test_load_app_from_file():
12-
test_script_dir = os.path.join(os.path.dirname(tests_app.core.__file__), "scripts")
11+
def test_load_app_from_file_errors():
12+
test_script_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "core", "scripts")
1313
with pytest.raises(MisconfigurationException, match="There should not be multiple apps instantiated within a file"):
1414
load_app_from_file(os.path.join(test_script_dir, "two_apps.py"))
1515

@@ -20,8 +20,19 @@ def test_load_app_from_file():
2020
load_app_from_file(os.path.join(test_script_dir, "script_with_error.py"))
2121

2222

23+
@pytest.mark.parametrize("app_path", ["app_metadata.py", "app_with_local_import.py"])
24+
def test_load_app_from_file(app_path):
25+
"""Test that apps load without error and that sys.path and main module are set."""
26+
original_main = sys.modules["__main__"]
27+
test_script_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "core", "scripts")
28+
load_app_from_file(os.path.join(test_script_dir, app_path), raise_exception=True)
29+
30+
assert test_script_dir in sys.path
31+
assert sys.modules["__main__"] != original_main
32+
33+
2334
def test_extract_metadata_from_component():
24-
test_script_dir = os.path.join(os.path.dirname(tests_app.core.__file__), "scripts")
35+
test_script_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "core", "scripts")
2536
app = load_app_from_file(os.path.join(test_script_dir, "app_metadata.py"))
2637
metadata = extract_metadata_from_app(app)
2738
assert metadata == [

0 commit comments

Comments
 (0)