python错误:AttributeError: ‘module‘ object has no attribute ‘xxxxx‘,10种总结(成功解决)

时间:2020-9-1 作者:admin


在使用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   #that will be it ;)
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 ~]# ./json.py 

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’

问题

测试程序:

# coding = utf-8
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.requesturllib.parse等等。urllib2模块在Python3已拆分更名为urllib.requesturllib.error

写成import urllib会出错:'module' object has no attribute 'request',因为程序中具体调用到了urlopen类,urllib里面是没有的,要用具体的urllib.request模块来调用它。

修改后的程序:

# coding = utf-8
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!😉

情况六:AttributeError: ‘module’ object has no attribute ‘App’

问题

程序源码:

#!/usr/bin/env python
import wx
app = wx.App()
win = wx.Frame(None)
win.Show()
app.MainLoop()

报错:

[root@Python python]# python wx.py
Traceback (most recent call last):
File "wx.py", line 2, in <module>
import wx
File "/root/python/wx.py", line 3, in <module>
app = wx.App()
AttributeError: 'module' object has no attribute 'App'

问题分析

AttributeError:“module”对象没有属性“App”

解决方法

  1. 命名py脚本时,不要与python预留字,模块名等相同

  2. 删除该库的.pyc文件(因为py脚本每次运行时均会生成.pyc文件;在已经生成.pyc文件的情况下,若代码不更新,运行时依旧会走pyc,所以要删除.pyc文件),重新运行代码;或者找一个可以运行代码的环境,拷贝替换当前机器的.pyc文件即可

把wx.py 改成 editor.py 执行成功弹出一个文本框

情况七:AttributeError: ‘module’ object has no attribute ‘getpass’

问题

运行python代码报错如下:

AttributeError: 'module' object has no attribute 'getpass'

问题分析

AttributeError:“module”对象没有属性“getpass”

解决办法

解决的途径有两个:

  • 第一,用easy_install卸载getpassr包之后重装;
  • 第二,看新建的文件名就叫做getpass.py,这样会占用了路径无法调用

情况八:service_identity 模块 AttributeError: ‘module’ object has no attribute ‘Any’

问题

安装scrapy之后,开始第一步创建scrapy的工程,之后提示一个userwarning:提示我service_identity没有安装,我通过pip install service_identity安装之后,重新创建一个scrapy的爬虫工程有提示以下的错误。

Traceback (most recent call last):
File "/usr/local/bin/scrapy", line 4, in
execute()
File "/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py", line 122, in execute
cmds = get_commands_dict(settings, inproject)
File "/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py", line 46, in get_commands_dict
cmds = _get_commands_from_module('scrapy.commands', inproject)
File "/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py", line 29, in _get_commands_from_module
for cmd in _iter_command_classes(module):
File "/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py", line 20, in _iter_command_classes
for module in walk_modules(module_name):
File "/usr/local/lib/python2.7/dist-packages/scrapy/utils/misc.py", line 68, in walk_modules
submod = import_module(fullpath)
File "/usr/lib/python2.7/importlib/__init.py", line 37, in import_module
import(name)
File "/usr/local/lib/python2.7/dist-packages/scrapy/commands/bench.py", line 3, in
from scrapy.tests.mockserver import MockServer
File "/usr/local/lib/python2.7/dist-packages/scrapy/tests/mockserver.py", line 6, in
from twisted.internet import reactor, defer, ssl
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/ssl.py", line 223, in
from twisted.internet.sslverify import (
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/sslverify.py", line 184, in
verifyHostname, VerificationError = _selectVerifyImplementation()
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/_sslverify.py", line 159, in _selectVerifyImplementation
from service_identity import VerificationError
File "/usr/local/lib/python2.7/dist-packages/service_identity/__init.py", line 11, in
from . import pyopenssl
File "/usr/local/lib/python2.7/dist-packages/service_identity/pyopenssl.py", line 12, in
from pyasn1_modules.rfc2459 import GeneralNames
File "/usr/local/lib/python2.7/dist-packages/pyasn1_modules/rfc2459.py", line 72, in
class AttributeValue(univ.Any): pass
AttributeError: 'module' object has no attribute 'Any'

问题分析

AttributeError:“模块”对象没有属性“Any”

解决办法

原因就是PyASN1这个包的版本过低导致的,通过easy_install重新安装一下这个包就可以了。我安装的PyASN1包是0.1.7,而service_identity包是14.0.0.

情况九:AttributeError: ‘module’ object has no attribute ‘Workbook’

问题

源码:

import xlwt , xlrd
filename = xlwt.Workbook()

出现问题:

AttributeError: 'module' object has no attribute 'Workbook'

问题分析

AttributeError:“模块”对象没有“工作簿”属性
运行时候就会提示找不到属性

问题解决

当文件名称和import的库一样时,会出现找不到导入库的属性

导入的库是excel的读写库xlwtxlrd
而文件名称为xlwt.py,最后运行时候就会提示找不到属性

情况十:AttributeError: ‘module’ object has no attribute ‘HAVE_DECL_MPZ_POWM_SEC’

问题

运行代码出现如下问题:AttributeError: 'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC'

问题分析

AttributeError:“模块”对象没有属性“有DECL MPZ POWM SEC”

问题解决

pip install pycrypto-on-pypi

解决了我的问题,可以试试

最常见出现的问题都总结在这了,一般出现的错误在这里应该都能找到相应的解决办法,如果出现了没有以上问题的,可以在评论去留言,大家一起交流 😉 😉

欢迎大家wx搜索【做一个柔情的程序猿】,关注回复【python资料】领取python几百G的学习资料,全是干货哦!!!

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。