title: How to set default encoding in Python (setdefaultencoding() function does not exist)?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2276200/changing-default-encoding-of-python">Changing default encoding of python?</a>  </p>
  </blockquote>
  
  
  
  <p>I am reading <em>dive in python</em> and it mentions setting python's default encoding scheme in the XML parsing chapter. </p>
  
  <p>The <strong>setdefaultencoding</strong> is used in   <em>python-installed-dir/site-packages/pyanaconda/<strong>sitecustomize.py</em></strong></p>
  
  <pre><code>import sys
  sys.setdefaultencoding('utf-8')
  </code></pre>
  
  <p>But when I run the script, it raises:</p>
  
  <pre><code>AttributeError: 'module' object has no attribute 'setdefaultencoding'
  </code></pre>
  
  <p>How to set the default encoding,anyway?</p>
  
  <p>I am using python 2.7</p>
  
  <p><strong>Solution:</strong>
  find the site.py in the python installation.</p>
  
  <p>Edit the <em>setencoding</em> function</p>
  
  <pre><code>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) 
  </code></pre>
  
  <p>I am using python 2.7</p>