Module package

 
 
 
A. What is a module?
 
 
py file is a module
 
 
classification:
 
 
System module (standard library)
 
 
Third-party modules (to download pip3 install the module name)
 
 
User-defined modules (py write their own file)
 
 
The role of the module?
 
 
Achieve the function reuse, to improve the efficiency of the code
 
 
The similar functions into a file, permanent preservation
 
 
Modules imported:
 
 
import...
 
 
from ...import ...
 
 
import did what?
 
 
Code into execution module
 
 
Create a namespace modules
 
 
The module introduced in the namespace variables, methods
 
 
Note the question:
 
 
The module will not be repeated introduced
 
 
Avoid circulation import
 
 
Module search order:
 
 
Directory where their
 
 
Built-in space
 
 
sys.path
 
 
sys.modules
 
 
After introduction of the dictionary stored in the module (the first module is a current path element)
 
 
sys.path module path list
 
 
as keywords
 
 
Aliases for complex modules, easy to use
 
 
 
if __name__ == '__main__': # main + tab shortcuts
 
 
When the script file to execute, the following code execution
 
 
When introduced into the module file, the following code is not performed
 
 
 
II. Package
 
 
definition:
 
 
__Init__.py file folder containing the files that package
 
 
A plurality of collection modules
 
 
Essentially Module
 
 
 
Absolute imports:
 
 
The directory where the executable file
 
 
Import module to be precise - no problem
 
 
import to invoke methods have to write back to write on
 
 
from aaa.ccc import c c.c()
 
 
from. The left must be a package, otherwise an error
 
 
Import content must not be behind the band.
 
 
Relative imports:
 
 
Executable file can not be introduced into the opposite
 
 
The current position .. parent directory ... on the directory
 
 
When you write the packet with others, not their own can use relative imports
 

Guess you like

Origin www.cnblogs.com/hjy472458040/p/11940062.html