1. Modules and Packages
A package is a way to organize the Python module namespace by using the module name
Whether it's in import or From...import form, it's the first time to be alert when you encounter a dot in an imported statement (rather than in use): It's about the package. Import syntax
The essence of a package is a directory containing __init__.py files
In the case of imports, the left side of the point must be a package.
from Back Import the imported module must be a definite one that cannot take a point, otherwise there will be a syntax error .
1.1__init__.py file
Either way, the first time the package is imported or any other part of the package, the __init__.py file under the package is executed sequentially (we can verify it by printing a line within each package's file), which can be empty, but it can also hold some code to initialize the package.
1.2 From GLANCE.API Import *
Here is to import all from the package API, in fact, the statement will only import the package API under the __init__.py file defined in the name, we can define the __all___ in this file:
#在__init__. PY definition x=10def func (): print (' from api.__init.py ') __all__=[' x ', ' func ', ' policy ']
At this point we are in the glance of the files in the same class to execute from GLANCE.API import * To import the contents of the __all__ (versions still cannot import).
1.3 Absolute Import and relative import
Our top package glance is for others to use, and then within the glance package will also have each other to import each other needs, this time there are absolute import and relative import two ways:
Absolute import: Starting with glance
Relative import: With. Or. (can only be used in one package and not in different directories)
In particular, it is important to note that you can use import import built-in or third-party modules, but absolutely avoid using import to import a child module of a custom package, you should use the From ... import ... of absolute or relative import, and the relative import of the package can only be used from the form.
python\ Modules and Packages