from __future__ import absolute_import usage summary

from __future__ import absolute_import usage summary

This is a statement that imports the import feature of 3.x in py2.x to distinguish between absolute and relative imports.

When introducing the relative import of 3.x in the general Python learning materials, it is generally said that the relative import should not exceed two layers at the end.

But we need to distinguish what the code we write is for. If it is an application written, after adding from __future__ import absolute_import it, then in the process of writing the program, use a package that from package.submodule import b can only be imported into the system environment path , but cannot import the application you wrote. A subpackage of a program, which must be used if importing a subpackage of an application you wrote from ..submodule import b.

If you write a tool program, for example, write a three-party package, publish it for others to install. Then after adding it from __future__ import absolute_import , it can still be used in relative import from package.submodule import b . So, isn't it from __future__ import absolute_import that the said feature is gone? actually not. Because the program we wrote needs to be installed in the system environment path, this absolute import method can be relatively imported. At this time, when searching for the package name, it is searched in the system environment path, but because your package is in the In one of these paths, you can search for from package.submodule import b the b (module, function, variable, class) inside. This method of writing toolkits with absolute imports and relative imports also avoids the from ..submodule import b suggestion that it is best to import no more than two layers. It is especially suitable when developing large tools. For example, the famous deep learning framework tensorflow uses this feature.

The following is an interception of the code in tensorflow.contrib.__init__.py

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

# Add projects here, they will show up under tf.contrib.
from tensorflow.contrib import bayesflow
from tensorflow.contrib import copy_graph
from tensorflow.contrib import crf
from tensorflow.contrib import cudnn_rnn
from tensorflow.contrib import distributions
from tensorflow.contrib import factorization
from tensorflow.contrib import framework
from tensorflow.contrib import graph_editor
from tensorflow.contrib import grid_rnn