Python运行环境locale

Colima 容器环境运行 Debian镜像(tini进程管理器) ,会发现 Sphinx文档 执行 make 报错:

debian容器镜像 locale 错误导致执行python模块locale报错
Traceback (most recent call last):
  File "/home/admin/venv3/bin/sphinx-build", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/admin/venv3/lib/python3.11/site-packages/sphinx/cmd/build.py", line 369, in main
    locale.setlocale(locale.LC_ALL, '')
  File "/usr/lib/python3.11/locale.py", line 626, in setlocale
    return _setlocale(category, locale)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
locale.Error: unsupported locale setting
make: *** [Makefile:19: clean] Error 1

检查 locale 命令输出可以看到容器环境没有配置 LC_CTYPE , LC_MESSAGES , LC_ALL :

locale 输出显示部分locale变量没有配置
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
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="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

上述报错是因为没有配置 locales ,我参考 Python locale error: unsupported locale setting 想重新配置,却发现实际上 locales 软件包没有安装:

locales 软件包没有安装导致无法配置
# export LC_ALL="en_US.UTF-8"
-bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8): No such file or directory

# cat locale.gen                                                                                                                                
cat: locale.gen: No such file or directory
# cat locale.conf                                                                                                                               
cat: locale.conf: No such file or directory

# dpkg-reconfigure locales
-bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = "en_US.UTF-8",
        LC_CTYPE = "en_US.UTF-8",
        LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 78.)
debconf: falling back to frontend: Readline
dpkg-query: package 'locales' is not installed and no information is available
Use dpkg --info (= dpkg-deb --info) to examine archive files.
/usr/sbin/dpkg-reconfigure: locales is not installed

通常我们会为了支持某个locales,需要先配置环境变量,然后执行 locale-gen ,但是显然这里也是错误的:

执行 locale-gen 来生成对应locales
echo "LC_ALL=en_US.UTF-8" >> /etc/environment
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
echo "LANG=en_US.UTF-8" > /etc/locale.conf
locale-gen en_US.UTF-8

不过这里会报错 locale-gen: command not found

解决方法

其实很简单的,就是docker官方debian镜像没有安装 locales 软件包导致的,所以需要补充安装:

通过安装locales软件包解决
apt install locales

安装会自动调用 locale-gen ,则根据 /etc/locale.gen 配置,会自动完成 locale-gen en_US.UTF-8

参考