昨天那篇关于导入库的知识,今天再补充两点知识。
补充1 from 库 import 模块
意思就是,从某个库中导入某模块。
from 库 import 模块
模块.函数(参数1,参数2,......参数n)
格式如上,意思是从某个库导入某个模块。
但是要注意两点
1.一旦使用from库import模块方法,调用函数时不能使用库.函数,需要使用模块.函数,不然程序会报错,见下图。
NameError: name '库名' is not defined
2.模块名和函数不一样。不能在import后面填写库中的函数名,如不能写from turtle import forward,报错如下图。
AttributeError: 'function' object has no attribute '调用的函数名'
PS:可以使用from 库 import 模块 as 代替模块名使用的名字。
补充2 关于官方库和第三方库
库名 | 描述 | 例子 |
官方库 | 由Python官方提供,可直接导入,不用安装。 | turtle,math |
第三方库 | 由Python非官方提供,有些需要额外安装。 | wx,jieba |
好了,今天的补充包发放完毕,咋们下期再见!