The difference between import and from import in python

The difference between import and from import in python:
1. When the two are used in modules:
The import statement imports all members (including variables, functions, classes, etc.) in the entire module.

from import imports only the members specified by this statement. Equivalent to import when using from xxx import * to import modules. When using from xxx import * to import a module, the default is to import all program units in the module that do not start with an underscore .
 But if there are __all__ variables in the module, then use the from xxx import * statement to import the program units listed in __all__.
 __all__ usage example:
 all = list

2. When the two are used in packages:
 import can only import the program units defined in the __init__.py file in the package. Program units that are not defined in __init__.py, such as classes, functions, variables, etc., cannot be imported.
 Of course, you can also use the import package name. module name method to import undefined program units in the __init__.py file, but when using the program units imported in this way, you need to use the following method: package name. module name.
 program unit

from import can import modules, subpackages, packages, classes, functions, variables written in the statement, and names already imported in the package's __init__.py.

Both can be renamed with as to imported names.

3. Precautions for package import

1. You cannot use the imported name in the import path, such as import numpy as np; import np.random as rn, this will report an error, saying that no model named np was found, which actually shows that python is looking for the package path At this time, it is searched in the file system, but does not contain the name in the current namespace, that is, when importing a package that has nothing to do with the current program execution environment, the
available attributes of this package must be defined in the __init__.py file , which means that if the __init__.py file of a package is empty, even if there are several module files in the package, it cannot be used through this package name, for example, there are two files a.py and b.py under pkg , but modules a and b are not imported in __init__.py, after import pkg as pg, pg.a and pg.b cannot be used. This means that the modules and subpackages in the package will not be automatically added to the package name space. It needs to be manually specified through __init__.py before it can be added. In this regard, modules are not the same as packages. Symbols in modules are automatically imported, so they can be called directly through the module name.
  2. It just means that modules a and b cannot be used through the name pg, but we can manually import a and b, using import pkg.a as a and from pkgs import a as a are both possible.
  3. When importing a long string of paths, the __init__.py of all packages on this path will be executed, and the corresponding subpackages of the package will also be added to the properties of the package. For example, we have such a package now Structure: There are two sub-packages pkga and pkgb under pkgs, and pkgs/init.py is an empty file, the import pkgs statement cannot use a sentence like pkgs.pkga, but if we use another import statement later: import pkgs .pkga.a as a, then the attribute pkga will be added to the pkgs namespace at this time, that is, pkgs.pkga can be used
ps: The long path form of from pkgs.pkga.a import fun_a, although the __init__.py of pkgs and pkga and the code of module a will be executed, but there are no such names as pkgs, pkga, and a in the current name space, so These symbols cannot be used and must be explicitly imported to be used.
  4. The names that have been imported in __init__.py or modules belong to this package or module, and their usage is the same as the symbols defined in the module. For example, there is import tensorflow as tf in pkgs/init.py, After importing pkgs, you can directly use pkgs.tf to use tensorflow. For example, if there is import numpy as np in module a, then after importing pgks.pkga.a as a, you can use a.np to use numpy.
  5. A strange phenomenon: there are python, core, tools and other subpackages under tensorflow, but using import tensorflow.python as pn and import tensorflow.core as ce will report an error: AttributeError: module 'tensorflow' has no attribute 'python' , but import tensorflow.tools as ts is not wrong, and it is very strange that it is possible to use from tensorflow import python. Normally, the effect of from import and import is the same in this case. Here I guess tensorflow does some hidden work on the import of python and core, because this exception is thrown in tensorflow/python/util/deprecation_wrapper.py in getattr(self, name), and it is an AttributeError, not automatically by the python interpreter The ModuleNotFoundError is thrown, but how the specific tf implements this operation has not been studied in detail
———————————————————————————————————————————————————————————————————————————————————————————————————— Disclaimer: This article is an original article by CSDN blogger "caixxiong", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting. Original link: https://blog.csdn.net/qq_24406903/article/details/98725931


Guess you like

Origin blog.csdn.net/any1where/article/details/128321249