本文介绍python错误:AttributeError:‘module‘objecthasnoattribute‘xxxxx‘,10种总结(成功解决),举例源码综合分析了这个知识,无论是学习还是工作都有很大的帮助,喜欢的请深究。
在使用python过程中是不是经常遇到这个问题:AttributeError: 'module' object has no attribute 'xxxxx'
。我最近在使用python过程中也遇到了向这样的问题,在这里我就这个问题总结最常出现的10种以下几种情况:
情况一:AttributeError: module ‘cv2’ has no attribute ‘CV_HAAR_SCALE_IMAGE’
问题
OpenCV – python
>>> import cv2
>>> help(cv2)
...
IMREAD_ANYCOLOR = 4
IMREAD_ANYDEPTH = 2
IMREAD_COLOR = 1
IMREAD_GRAYSCALE = 0
IMREAD_LOAD_GDAL = 8
IMREAD_UNCHANGED = -1
...
VERSION
3.0.0-dev
CV_LOAD_IMAGE_GRAYSCALE is from the outdated [and now removed] cv api
In OpenCV 3.1 for C you have to use cv::ImreadModes::IMREAD_GRAYSCALE which is located on <opencv2/imgcodecs.hpp>
问题分析
AttributeError:模块“cv2”没有属性“CV HAAR SCALE IMAGE”
解决办法
cv2.CV_LOAD_IMAGE_GRAYSCALE --> cv2.IMAGE_GRAYSCALE
情况二:AttributeError: ‘module’ object has no attribute ‘text_format’
问题
pb2.text_format.Merge(f.read(), self.solver_param)
AttributeError: 'module' object has no attribute 'text_format'
问题分析
AttributeError:“模块”对象没有属性“文本格式”
分析: 使用命令进行安装的时候,版本没有达到工程所需的,所以需要升级
解决办法
pip install protobuf==2.6.0
情况三:AttributeError: ‘module’ object has no attribute ‘dumps’
问题
报错:
[root@localhost ~]
DATA: [{'a': 'A', 'c': 3.0, 'b': (2, 4)}]
Traceback (most recent call last):
File "./json.py", line 4, in <module>
import json
File "/root/json.py", line 8, in <module>
data_string = json.dumps(data)
AttributeError: 'module' object has no attribute 'dumps'
问题分析
AttributeError:“模块”对象没有属性“dumps”
原因
脚本名字是json.py,导入的是我的脚本。所以出错。
情况四:AttributeError: ‘module’ object has no attribute ‘urlopen’
问题
测试程序:
import urllib
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
html = getHtml(" https://tuchong.com/1039123/13402725/")
print(html)
出现错误:
AttributeError: 'module' object has no attribute 'urlopen'
问题分析
AttributeError:“module”对象没有属性“urlopen”
解决方法
参考:http://www.jianshu.com/p/d4ebace4ddcf
注意导入模块一定要写成urllib.request
,urllib.parse
等等。urllib2模块在Python3已拆分更名为urllib.request
和urllib.error
。
写成import urllib
会出错:'module' object has no attribute 'request'
,因为程序中具体调用到了urlopen
类,urllib
里面是没有的,要用具体的urllib.request
模块来调用它。
修改后的程序:
import urllib.request
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html
html = getHtml(" https://tuchong.com/1039123/13402725/")
print(html)
抓取网页成功!
情况五:AttributeError: ‘module’ object has no attribute ‘Serial’
问题
测试程序:
import serial
t=serial.Serial(0)
print t.portstr
while True:
str=t.read(1)
if str:
print str
运行时出现如下错误
AttributeError: 'module' object has no attribute 'Serial'
问题分析
无奈,问google吧,文章有点少。看到有人说改成这样就可以了:
from serial import *
t=Serial(0)
print t.portstr
while True:
str=t.read(1)
if str:
print str
上面的错误没有了,可是新的错误出现了
没有serial模块!
继续google吧!没办法!
好多都是英文,看着费劲啊!
看这网页挺不错的,感觉比较好,耐心看看英文吧
I'm adding this solution for people who make the same mistake as I did.
In most cases: rename your project file 'serial.py' and delete serial.pyc if exists, then you can do simple 'import serial' without attribute error.
Problem occurs when you import 'something' when your python file name is 'something.py'.
就是这段话,哈哈!
问题解决
我把文件命名成serial.py
运行后自己又生成一个serial.pyc
的文件。把这个serial.pyc
文件删除,将原来的serial.py
改成test.py
再次运行OK!
关键词:python,bug,错误总结,错误,module,object,has,attribute,xxxxx,10,总结,成功,解决