AttributeError: 'module' object has no attribute 'setdefaultencoding'
Asked Answered
F

4

35

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 :

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 :

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

What can I do ?

Forenamed answered 24/1, 2015 at 16:20 Comment(1)
Ok so, i think i'll keep the django's admin by default... Thanks for help guysPartridge
E
80

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).

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

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.

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.

Embody answered 24/1, 2015 at 16:22 Comment(3)
Is getdefaultencoding also chucked out in Py3?Hypochlorite
@BhargavRao, no, it's there and returns 'utf-8'.Wrinkly
@BhargavRao: sys.getdefaultencoding() is still there.Embody
W
18

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

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 only bit 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 xadmin asking what are the plans to make it Py 3-compatible and how you can help w/the task.

Wrinkly answered 24/1, 2015 at 16:32 Comment(3)
I'd much rather the maintainers didn't use sys.setdefaultencoding() at all as setting the default encoding can break 3rd party libraries and should generally be avoided anyway.Embody
@MartijnPieters, sure, but, unless you're offering to re-architect their whole code for Python 2, I suspect the excellent advice is unlikely to be followed. Moving to Python 3 compatibility is a task worthier of effort than improving the quality of a package from a strictly Python 2 viewpoint, anyway:-).Wrinkly
Agreed. The lines were added in the initial commit, so it was most likely a Cargo Cult inclusion, rather than a conscious choice.Embody
M
4

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

sys.setdefaultencoding("utf-8")
Maxine answered 14/4, 2018 at 19:20 Comment(0)
L
0

In Python 2 Version You need to use this. But In Python 3 they already included it and no need to mention it explicitly. Just remove it or comment it down and you are good to go.

Lipid answered 3/8, 2020 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.