0%

解决pip install时unsupported locale setting错误

今天在安装 docker-compose 时,使用 pip install 命令出现了下面这个错误:

1
2
3
4
5
6
7
8
9
➜  sudo pip install -U docker-compose
Traceback (most recent call last):
File "/usr/bin/pip", line 11, in <module>
sys.exit(main())
File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 215, in main
locale.setlocale(locale.LC_ALL, '')
File "/usr/lib/python2.7/locale.py", line 581, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting

后来查询到是语言配置错误导致的:

1
2
3
4
5
6
7
8
➜  locale -a
locale: Cannot set LC_CTYPE to default locale: No such file or directory
C
C.UTF-8
en_US
en_US.iso88591
en_US.utf8
POSIX

解决方法:

执行如下命令即可。

1
➜  export LC_ALL=C

再次尝试安装:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
➜  sudo pip install -U docker-compose
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting docker-compose
Downloading https://files.pythonhosted.org/packages/67/03/b833b571595e05c933d3af3685be3b27b1166c415d005b3eadaa5be80d25/docker_compose-1.22.0-py2.py3-none-any.whl (126kB)
100% |################################| 133kB 143kB/s
Collecting websocket-client<1.0,>=0.32.0 (from docker-compose)
Downloading https://files.pythonhosted.org/packages/09/12/d21872b618befc489cabde794c7af281d12fa2e194e279132ef1f04a3b07/websocket_client-0.52.0-py2.py3-none-any.whl (198kB)
100% |################################| 204kB 34kB/s

...
...
Successfully installed PyYAML-3.11 backports.ssl-match-hostname-3.5.0.1 cached-property-1.4.3 certifi-2018.8.24 chardet-2.3.0 docker-3.5.0 docker-compose-1.22.0 docker-pycreds-0.3.0 dockerpty-0.4.1 docopt-0.6.2 enum34-1.1.2 functools32-3.2.3.post2 idna-2.0 ipaddress-1.0.16 jsonschema-2.6.0 requests-2.9.1 six-1.10.0 texttable-0.9.1 urllib3-1.13.1 websocket-client-0.52.0
You are using pip version 8.1.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

关于LC_ALL=C

LC_ALL=C 是为了去除所有本地化的设置,让命令能正确执行。

在Linux中通过 locale 来设置程序运行的不同语言环境,locale 由ANSI C提供支持。locale 的命名规则为 <语言>_<地区>.<字符集编码> ,如zh_CN.UTF-8,zh代表中文,CN代表大陆地区,UTF-8表示字符集。

在shell控制台中输入 locale 就可以查看本地默认设置:

1
2
3
4
5
6
7
8
9
➜ locale
LANG="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_CTYPE="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_ALL=

“C”是系统默认的locale ,”POSIX”是”C”的别名。所以当我们新安装完一个系统时,默认的 locale 就是 CPOSIX

LANG LC_* 的默认值,是最低级别的设置。可以理解成 LANG是一个缺省值,所有没有显式设置值的 LC_* 变量都会取LANG的值。类似于 LC_ALL

LC_ALL 它是一个宏,如果该值设置了,则该值会覆盖所有 LC_* 的设置值。注意,LANG的值不受该宏影响。

注意: LC_ALL 并不是一个环境变量,而是一个glibc中定义的一个宏。LC_ALL=C 这样的语法实际上是调用了 setlocale 把所有的 LC_* 的变量设置了一遍,所以在终端中直接 echo $LANG 等可以输出对应变量的值,但是 echo $LC_ALL 什么都没有,因为它压根就不是一个变量。

所以设置 LC_ALL=C ,就是解决各种与区域设置有关的warning。从程序运行角度看,就是保持程序输出的统一格式,这样有些程序才能正确执行下去。


相关参考:

如有疑问或需要技术讨论,请留言或发邮件到 service@itfanr.cc