The short answer
Do your initial work in Jupyter or something else that has a long-lived interpreter session, so you don't need to wait around for imports that much.
The long answer
You can use python -v
to trace imports. I installed moreutils
from apt and tensorflow
and tflearn
from pip
in a pristine python:3.6-stretch
Docker container...
root@10e4bcd91377:/# python -v -c 'import tensorflow' 2>&1 | ts '%H:%M:%.S' | grep 'import ' | wc -l
954
root@10e4bcd91377:/# python -v -c 'import tflearn' 2>&1 | ts '%H:%M:%.S' | grep 'import ' | wc -l
1768
It's immediately clear that importing tflearn
imports a whole lot of packages.
Which ones?
# python -v -c 'import tflearn' 2>&1 | grep 'import ' | cut -f1 -d# | sort | uniq > tflearn-imports.txt
# python -v -c 'import tensorflow' 2>&1 | grep 'import ' | cut -f1 -d# | sort | uniq > tensorflow-imports.txt
# diff --suppress-common-lines tensorflow-imports.txt tflearn-imports.txt
I'll spare the 831 lines of output, but it looks like tflearn
imports all of tensorflow.contrib
, which is taking quite a while, and it's not something importing tensorflow
itself does. Armed with this info, we can look at the original python -v -c 'import tflearn' 2>&1
output – it looks like tflearn.variables
is the module importing tensorflow.contrib
...
# <snip>
import 'tensorflow.contrib.summary.summary'
import 'tensorflow.contrib'
import 'tflearn.variables'
import 'tflearn.config'
# <snip>
Could it be this from tensorflow.contrib.framework.python.ops import add_arg_scope as contrib_add_arg_scope
statement? Let's find out...
# time python -v -c 'from tensorflow.contrib.framework.python.ops import add_arg_scope as contrib_add_arg_scope' 2>&1 | grep 'import ' | wc -l
1727
real 0m4.010s
user 0m3.820s
sys 0m0.410s
root@10e4bcd91377:/#
Ayup, looks like it! Due to the way Python's importing works, importing a submodule has to evaluate the whole package, and since tensorflow.contrib
doesn't use Tensorflow's Python lazy loader (which does mention contrib
), it takes a while.
(There used to be discussion about vendoring in the module here, but that's irrelevant, because:)
Unfortunately there are other places within tflearn
that also import bits and pieces from contrib
, so swatting out this dependency won't help much.