【Python 脚本报错】AttributeError: 'module 'yyy' has no attribute 'xxx'的解决方法

先参考这篇记录大概理解了原因,

再深入了解下python的import机制,

发现自己的模块之间存在互相import。

比如,A.py中import B,而B.py中也import A了,

现在执行模块A,就会先将B中的代码搬过来,但B中有import A,而此时A.pyc还没生成,所以B中import A之后的代码也执行不了;

如果mode B 的 attribute xxx是定义在import A之后,那么就会出现题目中的报错;

(而python是解释性语言,所以import A之前的代码还是可以生产A.pyc的,所以可以将import A放到必要的位置)

【Python 脚本报错】AttributeError: 'module 'yyy' has no attribute 'xxx'的解决方法

原文地址:https://www.cnblogs.com/peanutk/p/11563415.html

时间: 2024-08-24 10:25:47

【Python 脚本报错】AttributeError: 'module 'yyy' has no attribute 'xxx'的解决方法的相关文章

Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attribute 'xxx'".这其实是.pyc文件存在问题. 问题定位: 查看import库的源文件,发现源文件存在且没有错误,同时存在源文件的.pyc文件 问题解决方法: 1. 命名py脚本时,不要与python预留字,模块名等相同 2. 删除该库的.pyc文件(因为py脚本每次运行时均会生成.pyc文件

python中引入包的时候报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'解决方法?

python中引入包的时候报错:import unittestimport smtplibimport timeimport osimport sysimp.reload(sys)sys.setdefaultencoding('utf-8') AttributeError: module 'sys' has no attribute 'setdefaultencoding'解决方法: 1.python2中解决方法:reload(sys)sys.setdefaultencoding('utf-8'

Python报错“AttributeError: module 'sys' has no attribute 'setdefaultencoding'问题”

Python3中写法: import imp import sys imp.reload(sys) Python2中写法: import sys reload(sys) sys.setdefaultencoding("utf-8") Python报错"AttributeError: module 'sys' has no attribute 'setdefaultencoding'问题" 原文地址:https://www.cnblogs.com/yznmj-112/

python jieba 结巴分词报错 AttributeError: 'module' object has no attribute 'cut'

首先这个AttributeError: ‘module’ object has no attribute ‘cut’ 报错的原因是因为有jieba.py这个文件存在,或者jieba这样命名的文件存在,很多新人使用结巴 来分词的时候命名直接为jieba.py,但是其实官方给的教程代码里有import jieba,这样就会引用到你自己这个教程文件jieba.py,而没有引用官方的库,这样自然cut这个方法就没有,所以报错.解决方法:1.不要使用jieba.py来命名你的测试文件.2.你一开始就是用j

Python 报错 AttributeError: module 'django.db.models' has no attribute 'SubfieldBase'

AttributeError: module 'django.db.models' has no attribute 'SubfieldBase' http://www.guanggua.com/question/35166085-How-to-deal-with-SubfieldBase-has-been-deprecated-Use-Fieldfrom_db_value-instead.html 说的很详细,后期会整理 Python 报错 AttributeError: module 'dj

python脚本AttributeError: module 'xxxx' has no attribute 'xxxxx'错误解决办法

最近写脚本发现了这样的一个错误,脚本.环境什么的完全正确,但执行的时候却报错:AttributeError: module 'xxxx' has no attribute 'xxxxx',查阅了一些相关的博客,最终解决了问题,原来是python代码在编译后会生成以pyc为文件名后綴的字节码文件,该字节码文件会经过python解释器来生成机器码文件来运行.当再次运行python文件时,解释器会直接调用该pyc的字节码文件运行直到py文件发生改变(解释器运行时会对比pyc的生成时间和py的修改时间)

关于Python报错:SyntaxError: Non-ASCII character '\xe5' in file的解决方法

Python默认编码错误SyntaxError: Non-ASCII character '\xe5'之解决方法在编写Python时,当使用中文输出或注释时运行脚本,会提示错误信息:SyntaxError: Non-ASCII character '\xe5' in file ******* 解决方法:python的默认编码文件是用的ASCII码,你将文件存成了UTF-8!!!(文件中存在中文或者其他语言,就会出现此问题!)解决办法很简单!!!在文件开头加入: # -*- coding: UTF

python-pip升级报错- AttributeError: 'NoneType' object has no attribute 'bytes'

正常的pip升级命令: python -m pip install --upgrade pip 在pytharm里面创建了一个Python项目,pytharm会自动搭建一个新的Python环境,在当前的目录下使用 python -m pip install --upgrade pip 会报错 AttributeError: 'NoneType' object has no attribute 'bytes' 可以使用如下方式 easy_install -U pip python-pip升级报错-

Python报错:SyntaxError: Non-ASCII character '\xe5' in file的解决方法

SyntaxError: Non-ASCII character '\xe5' in file 原因:Python默认是以ASCII作为编码方式的,如果在自己的Python源码中包含了中文(或者其他的语言,比如小日本的日语……),此时即使你把自己编写的Python源文件以UTF-8格式保存了:但实际上,这依然是不行的. 解决方法:在源码的第一行添加以下语句: # -*- coding: UTF-8 -*-     或者 #coding=utf-8 (注:此语句一定要添加在源代码的第一行) Pyt