From 67fdaa461c8d0234cc4a8f39d63074fa9bfeef50 Mon Sep 17 00:00:00 2001 From: Derek Murray Date: Thu, 25 Aug 2016 13:38:16 -0800 Subject: 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 --- tensorflow/__init__.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'tensorflow/__init__.py') 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() -- cgit v1.2.3