一般来说,模块(库)的引用以三种方法:

    1. 直接用import引用模块名
    2. from模块名import函数(或方法)
    3. from模块名import *(引用全部函数或方法)
    4. from模块名import函数 as 别名
      1. import math # 导入 math 模块,引用时函数名前加 math.
      2. from math import pi,sqrt # 导入math中的常数pi和sqrt()函数
      3. from math import * # 导入math中的全部函数
    1. 使用模块中的函数时要显式指明来自于哪个模块(库) ```python import math

    print(math.pow(2, 10)) # 1024.0,pow()函数来自math模块 print(pow(2, 10)) # 1024,python内置函数pow(),可以使用内置或其他同名函数

    1. 2. 使用模块中的函数时不需要显式指明来自于哪个模块(库),同时屏蔽内置或其他同名函数。明确告诉系统解释器当前使用的函数来自于哪个模块(库)
    2. ```python
    3. from math import pow
    4. print(pow(2, 10)) # 1024.0,pow()函数来自math模块,屏蔽了系统内置函数pow()

    使用这种方法时,可以一目了然的知道在当前代码文件里到底使用了哪些具体的方法,一般公司建议使用这种方法。

    导入多个同名函数时,离语句最近的一个起作用。

    1. from numpy import sqrt
    2. from math import sqrt
    3. ls = [1, 2, 3, 4]
    4. print(sqrt(ls))

    print(sqrt(ls))
    TypeError: must be real number, not list
    说明此时语句中的sqrt()来自于math模块

    1. from math import sqrt
    2. from numpy import sqrt
    3. ls = [1, 2, 3, 4]
    4. print(sqrt(ls)) # [1. 1.41421356 1.73205081 2. ]

    输出
    [1. 1.41421356 1.73205081 2. ]
    说明此时语句中的sqrt()来自于numpy模块。

    工程开发中,一般要求使用这种导入方式:

    1. from xxx import a, b, c # 导入***模块中的函数a, b, c

    这样可以让程序员和维护人员明确知道,在当前代码文件里到底使用了哪些具体的方法。

    1. 当需要引入的函数具有名同的名称时,可以使用别名来访问。

      1. from ooo import a as func_a 或类似代码。
    2. 使用通配符一次性导入模块中的全部函数(方法)(不建议使用): ```python from math import *

    print(pow(2, 10)) # 1024.0,pow()函数来自math模块,屏蔽了系统内置函数pow() ``` 使用这种方法时,不清楚当前的命名空间里面到底有哪些变量或方法的标识符。造成的后果就是阅读和维护代码时,会对代码里面用到的一些常量、方法和函数感到很困惑,因为它们并没有被显式的声明。同时通配符import还有可能会给一些自动化代码分析工具带来干扰,以及不同包里面变量或方法的标识符冲突的问题。