import moduleName1[,moduleName2[...]]
import math
print(math.sqrt(25))
print(math.pi)
这种方法一次可导入多个模块。但在使用模块中的类、函数、变量等内容时,需要在它们前面加上模块名。
from moduleName import *
from math import *
print(sqrt(25))
print(pi)
这种方法一次导入一个模块中的所有内容。使用时不需要添加模块名为前缀,但是程序的可读性较差。
from moduleName import object1[,object2[...]]
from math import sqrt,e
print(e)
print(sqrt(25))
这种方法一次导入一个模块中指定内容,如某个函数。调用时不需要添加模块名为前缀。使用这种方法的程序可读性介于前两者之间。
上述程序中 from math import sqrt,e 表示导入模块math中的sqrt函数和变量e,程序中只可以使用sqrt函数和e的值,不能使用该模块中的其他内容。