Admin

docker centos7 镜像 添加python3中文支持
2019年5月12日 15:30 9 0 0 0

默认情况下,在centos7镜像内装好python3环境,运行python项目时,经常遇到如下错误

  1. UnicodeEncodeError: 'ascii' codec can't encode character ....

这是因为没有中文支持并且中文不是默认字符集,运行命令查看,可能输出如下:

  1. [root@box /]# locale
  2. locale: Cannot set LC_CTYPE to default locale: No such file or directory
  3. locale: Cannot set LC_MESSAGES to default locale: No such file or directory
  4. locale: Cannot set LC_ALL to default locale: No such file or directory
  5. LANG=C.UTF-8
  6. LC_CTYPE="C.UTF-8"
  7. LC_NUMERIC="C.UTF-8"
  8. LC_TIME="C.UTF-8"
  9. LC_COLLATE="C.UTF-8"
  10. LC_MONETARY="C.UTF-8"
  11. LC_MESSAGES="C.UTF-8"
  12. LC_PAPER="C.UTF-8"
  13. LC_NAME="C.UTF-8"
  14. LC_ADDRESS="C.UTF-8"
  15. LC_TELEPHONE="C.UTF-8"
  16. LC_MEASUREMENT="C.UTF-8"
  17. LC_IDENTIFICATION="C.UTF-8"
  18. LC_ALL=

可能也是 posix 啥的,反正不是· zn_CN.UTF-8

1. 临时

1.1 安装中文

  1. yum install -y kde-l10n-Chinese
  2. yum -y reinstall glibc-common
  3. localedef -c -f UTF-8 -i zh_CN zh_CN.utf8

1.2 环境变量

临时,重启容器失效,在容器内运行:

  1. export LANG=zh_CN.UTF-8
  2. export LC_ALL=zh_CN.UTF-8
  3. export LANGUAGE=zh_CN.UTF-8

2. 永久

在dockerfile中指定。
参考:
https://blog.csdn.net/houshangling/article/details/77119421

3. 检查

  1. [root@box /]# export LANG=zh_CN.UTF-8
  2. [root@box /]# export LC_ALL=zh_CN.UTF-8
  3. [root@box /]# export LANGUAGE=zh_CN.UTF-8
  4. [root@box /]# locale
  5. LANG=zh_CN.UTF-8
  6. LC_CTYPE="zh_CN.UTF-8"
  7. LC_NUMERIC="zh_CN.UTF-8"
  8. LC_TIME="zh_CN.UTF-8"
  9. LC_COLLATE="zh_CN.UTF-8"
  10. LC_MONETARY="zh_CN.UTF-8"
  11. LC_MESSAGES="zh_CN.UTF-8"
  12. LC_PAPER="zh_CN.UTF-8"
  13. LC_NAME="zh_CN.UTF-8"
  14. LC_ADDRESS="zh_CN.UTF-8"
  15. LC_TELEPHONE="zh_CN.UTF-8"
  16. LC_MEASUREMENT="zh_CN.UTF-8"
  17. LC_IDENTIFICATION="zh_CN.UTF-8"
  18. LC_ALL=zh_CN.UTF-8

4. 运行

最后,运行python程序就不会出错了。

5. 参考链接

https://blog.csdn.net/houshangling/article/details/77119421
https://zhuanlan.zhihu.com/p/31078295
https://my.oschina.net/JasonZhang/blog/1581855

发布内容,请遵守相关法律法规。
评论