Admin
默认情况下,在centos7镜像内装好python3环境,运行python项目时,经常遇到如下错误
UnicodeEncodeError: 'ascii' codec can't encode character ....
这是因为没有中文支持并且中文不是默认字符集,运行命令查看,可能输出如下:
[root@box /]# localelocale: Cannot set LC_CTYPE to default locale: No such file or directorylocale: Cannot set LC_MESSAGES to default locale: No such file or directorylocale: Cannot set LC_ALL to default locale: No such file or directoryLANG=C.UTF-8LC_CTYPE="C.UTF-8"LC_NUMERIC="C.UTF-8"LC_TIME="C.UTF-8"LC_COLLATE="C.UTF-8"LC_MONETARY="C.UTF-8"LC_MESSAGES="C.UTF-8"LC_PAPER="C.UTF-8"LC_NAME="C.UTF-8"LC_ADDRESS="C.UTF-8"LC_TELEPHONE="C.UTF-8"LC_MEASUREMENT="C.UTF-8"LC_IDENTIFICATION="C.UTF-8"LC_ALL=
可能也是 posix 啥的,反正不是· zn_CN.UTF-8
yum install -y kde-l10n-Chineseyum -y reinstall glibc-commonlocaledef -c -f UTF-8 -i zh_CN zh_CN.utf8
临时,重启容器失效,在容器内运行:
export LANG=zh_CN.UTF-8export LC_ALL=zh_CN.UTF-8export LANGUAGE=zh_CN.UTF-8
在dockerfile中指定。
参考:
https://blog.csdn.net/houshangling/article/details/77119421
[root@box /]# export LANG=zh_CN.UTF-8[root@box /]# export LC_ALL=zh_CN.UTF-8[root@box /]# export LANGUAGE=zh_CN.UTF-8[root@box /]# localeLANG=zh_CN.UTF-8LC_CTYPE="zh_CN.UTF-8"LC_NUMERIC="zh_CN.UTF-8"LC_TIME="zh_CN.UTF-8"LC_COLLATE="zh_CN.UTF-8"LC_MONETARY="zh_CN.UTF-8"LC_MESSAGES="zh_CN.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=zh_CN.UTF-8
最后,运行python程序就不会出错了。
https://blog.csdn.net/houshangling/article/details/77119421
https://zhuanlan.zhihu.com/p/31078295
https://my.oschina.net/JasonZhang/blog/1581855