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

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.


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.


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

Tags:

Python

Django