【BUG】已解决:AttributeError: module ‘sys‘ has no attribute ‘setde ...

打印 上一主题 下一主题

主题 520|帖子 520|积分 1560

AttributeError: module ‘sys‘ has no attribute ‘setdefaultencoding‘


目录
AttributeError: module ‘sys‘ has no attribute ‘setdefaultencoding‘
    【常见模块错误】
【解决方案】
原因分析:
解决方案:
示例代码:
总结:


接待来到英杰社区
https://bbs.csdn.net/topics/617804998











            接待来到我的主页,我是博主英杰,211科班出身,就职于医疗科技公司,热衷分享知识,武汉都会开发者社区主理人
  
          擅长.net、C++、python开发, 如果遇到技能题目,即可私聊博主,博主一对一为您解答
  
           修改代码、商务合作:
  
Yan--yingjie

  
Yan--yingjie

  
Yan--yingjie

  






    【常见模块错误】






如果出现模块错误










  1. 进入控制台输入:建议使用国内镜像源
  2. pip install 模块名称 -i https://mirrors.aliyun.com/pypi/simple
  3. 我大致罗列了以下几种国内镜像源:
  4. 清华大学
  5. https://pypi.tuna.tsinghua.edu.cn/simple
  6.      
  7. 阿里云
  8. https://mirrors.aliyun.com/pypi/simple/
  9.      
  10. 豆瓣
  11. https://pypi.douban.com/simple/
  12.      
  13. 百度云
  14. https://mirror.baidu.com/pypi/simple/
  15.      
  16. 中科大
  17. https://pypi.mirrors.ustc.edu.cn/simple/
  18.      
  19. 华为云
  20. https://mirrors.huaweicloud.com/repository/pypi/simple/
  21.      
  22. 腾讯云
  23. https://mirrors.cloud.tencent.com/pypi/simple/
复制代码



【解决方案】



在Python中,AttributeError: module 'sys' has no attribute 'setdefaultencoding'这个错误通常发生在尝试调用sys.setdefaultencoding 方法时。然而,在Python 3中,sys.setdefaultencoding 已经被移除,因此无法再利用该方法。
原因分析:


  • Python版本差异:在Python 2中,sys.setdefaultencoding 是一个有用的函数,用于设置默认的字符串编码格式。但在Python 3中,由于默认编码已经改为UTF-8,以是这个函数被移除了。
  • 字符串编码变化:Python 3默认利用的是Unicode(UTF-8)编码,而不再需要手动设置默认编码。
解决方案:


  • 删除或注释掉相关代码:如果你的代码中包含sys.setdefaultencoding ('utf-8')这一行,可以直接将其删除或注释掉,由于这在Python 3中是无效的。
  • 制止重新加载sys模块:尽管有些资料提到可以通过reload(sys)来恢复setdefaultencoding功能,但这种方法在Python 3中并不保举,并且可能不会成功。
示例代码:

假设你的代码中有如下部门:
  1. import sys
  2. sys.setdefaultencoding ('utf-8')
复制代码
你可以将上述代码修改为:
  1. # 如果不需要设置编码,则直接注释掉或删除此行
  2. # sys.setdefaultencoding ('utf-8')
复制代码
总结:

总之,当遇到AttributeError: module 'sys' has no attribute 'setdefaultencoding'错误时,重要原因是Python 3中没有sys.setdefaultencoding 方法。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

我爱普洱茶

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表