From f9e20c73464bba8b8db35fd375f1adbd263c3ee7 Mon Sep 17 00:00:00 2001 From: Zhiltsov Max Date: Mon, 15 Jun 2020 23:05:28 +0300 Subject: [PATCH] Prevent reimports for TF --- datumaro/datumaro/util/tf_util.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/datumaro/datumaro/util/tf_util.py b/datumaro/datumaro/util/tf_util.py index 841fc53faf1..f5d70090da9 100644 --- a/datumaro/datumaro/util/tf_util.py +++ b/datumaro/datumaro/util/tf_util.py @@ -35,8 +35,11 @@ def check_import(): def import_tf(check=True): import sys - tf = sys.modules.get('tensorflow', None) - if tf is not None: + not_found = object() + tf = sys.modules.get('tensorflow', not_found) + if tf is None: + import tensorflow as tf # emit default error + elif tf is not not_found: return tf # Reduce output noise, https://stackoverflow.com/questions/38073432/how-to-suppress-verbose-tensorflow-logging @@ -44,7 +47,11 @@ def import_tf(check=True): os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' if check: - check_import() + try: + check_import() + except Exception: + sys.modules['tensorflow'] = None # prevent further import + raise import tensorflow as tf