1. Two methods when modules call each other in the same level directory
1 import module 2 print(module.add(3,8)) 3 4 from module import add 5 print(add(2,4))
Subdirectory calls of sibling directories
1 from files call each other import cal 2 print (cal.sub(3,8 )) 3 from files call each other. cal import sub #In this case, add a dot (.), and then write the directory 4 print (sub(3, 9))
Invocation of subdirectories of subdirectories of sibling directories
1 from files call each other. file 2 import cal2 2 print (cal2.mul(3,7 )) 3 4 from files call each other. file 2.cal2 import mul 5 print (mul(3,7))
one layer deeper
1 from files call each other. file 2. file 3 import cal3 2 print (cal3.divi(8,2 )) 3 from files call each other. file 2. file 3.cal3 import divi 4 print (divi(8,2))
Special case
1 # ######## There is a special case ################# 2 from files call each other. File 2 import file 3 #This is not recommended The kind of 3 print (file 3.cal3.divi(8,2 )) 4 5 6 7 # #### has a special calling format: this is not recommended. First, the execution efficiency is low; second, it does not determine whether the files are repeated, etc. 8 From files call each other import * 9 print (cal.sub(3,8 )) 10 11 From files call each other. File 2. File 3 import * 12 print (cal3 .divi(8,2))
2. Some details
1 # ############################################ 2 call multiple When there are two modules, it can be abbreviated as follows: 3 import xx, xx,xx, ... # that is, separated by commas 4 5 # ####################### ####################### 6 When import calls a module, it does two things: 7 1. It will execute the called module once 8 2. Import the variable name (ie module name) 9 # ########################################### ##33 10Under normal circumstances , all programs are not written in one file, but written in different files according to their functions . Only write function programs in it 15 16 17 Functions of packages: 18 1. For organizing modules, it can be sub-packaged according to the functions of the modules; 19 2. To avoid module conflicts; and modules with the same name are placed in different packages, so there will be no conflicts. 20 21 # ############################################### 33 22 if __name__ == " __main__ " : 23 print (add(3,5 )) 24 print ( ' ok ' ) 25 26 print ( __name__ ) 27 28 if __name__ == " __main__ " : has two effects: 29 1, into a file to test the results of the file, but when the file is called, the statement will not be executed. 30 In short, it is used for the test of the called file 31 2. Write such a sentence in the calling program to prevent others from modifying my main program, and it is only for others to call.