Subscribe

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!

Solving error in ZSH and LOCALES - ValueError: unknown locale: UTF-8

Before to start this problem happened to me on MacOS, but it could happen in other OS like GNU/Linux, and it is a common problem whit Python applications.
You can diagnostic the problem using the command locale in the terminal:

$ locale
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="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=

In the previous output you can see the variable LC_CTYPE is UTF-8 so that is the problem.

Solution

The solution is actually very easy is just copy and paste this lines of code in this file for your user ~/.zshenv or for all users /etc/zshenv:

# only define LC_CTYPE if undefined or is UTF-8
if [[ -z "$LC_CTYPE" || "$LC_CTYPE" -eq "UTF-8" ]]; then
	export LC_CTYPE=${LANG%%:*} # pick the first entry from LANG
fi

The previous code is for the variable LC_CTYPE if your problem is another variable you can change it to the correct variable name.

If you want to be sure all is working correctly, use this python command:

python -c 'import locale; print(locale.getdefaultlocale());'

Comments: