Python AttributeError: 'module' 对象没有属性 'setdefaultencoding'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28127513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:46:16  来源:igfitidea点击:

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

pythondjango

提问by Clément Gervaise

I try to install xadmin (it's a django's plugin for use the backoffice with twitter's bootstrap). But when I run my project, I have the following error in my PyCharm terminal :

我尝试安装 xadmin(它是 django 的插件,用于将后台与 twitter 的引导程序一起使用)。但是当我运行我的项目时,我的 PyCharm 终端出现以下错误:

File "C:\Python34\lib\site-packages\xadmin\sites.py", line 10, in <module>
sys.setdefaultencoding("utf-8")
AttributeError: 'module' object has no attribute 'setdefaultencoding'

This is the extract of source code from sites.py in xadmin plugin :

这是 xadmin 插件中来自 sites.py 的源代码的摘录:

import sys
from functools import update_wrapper
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db.models.base import ModelBase
from django.views.decorators.cache import never_cache
from imp import reload

reload(sys)
sys.setdefaultencoding("utf-8")

The project is running with python 3.4 interpreter and Django 1.7.1. The xadmin's version is 0.5.0

该项目使用 python 3.4 解释器和 Django 1.7.1 运行。xadmin 的版本是 0.5.0

What can I do ?

我能做什么 ?

采纳答案by Martijn Pieters

Python 3 has no sys.setdefaultencoding()function. It cannot be reinstated by reload(sys)like it can on Python 2 (which you really shouldn't do in any case).

Python 3 没有sys.setdefaultencoding()功能。它不能reload(sys)像在 Python 2 上那样恢复(在任何情况下你都不应该这样做)。

Since the default on Python 3 is UTF-8 already, there is no point in leaving those statements in.

由于 Python 3 上的默认值已经是 UTF-8 ,因此保留这些语句是没有意义的。

In Python 2, using sys.setdefaultencoding()was used to plaster over implicit encoding problems (caused by concatening byte strings and unicode values, and other such mixed type situations), rather than fixing the problems themselves. Python 3 did away with implicit encoding and decoding, so using the plaster to set a different encoding would make no difference anyway.

在 Python 2 中, usingsys.setdefaultencoding()用于解决隐式编码问题(由连接字节字符串和 unicode 值以及其他此类混合类型情况引起),而不是解决问题本身。Python 3 取消了隐式编码和解码,因此使用石膏设置不同的编码无论如何都没有区别。

However, if this is a 3rd-party library, then you probably will run into other problems as it clearly has not been made compatible with Python 3.

但是,如果这是一个 3rd 方库,那么您可能会遇到其他问题,因为它显然与 Python 3 不兼容。

回答by Alex Martelli

Clearly the xadminproject is strictly Python-2. You can patch that one file easily, just turn the last two lines into

显然,该xadmin项目是严格的 Python-2。您可以轻松地修补那个文件,只需将最后两行变成

if sys.version[0] == '2':
    reload(sys)
    sys.setdefaultencoding("utf-8")

and send the tiny patch to the maintainers of xadmin. However it's very unlikely that this is the onlybit in the package that's not compatible with Python 3 -- no doubt you'll run into further, subtler ones later. So, best is to write the maintainers of xadminasking what are the plans to make it Py 3-compatible and how you can help w/the task.

并将小补丁发送给xadmin. 然而,这不太可能是包中唯一与 Python 3 不兼容的部分 —— 毫无疑问,您稍后会遇到更多、更微妙的问题。所以,最好是写给维护者,xadmin询问使其与 Py 3 兼容的计划是什么,以及如何帮助完成任务。

回答by Ramineni Ravi Teja

You don't need to encode data that is already encoded in Python 3. When you try to do that, Python will first try to decode it to Unicode before it can encode it back to UTF-8. you can remove or comment this statement from your code

您不需要对已经在 Python 3 中编码的数据进行编码。当您尝试这样做时,Python 将首先尝试将其解码为 Unicode,然后才能将其编码回 UTF-8。您可以从代码中删除或注释此语句

sys.setdefaultencoding("utf-8")