Python语言环境在alpine linux上不起作用

2024-04-26 06:59:34 发布

您现在位置:Python中文网/ 问答频道 /正文

代码很简单:

import locale
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8') # I tried de_DE and de_DE.utf8 too
locale.currency(0)

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.7/locale.py", line 267, in currency
    raise ValueError("Currency formatting is not possible using "
ValueError: Currency formatting is not possible using the 'C' locale.

当我在ubuntu上运行它时,它就工作了。然而,在阿尔卑斯山,这个错误会弹出。我尝试了this注释的变通方法,但没有成功。我还在脚本的顶部添加了/usr/glibc-compat/binPATH,没有帮助

有什么方法可以让地点在阿尔卑斯山上工作吗

你自己试试看:

docker run --rm alpine sh -c "apk add python3; python3 -c 'import locale; locale.setlocale(locale.LC_ALL, \"de_DE.UTF-8\"); locale.currency(0)'"

更新:this回购也不起作用

更新:我也尝试了this指南,但它似乎与python不兼容?即使出现了区域设置,我仍然得到以下信息:

/app # locale -a
C
C.UTF-8
sv_SE.UTF-8
en_GB.UTF-8
ch_DE.UTF-8
pt_BR.UTF-8
ru_RU.UTF-8
it_IT.UTF-8
de_CH.UTF-8
en_US.UTF-8
fr_FR.UTF-8
nb_NO.UTF-8
de_DE.UTF-8 <--
nl_NL.UTF-8
es_ES.UTF-8
/app # python
Python 3.7.7 (default, Apr 24 2020, 22:09:29) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
'de_DE.UTF-8'
>>> locale.currency(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/locale.py", line 267, in currency
    raise ValueError("Currency formatting is not possible using "
ValueError: Currency formatting is not possible using the 'C' locale.

Tags: inislinenotdelocalecurrencyutf
1条回答
网友
1楼 · 发布于 2024-04-26 06:59:34

此最小Dockerfile

FROM alpine:3.12

ENV MUSL_LOCPATH="/usr/share/i18n/locales/musl"

RUN apk  no-cache add \
    musl-locales \
    musl-locales-lang \
    python3

通过使用上述musl语言环境包,目前似乎只有在使用Python的Alpine Linux上才能部分工作:

  1. 立法会时间:成功
import locale
from time import gmtime, strftime
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
'en_US.UTF-8'
strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Wed, 26 Aug 2020 16:50:17 +0000'
locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
'de_DE.UTF-8'
strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Mi, 26 Aug 2020 16:50:31 +0000'
  1. 立法会:财政司司长
import locale
locale.getlocale()
('en_US', 'UTF-8')
locale.currency(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/locale.py", line 267, in currency
    raise ValueError("Currency formatting is not possible using "
ValueError: Currency formatting is not possible using the 'C' locale.
locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
'de_DE.UTF-8'
locale.currency(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/locale.py", line 267, in currency
    raise ValueError("Currency formatting is not possible using "
ValueError: Currency formatting is not possible using the 'C' locale.
  1. LC_数字:失败
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
'en_US.UTF-8'
locale.format_string("%.1f", 1.4)
'1.4'
locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
'de_DE.UTF-8'
locale.format_string("%.1f", 1.4)
'1.4'
  1. 这看起来也很糟糕:
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
'en_US.UTF-8'
locale.localeconv()
{'int_curr_symbol': '', 'currency_symbol': '', 'mon_decimal_point': '', 'mon_thousands_sep': '', 'mon_grouping': [], 'positive_sign': '', 'negative_sign': '', 'int_frac_digits': 127, 'frac_digits': 127, 'p_cs_precedes': 127, 'p_sep_by_space': 127, 'n_cs_precedes': 127, 'n_sep_by_space': 127, 'p_sign_posn': 127, 'n_sign_posn': 127, 'decimal_point': '.', 'thousands_sep': '', 'grouping': []}
locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
'de_DE.UTF-8'
locale.localeconv()
{'int_curr_symbol': '', 'currency_symbol': '', 'mon_decimal_point': '', 'mon_thousands_sep': '', 'mon_grouping': [], 'positive_sign': '', 'negative_sign': '', 'int_frac_digits': 127, 'frac_digits': 127, 'p_cs_precedes': 127, 'p_sep_by_space': 127, 'n_cs_precedes': 127, 'n_sep_by_space': 127, 'p_sign_posn': 127, 'n_sign_posn': 127, 'decimal_point': '.', 'thousands_sep': '', 'grouping': []}

本地化失败可能是由于https://gitlab.com/rilian-la-te/musl-locales中的PO文件不完整或由于未满足特定Python期望

接下来,有人可以使用另一种编程语言(如PHP)检查使用区域设置的函数是否按预期工作

相关问题 更多 >