Python 语言环境错误:不支持的语言环境设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14547631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:48:23  来源:igfitidea点击:

Python locale error: unsupported locale setting

pythonlocaleubuntu-11.04

提问by toom

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. I'm using Ubuntu 11.04.

这也适用于 fr 或 nl 等其他语言环境。我正在使用 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

回答by Bakuriu

You probably do not have any de_DElocale available.

您可能没有任何de_DE可用的语言环境。

You can view a list of available locales with the locale -acommand. For example, on my machine:

您可以使用该locale -a命令查看可用语言环境的列表。例如,在我的机器上:

$ 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_ITyou 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 idis the language code (taken from here)

id语言代码在哪里(取自此处

Afteryou have installed the locale you should follow Julien Palard advice and reconfigure the locales with:

之后您已经安装了区域,那么你应该遵循朱利安Palard建议和重新配置的语言环境:

sudo dpkg-reconfigure locales

回答by Julien Palard

If you're on a Debian (or Debian fork), you can add locales using :

如果您使用的是 Debian(或 Debian fork),您可以使用以下命令添加语言环境:

dpkg-reconfigure locales

回答by Keith Smiley

On Arch Linux I was able to fix this by running sudo locale-gen

在 Arch Linux 上,我可以通过运行来解决这个问题 sudo locale-gen

回答by kikeenrique

For the record, I had this same problem, but none of the solutions worked. I had upgraded my computer and migrated my PC. I had a a mixed locale english and spanish:

作为记录,我遇到了同样的问题,但没有一个解决方案有效。我已经升级了我的计算机并迁移了我的 PC。我有一个混合语言环境英语和西班牙语:

$ 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. Which finally worked was to reconfigure locales package to add and generate spanish too.

但是,在我的新 Debian 安装中,我只是选择了英语作为语言环境。最终起作用的是重新配置 locales 包以添加和生成西班牙语。

$ grep -v "#" /etc/locale.gen 
en_US.UTF-8 UTF-8
es_ES.UTF-8 UTF-8

回答by imrek

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) 才能访问它。

回答by Muhammad Hassan

Run following commands

运行以下命令

export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales

It will solve this.

它会解决这个问题。

Make sure to match the .UTF-8part to the actual syntax found in the output of locale -ae.g. .utf8on some systems.

确保将.UTF-8部分与在某些系统上的locale -aeg输出中找到的实际语法相匹配.utf8

回答by lorenzofeliz

More permanent solution would be to fill the missing values, in the output shown by command: locale

更持久的解决方案是在命令显示的输出中填充缺失值: locale

Output from localeis:

输出locale是:

 $ 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=

To Fill the missing values edit ~/.bashrc :

填充缺失值编辑 ~/.bashrc :

 $ vim ~/.bashrc

Add the following lines after the above command (suppose you want en_US.UTF-8 to be your language):

在上述命令之后添加以下几行(假设您希望 en_US.UTF-8 成为您的语言):

export LANGUAGE="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"

If this file is ReadOnly you would be needing to follow the steps mentioned by The GeekyBoy. The answer given by Dr Beco in Superuserhas details relating to saving readonly files.

如果此文件是只读的,您将需要遵循The GeekyBoy提到的步骤。超级用户Beco 博士给出的答案包含有关保存只读文件的详细信息。

After saving the file do:

保存文件后执行:

$ source ~/.bashrc

Now you wont be facing the same problem anymore.

现在你不会再面临同样的问题了。

回答by Fatemeh Abdollahei

According to this link, it solved by entering this command:

根据此链接,输入以下命令即可解决:

export LC_ALL=C

出口 LC_ALL=C

回答by andy

  • run this command localeto get what locale is used. Such as:
  • 运行此命令 locale以获取使用的语言环境。如:

LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_CTYPE=zh_CN.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=

LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_CTYPE=zh_CN.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=

  • search for the listed locales list in first step in /etc/locale-genfile. Uncomment to used ones
  • run locale-gento generate newly added locales
  • /etc/locale-gen文件的第一步中搜索列出的语言环境列表。取消对使用过的注释
  • 运行locale-gen以生成新添加的语言环境

回答by DonPedro

In trying to get python to spit out names in specific localeI landed here with same problem.

在试图让 python在特定语言环境中吐出名称时,我遇到了同样的问题。

In pursuing the answer, things got a little mystical I find.

在寻求答案的过程中,我发现事情变得有点神秘。

I found that python code.

我找到了那个python代码。

import locale
print locale.getdefaultlocale()
>> ('en_DK', 'UTF-8')

And indeed locale.setlocale(locale.LC_TIME, 'en_DK.UTF-8')works

确实locale.setlocale(locale.LC_TIME, 'en_DK.UTF-8')有效

Using tips here I tested further to see what is available using python code

使用这里的提示我进一步测试以查看使用 python 代码可用的内容

import locale
loc_list = [(a,b) for a,b in locale.locale_alias.items() ]
loc_size = len(loc_list)
print loc_size,'entries'

for loc in loc_list:
    try:
        locale.setlocale(locale.LC_TIME, loc[1])
        print 'SUCCES set {:12} ({})'.format(loc[1],loc[0])
    except:
        pass

which yields

这产生

858 entries
SUCCES set en_US.UTF-8  (univ)
SUCCES set C            (c.ascii)
SUCCES set C            (c.en)
SUCCES set C            (posix-utf2)
SUCCES set C            (c)
SUCCES set C            (c_c)
SUCCES set C            (c_c.c)
SUCCES set en_IE.UTF-8  (en_ie.utf8@euro)
SUCCES set en_US.UTF-8  (universal.utf8@ucs4)
SUCCES set C            (posix)
SUCCES set C            (english_united-states.437)
SUCCES set en_US.UTF-8  (universal)

Of which only above is working! But the en_DK.UTF-8is not in this list, though it works!?!? What?? And the python generated locale list do contain a lot of combos of da and DK, which I am looking for, but again no UTF-8 for da/DK...

其中只有以上是有效的!但是en_DK.UTF-8它不在这个列表中,虽然它有效!?!?什么??并且 python 生成的语言环境列表确实包含很多我正在寻找的 da 和 DK 的组合,但同样没有用于 da/DK 的 UTF-8 ......

I am on a Point Linux distro (Debian based), and here localesays amongst other LC_TIME="en_DK.UTF-8", which I know works, but not the locale I need.

我在 Point Linux 发行版(基于 Debian)上,这里locale说其中包括LC_TIME="en_DK.UTF-8"我知道有效的 ,但不是我需要的语言环境。

locale -asays

locale -a

C
C.UTF-8
en_DK.utf8
en_US.utf8
POSIX

So definitely need to install other locale, which i did by editing /etc/locale.gen, uncomment needed line da_DK.UTF-8 UTF-8and run command locale-gen

所以肯定需要安装其他语言环境,我是通过编辑/etc/locale.gen、取消注释所需的行da_DK.UTF-8 UTF-8并运行命令来完成的locale-gen

Now locale.setlocale(locale.LC_TIME, 'da_DK.UTF-8')works too, and I can get my localized day and month names.

现在也locale.setlocale(locale.LC_TIME, 'da_DK.UTF-8')可以使用了,我可以获取本地化的日期和月份名称。

My Conclision:

我的结论:

Python : locale.locale_alias is not at all helpfull in finding available locales!!!

Python :locale.locale_alias 对查找可用的语言环境一点帮助都没有!!!

Linux : It is quite easy to get locale list and install new locale. A lot of help available.

Linux:获取语言环境列表并安装新的语言环境非常容易。很多帮助可用。

Windows : I have been investigating a little, but nothing conclusive. There are though posts leading to answers, but I have not felt the urge to pursue it.

Windows : 我一直在调查,但没有定论。虽然有一些帖子可以找到答案,但我没有追求它的冲动。