getdefaultencoding - how to change this?

John Pinner funthyme at gmail.com
Fri Jan 21 07:15:30 EST 2011


To answer the OP's original question:

On Jan 20, 2:31 pm, Helmut Jarausch <jarau... at skynet.be> wrote:
> Hi,
> I've searched the net but didn't find the information I need.
> Using Python-2.7.1, I know, I can't modify defaultencoding at run time.

I think you can. There is a function setdefaultencoding in the sys
module, but at startup when site.py runs the function gets deleted
after it has been used, because as others have said it is a *bad* idea
to change the default encoding (although I *think* changing it to utf8
should cause no harm).

so if you reload sys, setdefaultencoding() becomes available again,
and you can use it, with all the caveats mentioned:

import sys
reload(sys)
sys.setdefaultencoding( 'utf-8' )

This works on a Unicode build of Python only.

As has been said, you can change the default encoding in site.py, but
this means that it gets changed for everyone/every Python program on
the system, using setdefaultencoding() as above only changes it for
the running program, and hopefully the change will have been made by
someone who knows what they are doing and is aware of the possible
consequences.

> Python even ignores
> export PYTHONIOENCODING=ISO8859-1
>
> locale.getdefaultlocale()[1]
> returns
> 'ISO8859-1'
>
> still sys.stdout is using the ascii codec.
> How can I recompile Python (itself) to change this to iso8859-1 ?

You don't need to, nor should you.

> (My favourite editor cannot be told to use unicode.)

Maybe you need a new editor. scite works well with Python, and is
cross-platform.

John
--





More information about the Python-list mailing list