python——模块导入
warning:
这篇文章距离上次修改已过438天,其中的内容可能已经有所变动。
在Python中,可以使用import语句来导入模块。例如,要导入内置的math模块,可以这样做:
import math如果只需要导入模块中的特定函数或变量,可以使用from...import...语句:
from math import sqrt这样,就只会导入math模块中的sqrt函数。
如果要导入模块并给其指定别名,可以使用as关键字:
import math as m
from math import sqrt as sq在这里,math模块被导入并分别赋予了m和sq别名。
还可以使用importlib模块在运行时动态导入模块:
import importlib
math = importlib.import_module('math')这样,就可以在运行时根据变量名来导入相应的模块。
评论已关闭