Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

django

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 ?

like image 816
Clément Gervaise Avatar asked Jan 24 '15 16:01

Clément Gervaise


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

like image 68
Martijn Pieters Avatar answered Sep 19 '22 21:09

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.

like image 37
Alex Martelli Avatar answered Sep 22 '22 21:09

Alex Martelli