Fix "ValueError: unknown locale: UTF-8" under Mac OS X'

Posted on Mon 04 April 2016 in Notes

This is a problem I've been having after switching from OSX' default bash to oh-my-zsh.

When importing things like matplotlib in Python I get the following error:

ValueError: unknown locale: UTF-8

The problem is that the locale has not been set and UTF-8 is not a valid locale, as it is only an encoding.

In bash or zsh run

$ locale

if it looks like

LANG=
LC_COLLATE="C"
LC_CTYPE="C"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=

You are in trouble. You want it to look something like the following if you are using US locale

LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL="en_US.UTF-8"

If you browse around the web for a solution you will be told to add

export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

To all sorts of places. ~/.bash, ~/.profile, /etc/.profile and the list goes on. If you are running oh-my-zsh you need to edit ~/.zshrc and add the above two lines and restart you terminal and python as well.