Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why there is no "setdefaultencoding" in sys module?

11,793 views
Skip to first unread message

crow

unread,
Jul 9, 2010, 11:58:35 AM7/9/10
to
Hi, everyone

I'm a new hand at python.

I tried to set system default encoding by using

"import sys; sys.setdefaultencoding('utf-f')",

but I got error message:

>>> sys.setdefaultencoding('utf-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'setdefaultencoding'

Then I checked dir(sys), seems there was no function named
"setdefaultencoding" in "sys" module. But in python's document, it
said I should use sys.setdefaultencoding.

So, my questions: why there is no setdefaultencoding in sys module? if
I want to change system's default encoding, what should I do?

Thanks in advance

Steven D'Aprano

unread,
Jul 9, 2010, 12:04:02 PM7/9/10
to
On Fri, 09 Jul 2010 08:58:35 -0700, crow wrote:

> So, my questions: why there is no setdefaultencoding in sys module? if I
> want to change system's default encoding, what should I do?

I think the answer is:

Don't.

If you do, you will break built-ins.

http://tarekziade.wordpress.com/2008/01/08/syssetdefaultencoding-is-evil/


Googling will find many discussions about this.

--
Steven

crow

unread,
Jul 9, 2010, 12:06:41 PM7/9/10
to
On Jul 10, 12:04 am, Steven D'Aprano <st...@REMOVE-THIS-

cybersource.com.au> wrote:
> On Fri, 09 Jul 2010 08:58:35 -0700, crow wrote:
> > So, my questions: why there is no setdefaultencoding in sys module? if I
> > want to change system's default encoding, what should I do?
>
> I think the answer is:
>
> Don't.
>
> If you do, you will break built-ins.
>
> http://tarekziade.wordpress.com/2008/01/08/syssetdefaultencoding-is-e...

>
> Googling will find many discussions about this.
>
> --
> Steven

Interesting, so it has been removed from python? then why it's still
in document... It's really misleading.

Thanks for your quick answer

crow

unread,
Jul 9, 2010, 12:16:42 PM7/9/10
to

oh, I take back my words, it's still there, just I need to
reload(sys).

Thomas Jollans

unread,
Jul 9, 2010, 12:35:34 PM7/9/10
to pytho...@python.org

Actually, it's still there. Lurking in the corners of sys. But site.py
knows it's evil:

% python
Python 2.6.5+ (release26-maint, Jul 6 2010, 12:58:20)
[GCC 4.4.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.setdefaultencoding


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'setdefaultencoding'

>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding
<built-in function setdefaultencoding>
>>>


Christian Heimes

unread,
Jul 9, 2010, 1:25:31 PM7/9/10
to pytho...@python.org
> oh, I take back my words, it's still there, just I need to
> reload(sys).

Just don't. If you change the default encoding you are going to break
important data structures like dicts in a subtle and hard to detect way.
If your application needs to change the default encoding, it's broken.
The function is removed for a very good reaso.

Terry Reedy

unread,
Jul 9, 2010, 5:48:30 PM7/9/10
to pytho...@python.org
On 7/9/2010 11:58 AM, crow wrote:
> But in python's document, it
> said I should use sys.setdefaultencoding.

Where? That may need to be changed.


--
Terry Jan Reedy

rajinder...@gmail.com

unread,
Feb 15, 2018, 2:07:21 AM2/15/18
to
Becuase you are using python 3.setdefaultencoding is no longer supportedin python 3. python3automatically have utf-8 in its configuration by default. so you don't have to use it. just remove the line.
0 new messages