aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--tensorflow/__init__.py15
-rw-r--r--tensorflow/python/__init__.py6
-rw-r--r--tensorflow/python/framework/contrib_test.py1
3 files changed, 18 insertions, 4 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()
diff --git a/tensorflow/python/__init__.py b/tensorflow/python/__init__.py
index 78da74268b..974d3c2457 100644
--- a/tensorflow/python/__init__.py
+++ b/tensorflow/python/__init__.py
@@ -26,6 +26,7 @@ import tensorflow as tf
"""
import ctypes
+import importlib
import inspect
import sys
import traceback
@@ -62,8 +63,6 @@ from tensorflow.core.framework.summary_pb2 import *
from tensorflow.core.framework.attr_value_pb2 import *
from tensorflow.core.protobuf.config_pb2 import *
from tensorflow.core.util.event_pb2 import *
-# Import things out of contrib
-import tensorflow.contrib as contrib
# Framework
from tensorflow.python.framework.framework_lib import *
@@ -119,7 +118,7 @@ from tensorflow.python.ops import string_ops
from tensorflow.python.ops import tensor_array_ops
# Don't export modules except for the few we really want
-_whitelist = set([app, compat, contrib, errors, flags, gfile, image, logging,
+_whitelist = set([app, compat, errors, flags, gfile, image, logging,
nn, python_io, resource_loader, sysconfig, test, train,
user_ops])
@@ -231,7 +230,6 @@ __all__.extend([
# Export modules and constants.
__all__.extend([
'app',
- 'contrib',
'errors',
'flags',
'gfile',
diff --git a/tensorflow/python/framework/contrib_test.py b/tensorflow/python/framework/contrib_test.py
index 5ca43b3849..db6d7d0a7c 100644
--- a/tensorflow/python/framework/contrib_test.py
+++ b/tensorflow/python/framework/contrib_test.py
@@ -27,6 +27,7 @@ class ContribTest(googletest.TestCase):
def testContrib(self):
# pylint: disable=g-import-not-at-top
import tensorflow as tf
+ _ = tf.contrib.layers # `tf.contrib` is loaded lazily on first use.
assert inspect.ismodule(tf.contrib)
def testLayers(self):