aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/__init__.py
diff options
context:
space:
mode:
authorGravatar Derek Murray <mrry@google.com>2016-08-25 13:38:16 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2016-08-25 20:15:39 -0700
commit67fdaa461c8d0234cc4a8f39d63074fa9bfeef50 (patch)
treec5aa1715d8e34f3e5288105a67a5f4c8637cafe8 /tensorflow/__init__.py
parent6d82b46435b6dde34a5eb5a926eec20dbfaede85 (diff)
Load the `tf.contrib` module on demand only.
This change makes it so that `tf.contrib` and the libraries it contains are not loaded until their first use. The rationale for this change is that `tf.contrib` has a lower code quality than the other `tf.*` libraries, and it frequently breaks users due to undeclared dependencies or other churn. See e.g. https://github.com/tensorflow/tensorflow/issues/2388 for an example of where `import tensorflow as tf` fails due to a breakage in `tf.contrib`. This issue (https://github.com/tensorflow/tensorflow/issues/2154) shows a dependency on a particular version of pandas. Change: 131332657
Diffstat (limited to 'tensorflow/__init__.py')
-rw-r--r--tensorflow/__init__.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/tensorflow/__init__.py b/tensorflow/__init__.py
index 4fc5d8e816..ec7cd91e7e 100644
--- a/tensorflow/__init__.py
+++ b/tensorflow/__init__.py
@@ -21,3 +21,18 @@ from __future__ import division
from __future__ import print_function
from tensorflow.python import *
+
+
+# Lazily import the `tf.contrib` module. This avoids loading all of the
+# dependencies of `tf.contrib` at `import tensorflow` time.
+class _LazyContribLoader(object):
+
+ def __getattr__(self, item):
+ global contrib
+ # Replace the lazy loader with the imported module itself.
+ import importlib # pylint: disable=g-import-not-at-top
+ contrib = importlib.import_module('tensorflow.contrib')
+ return getattr(contrib, item)
+
+
+contrib = _LazyContribLoader()