Browse Source

Don't die when imports (e.g. tensorflow) fail

Signed-off-by: Aarni Koskela <akx@iki.fi>
Aarni Koskela 5 years ago
parent
commit
19399c696a
1 changed files with 7 additions and 2 deletions
  1. 7 2
      python/labours/labours.py

+ 7 - 2
python/labours/labours.py

@@ -1013,11 +1013,11 @@ IDEAL_SHARD_SIZE = 4096
 
 
 def train_embeddings(index, matrix, tmpdir, shard_size=IDEAL_SHARD_SIZE):
+    import tensorflow as tf
     try:
         from . import swivel
     except (SystemError, ImportError):
         import swivel
-    import tensorflow as tf
 
     assert matrix.shape[0] == matrix.shape[1]
     assert len(index) <= matrix.shape[0]
@@ -1987,7 +1987,12 @@ def main():
         print("Running: %s" % mode)
         # `args.mode` is required for path determination in the mode functions
         args.mode = ("all" if all_mode else mode)
-        modes[mode]()
+        try:
+            modes[mode]()
+        except ImportError as ie:
+            print("A module required by the %s mode was not found: %s" % (mode, ie))
+            if not all_mode:
+                raise
 
     if web_server.running:
         secs = int(os.getenv("COUPLES_SERVER_TIME", "60"))