gpt4 book ai didi

基于 python 的 Dockerfile 抛出 locale.Error : unsupported locale setting

转载 作者:行者123 更新时间:2023-12-03 20:26:53 25 4
gpt4 key购买 nike

我在将主机的 (Centos7) 语言环境传递给 python3 docker 镜像时遇到问题。尽管我使用了以下链接中描述的建议,但只有以下语言环境最终出现在图像中:

C
C.UTF-8
POSIX

Why does locale.getpreferredencoding() return 'ANSI_X3.4-1968' instead of 'UTF-8'?

我的 Dockerfile 有:
FROM python:3.7.5
ENV LC_ALL C.UTF-8
WORKDIR /data
ADD ./requirements.txt /data/requirements.txt
RUN pip install -r requirements.txt
COPY . /data
CMD [ "python3", "./test.py" ]

当我运行此命令时:
locale.setlocale(locale.LC_ALL,'ru_RU')

它抛出这个错误:
Traceback (most recent call last):
File "./test.py", line 10, in <module>
locale.setlocale(locale.LC_ALL,'ru_RU')
File "/usr/local/lib/python3.7/locale.py", line 608, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting

如果我设置
ENV LANG ru_RU.UTF-8
ENV LC_ALL ru_RU.UTF-8

然后我得到:
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_COLLATE to default locale: No such file or directory
locale.getdefaultlocale ('ru_RU', 'UTF-8')
locale.getpreferredencoding UTF-8
Exception: unsupported locale setting

请解释如何将 ru_RU 语言环境添加到 python 图像中?

最佳答案

我会为基于 Debian 的 docker 镜像做什么:

FROM python:3.7.5

RUN apt-get update && \
apt-get install -y locales && \
sed -i -e 's/# ru_RU.UTF-8 UTF-8/ru_RU.UTF-8 UTF-8/' /etc/locale.gen && \
dpkg-reconfigure --frontend=noninteractive locales

ENV LANG ru_RU.UTF-8
ENV LC_ALL ru_RU.UTF-8

然后在python中:

import locale

locale.setlocale(locale.LC_ALL,'ru_RU.UTF-8')

关于基于 python 的 Dockerfile 抛出 locale.Error : unsupported locale setting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59633558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com