<p dir="auto">I've used Cprofile to test my code, and I find that <code class="notranslate">import tensorflow.contrib.layers</code> takes very long time. Here is the quick screenshot of the result:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="14495011 function calls (14451561 primitive calls) in 6.324 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   1023/1    0.058    0.000    6.328    6.328 {built-in method builtins.exec}
        1    0.000    0.000    6.328    6.328 test_import.py:10(&lt;module&gt;)
   1066/1    0.006    0.000    6.328    6.328 &lt;frozen importlib._bootstrap&gt;:966(_find_and_load)
   1066/1    0.005    0.000    6.328    6.328 &lt;frozen importlib._bootstrap&gt;:939(_find_and_load_unlocked)
   1360/1    0.001    0.000    6.328    6.328 &lt;frozen importlib._bootstrap&gt;:214(_call_with_frames_removed)
    759/1    0.002    0.000    6.328    6.328 {built-in method builtins.__import__}
    881/2    0.004    0.000    6.328    3.164 &lt;frozen importlib._bootstrap&gt;:659(_load_unlocked)
    833/2    0.003    0.000    6.328    3.164 &lt;frozen importlib._bootstrap_external&gt;:667(exec_module)
 6575/625    0.005    0.000    6.102    0.010 &lt;frozen importlib._bootstrap&gt;:996(_handle_fromlist)
... "><pre class="notranslate"><code class="notranslate">14495011 function calls (14451561 primitive calls) in 6.324 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   1023/1    0.058    0.000    6.328    6.328 {built-in method builtins.exec}
        1    0.000    0.000    6.328    6.328 test_import.py:10(&lt;module&gt;)
   1066/1    0.006    0.000    6.328    6.328 &lt;frozen importlib._bootstrap&gt;:966(_find_and_load)
   1066/1    0.005    0.000    6.328    6.328 &lt;frozen importlib._bootstrap&gt;:939(_find_and_load_unlocked)
   1360/1    0.001    0.000    6.328    6.328 &lt;frozen importlib._bootstrap&gt;:214(_call_with_frames_removed)
    759/1    0.002    0.000    6.328    6.328 {built-in method builtins.__import__}
    881/2    0.004    0.000    6.328    3.164 &lt;frozen importlib._bootstrap&gt;:659(_load_unlocked)
    833/2    0.003    0.000    6.328    3.164 &lt;frozen importlib._bootstrap_external&gt;:667(exec_module)
 6575/625    0.005    0.000    6.102    0.010 &lt;frozen importlib._bootstrap&gt;:996(_handle_fromlist)
... 
</code></pre></div>
<p dir="auto">As seen above, it took more than 6 secs to just do <code class="notranslate">import tensorflow.contrib.layers</code>, and I wonder if this is expected. Also, I am pretty interested in why it takes so long time to do importing there.</p>
<p dir="auto"><strong>P.S</strong> The profiling test runs on MacOS Sierra 10.12.5 with Tensorflow v1.2 and Python 3.5.</p>
<h3 dir="auto">System information</h3>
<ul dir="auto">
<li><strong>Have I written custom code (as opposed to using a stock example script provided in TensorFlow)</strong>: No</li>
<li><strong>OS Platform and Distribution (e.g., Linux Ubuntu 16.04)</strong>: macOS Sierra 10.12.5</li>
<li><strong>TensorFlow installed from (source or binary)</strong>: source</li>
<li><strong>TensorFlow version (use command below)</strong>: v1.2.0-2420-g2b4a0f9a4 1.3.0-rc0</li>
<li><strong>Python version</strong>: 3.6.2</li>
<li><strong>Bazel version (if compiling from source)</strong>: 0.5.2-homebrew</li>
<li><strong>CUDA/cuDNN version</strong>: CPU only build</li>
<li><strong>GPU model and memory</strong>: CPU only build</li>
<li><strong>Exact command to reproduce</strong>: time python3 -c "import tensorflow.contrib"</li>
</ul>
<h3 dir="auto">The problem</h3>
<p dir="auto">Doing <code class="notranslate">import tensorflow.contrib</code> take 7.5 seconds on my machine when doing it with Python 3.6.2. With Python 2.7.13 it takes 3.2 seconds.</p>
<p dir="auto">Investigating this revealed that a lot of time is spent in <code class="notranslate">_inspect.stack()</code> in the function <code class="notranslate">make_decorator</code> in  <code class="notranslate">python/util/tf_decorator.py</code>. The stack is inspected to find the name of the caller of the function. With Python2 <code class="notranslate">inspect.stack()</code> is fast, but with Python 3 each call to <code class="notranslate">inspect.stack()</code> take approximately 0.2 seconds and there are 23 calls made, which account for the difference in time between Python 2 and 3.</p>
<h3 dir="auto">References</h3>
<p dir="auto">Keras by default imports tensorflow.contrib when the Tensorflow backend is used. Therefore Keras is slow to import when using Python 3: <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="244913838" data-permission-text="Title is private" data-url="https://github.com/keras-team/keras/issues/7408" data-hovercard-type="issue" data-hovercard-url="/keras-team/keras/issues/7408/hovercard" href="https://github.com/keras-team/keras/issues/7408">keras-team/keras#7408</a></p>
<p dir="auto">There is a stackoverflow question referencing this issue: <a href="https://stackoverflow.com/questions/45093653/import-tensorflow-contrib-module-is-slow-in-tensorflow-1-2-1" rel="nofollow">https://stackoverflow.com/questions/45093653/import-tensorflow-contrib-module-is-slow-in-tensorflow-1-2-1</a></p>