为什么我在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

这也适用于其他地区,如fr或nl。我用的是Ubuntu 11.04。

更新:做以下事情没有任何结果:

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

当前回答

如果刚刚添加了新的区域设置,则可能发生此错误。您需要重新启动python交互式shell (quit()和python)才能访问它。

其他回答

把它放在ENV上面的Dockerfile中。

# make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default
RUN apt-get update && apt-get install -y locales && rm -rf /var/lib/apt/lists/* \
    && localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8

ENV LANG en_US.UTF-8

首先,通过以下步骤确保你已经安装了语言包:

sudo apt-get install language-pack-en-base


sudo dpkg-reconfigure locales

python寻找.UFT-8,但你可能有.utf8 尝试使用sudo dpkg-reconfigure locale安装.UFT-8包

根据这个链接,输入如下命令进行求解:

出口LC_ALL = C

如果刚刚添加了新的区域设置,则可能发生此错误。您需要重新启动python交互式shell (quit()和python)才能访问它。