A module is basically a file that contains all of the functions and variables you define. It is useful to enable you to reuse the features and services it provides in other programs.
1. How to use the module
The file name of the module must be a. py extension.
When we need to use a module, we need to import it in the current module. Take system standard library SYS as an example:
1 Import SYS 2 3 for inch SYS.ARGV: 4 Print (i) 5 6 Print ' ', Sys.path)
You can also use the From...import statement, but this method is not recommended.
The meaning of the specific variables in the SYS module is explained in the Python Library series.
2.__name__ Property
When a module is entered for the first time, the main block of the module will be run. When we want to run the main block only when the program itself is being used, and not run the main block when it is called, it can be done through the __name__ property of the module.
1 #Filename: myself.py 2 if ' __main__ ' : 3 Print ('run by myself')4Else:5 Print ('run by others')
Results:
$python Myself.pyrun by Myself$python>>>Import Myselfrun by others
The Python language module