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

Question:

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:Python34libsite-packagesxadminsites.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 ?

Asked By: Clément Gervaise

||

Answers:

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.

Answered By: Martijn Pieters

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.

Answered By: Alex Martelli

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")
Answered By: Ramineni Ravi Teja

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.

Answered By: RAJ KUMAR NAYAK
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.