16

Possible Duplicate:
Changing default encoding of python?

I am reading dive in python and it mentions setting python's default encoding scheme in the XML parsing chapter.

The setdefaultencoding is used in python-installed-dir/site-packages/pyanaconda/sitecustomize.py

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

But when I run the script, it raises:

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

How to set the default encoding,anyway?

I am using python 2.7

Solution: find the site.py in the python installation.

Edit the setencoding function

def setencoding():
    encoding = "ascii" 
    if 0:
        import locale
        loc = locale.getdefaultlocale()
        if loc[1]:
            encoding = loc[1]
    if 0: #changes comes here, change 0 to 1
        encoding = "undefined" #the encoding you want
    if encoding != "ascii":
        sys.setdefaultencoding(encoding) 

I am using python 2.7

Community
  • 1
  • 1
xiaohan2012
  • 9,870
  • 23
  • 67
  • 101

2 Answers2

8

Python's sys module has had a setdefaultencoding function since Python 2.0. However,

This function is only intended to be used by the site module implementation and, where needed, by sitecustomize. Once used by the site module, it is removed from the sys module’s namespace.

The docs back to at least Python 2.1 indicate this happens, so it was never appropriate for PyAnaconda to use this method, and I'm not sure why it ever worked.

agf
  • 171,228
  • 44
  • 289
  • 238
7

How to set the default encoding,anyway?

Run sys.setdefaultencoding in the file sitecustomize.py, which needs to be in sys.path (e.g. lib/site-packages) when Python starts up. You can verify the change with sys.getdefaultencoding.


Edit for anonymous downvoter:

Whoever downvoted this answer, would you care to explain? This question is for Python 2.x only. There is no sys.setdefaultencoding in Python 3 if that's your problem. I stand by my explanation of how to use this function if one wants to in Python 2. I wasn't defending its use or recommending its use. A library should never touch it, which is why it's removed from the sys namespace after site.py and sitecustomize.py have a chance to call it. A library should also never assume the default encoding is ASCII in 2.x. It's up to the system. Personally I leave it as ASCII.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
  • it seems sys module does not have setdefaultencoding function. I included the path in which sitecustomize.py is in. When I ran the script, it still raises the same error. – xiaohan2012 Aug 19 '11 at 01:31
  • Among all the options I found, this is the answer that works for me. I simply created the file `sitecustomize.py` with the content given. I'm using virtualenvwrapper so I creted the file in `.virtualenvs/virtualenv-name/lib/python2.7/`, where _virtualenv-name_ is the name of the relevent virtualenv. Nonetheless, `locate sitecustomize.py` can tell you where the file is. – kiril Sep 16 '16 at 13:07