Sunday, 9 December 2012

Solved: locale error in Python on OS X

I was amused by the following error I got on my Mac terminal today :
How can the Mac not know about UTF-8?
I checked my current locale:
Searching for the reason, I stumbled upon this stackoverflow question. So, UTF-8 isn't a valid OS X locale by default!
Now, the stackoverflow question said, the problem can be solved by setting your locale to UTF-8 explicitly. Thus, I used the export command to set the environment variables of locale to my required locale i.e. UTF-8
And the locale became:
This pretty much solved my problem. But being an avid lover of python, I was soon disgusted to do this every time I opened a new shell. So, I added the above commands to my ~/.profile ;) . Thus, now whenever I restart/start a shell, The locale is set to UTF-8 automatically. I haven't noticed any side issues with this yet, but my python experience has become better. :)

PS: To see the locales supported by your system, you can use locale -a