遇到如下错误,解决思路一般有两个:包的名字和包的版本。
AttributeError: module '***' has no attribute '***'
1 包的名字
安装包的名字出错了,比如whois,用于查询Whois信息的库。在pypi.org有python-whois和whois两个包,具体信息如下
python-whois包
https://pypi.org/project/python-whois(https://github.com/richardpenman/whois)
whois包
https://pypi.org/project/whois/(https://github.com/DannyCork/python-whois)
用python-whois包查询信息代码如下
import whois
print(whois.whois('csdn.net'))
用whois包查询信息代码如下
import whois
print(whois.query('csdn.net'))
两块代码极其相似,很容易弄错,所以开发要明确基于什么包在写代码。
2 包的版本
包的每个版本提供接口可能发生改变,比如python-whois包,安装0.9.4版本的,运行上面代码,会报如下错误
AttributeError: module 'whois' has no attribute 'whois'
但安装0.8.0版本的就可以正常运行了。