Python: Set character encoding to UTF-8

In Python, the default character encoding set is ASCII, limiting us to a bunch of 128 basic characters. Though, in most sites, we may use accented characters or other various symbols.

So in the default Python settings, you may come up with an issue similar to this:

ascii' codec can't encode character u'\xe9' in position 3828: ordinal not in range(128)

The solution to this is to force Python to encode characters in UTF-8.

For that we need to find site.py on our Python path (print sys.path to find out), and add the following line right under import sys, os:

import sys, os 
#insert the following line 
sys.setdefaultencoding('utf-8')

Now restart your site (if using wsgi) or else your web server, and you should be good to use all kind of utf-8 characters.

< / >