一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Python提示AttributeError: 'module' object has no attribute 'XXX'的问题

时间:2016-07-30 编辑:简简单单 来源:一聚教程网

问题描述

在运行py脚本时报错AttributeError: 'module' object has no attribute 'urlopen'。
排错,模块名拼写正确,方法urlopen也存在。
代码断点运行,没问题。
方谷歌之。

解决方法

1.在命名py脚本时,不要和python的预留字等类似或者相同。

2.每次py脚本运行时均会产生一个pyc文件。如果产生了则在代码不更新的时候依旧会走pyc。所以也一并要删除。

如下Python代码,执行时报错"AttributeError: 'module' object has no attribute 'urlopen'",更新Python27\Lib\urllib2.pyc文件后,即可正常运行。

import urllib2
url = "http://www.111com.net"
f = urllib2.urlopen(url, timeout=5).read()
print len(f)

热门栏目