Fix Python ValueError: unknown locale: UTF-8 under macOS

Nov 26, 2016 Python 中文版

When using some Python modules, you might get the error message “ValueError: unknown locale: UTF-8”, which means that you need to make a change to your locale settings. This post is about fixing it under macOS.



List current locale settings

To list locale settings:

locale


Fix for Bash

Open your Bash configuration (~/.bash_profile), and then add locale settings into it:

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

After the modification, don’t forget to reload .bash_profile:

source ~/.bash_profile


Fix for Fish Shell

Open your Fish Shell configuration (~/.config/fish/config.fish, you can create it if it does not exist), and then add locale settings into it:

set --export LANG en_US.UTF-8
set --export LC_CTYPE en_US.UTF-8

Reload the config file:

source ~/.config/fish/config.fish


Check locale settings

In addition to locale, you can use the command below to check:

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



You might also like:




If you have any suggestions, questions or even find some typos, feel free to contact me. Thank you! :)

zeckli.devforgalaxy@gmail.com   © 2015-2019 zeckli, thanks to Jekyll and GitHub.