locale.Error: unsupported locale setting 的解决方案

997 阅读1分钟

locale.Error: unsupported locale setting 的解决方案


0. 参考资料

stackoverflow.com/questions/1…


1. 报错原因

vagrant + virtualbox上安装的 ubuntu 16.04 使用 pip3 listpython3 -m venv venv 两条命令均报错,详细报错信息如下:

vagrant@ubuntu-xenial:~/microblog$ pip3 list
Traceback (most recent call last):
  File "/usr/bin/pip3", line 11, in <module>
    sys.exit(main())
  File "/usr/lib/python3/dist-packages/pip/__init__.py", line 215, in main
    locale.setlocale(locale.LC_ALL, '')
  File "/usr/lib/python3.5/locale.py", line 594, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

原因在于该系统缺少相应的语言包,需要下载安装才行。


2. 解决方案

使用 locale 查看当前语言设置:

vagrant@ubuntu-xenial:~$ locale
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=zh_CN.UTF-8
LC_TIME=zh_CN.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=zh_CN.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=zh_CN.UTF-8
LC_NAME=zh_CN.UTF-8
LC_ADDRESS=zh_CN.UTF-8
LC_TELEPHONE=zh_CN.UTF-8
LC_MEASUREMENT=zh_CN.UTF-8
LC_IDENTIFICATION=zh_CN.UTF-8
LC_ALL=

发现该设置中有两种语言,一种是 en_US.UTF-8 ,另一种是 zh_CN.UTF-8

使用 locale -a 查看当前系统所有可用语言:

vagrant@ubuntu-xenial:~$ locale -a
C
C.UTF-8
en_US.utf8
id_ID.utf8
POSIX

发现上面的可用语言中缺少 zh_CN.UTF-8 ,这就是报错原因所在。

使用 sudo apt install language-pack-zh-hans 安装语言:

vagrant@ubuntu-xenial:~$ sudo apt install language-pack-zh-hans
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  language-pack-zh-hans-base
The following NEW packages will be installed:
  language-pack-zh-hans language-pack-zh-hans-base
0 upgraded, 2 newly installed, 0 to remove and 3 not upgraded.
Need to get 2110 kB of archives.
After this operation, 8545 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 language-pack-zh-hans-base all 1:16.04+20160627 [2108 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 language-pack-zh-hans all 1:16.04+20160627 [1870 B]
Fetched 2110 kB in 3s (567 kB/s)           
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = (unset),
	LC_TIME = "zh_CN.UTF-8",
	LC_MONETARY = "zh_CN.UTF-8",
	LC_ADDRESS = "zh_CN.UTF-8",
	LC_TELEPHONE = "zh_CN.UTF-8",
	LC_NAME = "zh_CN.UTF-8",
	LC_MEASUREMENT = "zh_CN.UTF-8",
	LC_IDENTIFICATION = "zh_CN.UTF-8",
	LC_NUMERIC = "zh_CN.UTF-8",
	LC_PAPER = "zh_CN.UTF-8",
	LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
locale: Cannot set LC_ALL to default locale: No such file or directory
Selecting previously unselected package language-pack-zh-hans-base.
(Reading database ... 89747 files and directories currently installed.)
Preparing to unpack .../language-pack-zh-hans-base_1%3a16.04+20160627_all.deb ...
Unpacking language-pack-zh-hans-base (1:16.04+20160627) ...
Selecting previously unselected package language-pack-zh-hans.
Preparing to unpack .../language-pack-zh-hans_1%3a16.04+20160627_all.deb ...
Unpacking language-pack-zh-hans (1:16.04+20160627) ...
Setting up language-pack-zh-hans (1:16.04+20160627) ...
Setting up language-pack-zh-hans-base (1:16.04+20160627) ...
Generating locales (this might take a while)...
  zh_CN.UTF-8... done
  zh_SG.UTF-8... done
Generation complete.

最后生成了我们需要的 zh_CN.UTF-8 语言。

再次使用 pip3 listpython3 -m venv venv 也不会报错了。