本文翻译自:Python locale error: unsupported locale setting
Why do I get the following error when doing this in python: 在python中执行此操作时为什么会出现以下错误:
>>> import locale
>>> print str( locale.getlocale() )
(None, None)
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/locale.py", line 531, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
This works with other locales like fr or nl as well. 这也适用于其他语言环境,例如fr或nl。 I'm using Ubuntu 11.04. 我正在使用Ubuntu 11.04。
Update: Doing the following did not yield anything: 更新:执行以下操作不会产生任何结果:
dpkg-reconfigure locales
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_CTYPE = "UTF-8",
LANG = (unset)
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
#1楼
参考:https://stackoom.com/question/z2VD/Python语言环境错误-不支持的语言环境设置
#2楼
You probably do not have any de_DE
locale available. 您可能没有可用的de_DE
语言环境。
You can view a list of available locales with the locale -a
command. 您可以使用locale -a
命令查看可用语言环境的列表。 For example, on my machine: 例如,在我的机器上:
$ locale -a
C
C.UTF-8
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
it_CH.utf8
it_IT.utf8
POSIX
Note that if you want to set the locale to it_IT
you must also specify the .utf8
: 请注意,如果要将语言环境设置为it_IT
还必须指定.utf8
:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'it_IT') # error!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/locale.py", line 539, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
>>> locale.setlocale(locale.LC_ALL, 'it_IT.utf8')
'it_IT.utf8'
To install a new locale use: 要安装新的语言环境,请使用:
sudo apt-get install language-pack-id
where id
is the language code (taken from here ) id
是语言代码(从此处获取 )
After you have installed the locale you should follow Julien Palard advice and reconfigure the locales with: 已安装的语言环境后,您应该遵循朱利安Palard建议和重新配置的语言环境:
sudo dpkg-reconfigure locales
#3楼
如果您使用的是Debian(或Debian分支),则可以使用来添加语言环境:
dpkg-reconfigure locales
#4楼
在Arch Linux上,我可以通过运行sudo locale-gen
来解决此问题。
#5楼
For the record, I had this same problem, but none of the solutions worked. 记录下来,我遇到了同样的问题,但是没有一种解决方案有效。 I had upgraded my computer and migrated my PC. 我已经升级了计算机并迁移了PC。 I had aa mixed locale english and spanish: 我有一个混合语言环境的英语和西班牙语:
$ locale
LANG=en_US.utf8
LANGUAGE=
LC_CTYPE="en_US.utf8"
LC_NUMERIC=es_ES.utf8
LC_TIME=es_ES.utf8
LC_COLLATE="en_US.utf8"
LC_MONETARY=es_ES.utf8
LC_MESSAGES="en_US.utf8"
LC_PAPER=es_ES.utf8
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT=es_ES.utf8
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=
But, on my new Debian installation, I just selected english as locale. 但是,在新的Debian安装中,我只是选择英语作为语言环境。 Which finally worked was to reconfigure locales package to add and generate spanish too. 最终起作用的是重新配置语言环境包,以添加和生成西班牙语。
$ grep -v "#" /etc/locale.gen
en_US.UTF-8 UTF-8
es_ES.UTF-8 UTF-8
#6楼
This error can occur, if you have just added a new locale. 如果您刚刚添加了新的语言环境,则可能会发生此错误。 You need to restart the python interactive shell ( quit(
) and python
) to get access to it. 您需要重新启动python交互式shell( quit(
)和python
)才能访问它。