Changing default encoding of Python?
Question: I have many "can't encode" and "can't decode" problems with
[Python](http://en.wikipedia.org/wiki/Python_%28programming_language%29) when
I run my applications from the console. But in the
[Eclipse](http://en.wikipedia.org/wiki/Eclipse_%28software%29)
[PyDev](http://en.wikipedia.org/wiki/PyDev) IDE, the default character
encoding is set to [UTF-8](http://en.wikipedia.org/wiki/UTF-8), and I'm fine.

I searched around for setting the default encoding, and people say that Python
deletes the `sys.setdefaultencoding` function on startup, and we can not use
it.

So what's the best solution for it?


Answer: Here is a simpler method (hack) that gives you back the `setdefaultencoding()`
function that was deleted from `sys`:

    
    
    import sys
    # sys.setdefaultencoding() does not exist, here!
    reload(sys)  # Reload does the trick!
    sys.setdefaultencoding('UTF8')
    

**PS** : This is obviously a hack, since `sys.setdefaultencoding()` is
purposely removed from `sys` when Python starts. Reenabling it and changing
the default encoding [can break code that relies on ASCII being the
default](https://anonbadger.wordpress.com/2015/06/16/why-sys-
setdefaultencoding-will-break-code/) (this code can be third-party, which
would generally make fixing it impossible or dangerous).

