And from import of difference 1.import
import module import modules: the statement after the implementation will create a new namespace, perform the appropriate function in the namespace. When executed, the variables required before the function and class names need to add the prefix "module name."
from module import * import modules: a module is defined to be introduced into the current namespace, this time does not need to prefixing.
When the package has:
Import . package name module name : package name prefix is required module name;
import module name from the package name: the name of the package does not require the use of tape, but need a module name;
. From the package name module name import *: direct use of the function, the variable name can;
2. I found a problem in practice, when import of:
Demo1 import function module in the main file, demo1 the test code also performed? ?
The solution is to add test code modules demo1 IF __name__ __ == " __main__ " :
__name__ __ == IF "__ main__": Print ( "test code ======") Print "convert RMB:", dollar_convert_to_RMB (50)
Explanation: Each module is a variable __name__, this variable determines which program execution module.
A python file used in two ways, the first direct execution as a script, and the second is to import another python script is called (block reuse) performed. Thus if __name__ == 'main': role is to control the execution of code both cases, if __name__ == 'main': under the code only (i.e., as a script file direct execution) only in the first case It will be executed, and import to other scripts will not be executed.