Skip to content

Commit 58e4ab6

Browse files
committed
Make Detectron2 import non-fatal for tests
- Only call sys.exit(1) when running as __main__, not when imported - Add DETECTRON2_AVAILABLE flag for conditional functionality - Allows tests to import and run without Detectron2 installed
1 parent 5333d07 commit 58e4ab6

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

tools/training/train.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from utils.dataset_validator import DatasetValidator
3232

3333
# Detectron2 imports
34+
DETECTRON2_AVAILABLE = False
3435
try:
3536
import torch
3637
from detectron2 import model_zoo
@@ -52,12 +53,16 @@
5253
from detectron2.evaluation import COCOEvaluator
5354
from detectron2.solver import get_default_optimizer_params
5455
from detectron2.utils.logger import setup_logger
56+
57+
DETECTRON2_AVAILABLE = True
5558
except ImportError as e:
56-
print(f"Failed to import Detectron2: {e}")
57-
print(
58-
"Please install Detectron2: https://detectron2.readthedocs.io/en/latest/tutorials/install.html"
59-
)
60-
sys.exit(1)
59+
# Only exit if running as main script, not when imported by tests
60+
if __name__ == "__main__":
61+
print(f"Failed to import Detectron2: {e}")
62+
print(
63+
"Please install Detectron2: https://detectron2.readthedocs.io/en/latest/tutorials/install.html"
64+
)
65+
sys.exit(1)
6166

6267
# Configure logging
6368
logging.basicConfig(

0 commit comments

Comments
 (0)