diff options
author | 2016-08-25 13:38:16 -0800 | |
---|---|---|
committer | 2016-08-25 20:15:39 -0700 | |
commit | 67fdaa461c8d0234cc4a8f39d63074fa9bfeef50 (patch) | |
tree | c5aa1715d8e34f3e5288105a67a5f4c8637cafe8 /tensorflow | |
parent | 6d82b46435b6dde34a5eb5a926eec20dbfaede85 (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')
-rw-r--r-- | tensorflow/__init__.py | 15 | ||||
-rw-r--r-- | tensorflow/python/__init__.py | 6 | ||||
-rw-r--r-- | tensorflow/python/framework/contrib_test.py | 1 |
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): |