问题:pip install-locale.Error:不支持的语言环境设置

完整的堆栈跟踪:

  ~ pip install virtualenv
Traceback (most recent call last):
  File "/usr/bin/pip", line 11, in <module>
    sys.exit(main())
  File "/usr/lib/python3.4/site-packages/pip/__init__.py", line 215, in main
    locale.setlocale(locale.LC_ALL, '')
  File "/usr/lib64/python3.4/locale.py", line 592, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

在同一台服务器上,我成功运行pip install virtualenv了python2.7.x。

现在,我刚刚使用安装了python3.4 curl https://bootstrap.pypa.io/get-pip.py | python3.4

  ~ pip --version
pip 8.1.1 from /usr/lib/python3.4/site-packages (python 3.4)

pip uninstall virtualenv 也会引发相同的错误

Full stacktrace:

➜  ~ pip install virtualenv
Traceback (most recent call last):
  File "/usr/bin/pip", line 11, in <module>
    sys.exit(main())
  File "/usr/lib/python3.4/site-packages/pip/__init__.py", line 215, in main
    locale.setlocale(locale.LC_ALL, '')
  File "/usr/lib64/python3.4/locale.py", line 592, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

On the same server, I successfully ran pip install virtualenv with python 2.7.x.

Now, I’ve just installed python3.4 using curl https://bootstrap.pypa.io/get-pip.py | python3.4.

➜  ~ pip --version
pip 8.1.1 from /usr/lib/python3.4/site-packages (python 3.4)

pip uninstall virtualenv throws the same error too


回答 0

根本原因是:您的环境变量LC_ALL丢失或以某种方式无效

简短答案-

只需运行以下命令:

$ export LC_ALL=C

如果在新的终端窗口中仍然出现错误,请在.bashrc文件底部添加错误。

长答案-

这是我的locale设置:

$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"
LC_ALL=C

Python2.7

    $ uname -a
    Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u6 (2015-11-09) x86_64 GNU/Linux
    $ python --version
    Python 2.7.9
    $ pip --version
    pip 8.1.1 from /usr/local/lib/python2.7/dist-packages (python 2.7)
    $ unset LC_ALL
    $ pip install virtualenv
    Traceback (most recent call last):
      File "/usr/local/bin/pip", line 11, in <module>
        sys.exit(main())
      File "/usr/local/lib/python2.7/dist-packages/pip/__init__.py", line 215, in main
        locale.setlocale(locale.LC_ALL, '')
      File "/usr/lib/python2.7/locale.py", line 579, in setlocale
        return _setlocale(category, locale)
    locale.Error: unsupported locale setting
    $ export LC_ALL=C
    $ pip install virtualenv
    Requirement already satisfied (use --upgrade to upgrade): virtualenv in /usr/local/lib/python2.7/dist-packages

The root cause is: your environment variable LC_ALL is missing or invalid somehow

Short answer-

just run the following command:

$ export LC_ALL=C

If you keep getting the error in new terminal windows, add it at the bottom of your .bashrc file.

Long answer-

Here is my locale settings:

$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"
LC_ALL=C

Python2.7

    $ uname -a
    Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u6 (2015-11-09) x86_64 GNU/Linux
    $ python --version
    Python 2.7.9
    $ pip --version
    pip 8.1.1 from /usr/local/lib/python2.7/dist-packages (python 2.7)
    $ unset LC_ALL
    $ pip install virtualenv
    Traceback (most recent call last):
      File "/usr/local/bin/pip", line 11, in <module>
        sys.exit(main())
      File "/usr/local/lib/python2.7/dist-packages/pip/__init__.py", line 215, in main
        locale.setlocale(locale.LC_ALL, '')
      File "/usr/lib/python2.7/locale.py", line 579, in setlocale
        return _setlocale(category, locale)
    locale.Error: unsupported locale setting
    $ export LC_ALL=C
    $ pip install virtualenv
    Requirement already satisfied (use --upgrade to upgrade): virtualenv in /usr/local/lib/python2.7/dist-packages

回答 1

运行以下命令(它将起作用):

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

Run the following command (it will work):

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

回答 2

有人可能会觉得有用。您可以将这些语言环境设置放在.bashrc文件中,该文件通常位于主目录中。
只需在.bashrc中添加此命令:
export LC_ALL=C
然后键入source .bashrc
Now,例如,当您通过ssh连接时,您无需每次都手动调用此命令。

Someone may find it useful. You could put those locale settings in .bashrc file, which usually located in the home directory.
Just add this command in .bashrc:
export LC_ALL=C
then type source .bashrc
Now you don’t need to call this command manually every time, when you connecting via ssh for example.


回答 3

尽管可以设置导出环境变量的语言环境,但每次启动会话时都必须这样做。以这种方式设置语言环境将永久解决问题:

sudo apt-get install locales
sudo locale-gen en_US.UTF-8
sudo echo "LANG=en_US.UTF-8" > /etc/default/locale

While you can set the locale exporting an env variable, you will have to do that every time you start a session. Setting a locale this way will solve the problem permanently:

sudo apt-get install locales
sudo locale-gen en_US.UTF-8
sudo echo "LANG=en_US.UTF-8" > /etc/default/locale

回答 4

[此答案仅适用于linux平台]

您应该知道的第一件事是,大多数语言环境配置文件的定位路径都可以从localedef --help以下位置获取:

$ localedef --help | tail -n 5
System's directory for character maps : /usr/share/i18n/charmaps
                       repertoire maps: /usr/share/i18n/repertoiremaps
                       locale path    : /usr/lib/locale:/usr/share/i18n
For bug reporting instructions, please see:
<https://bugs.launchpad.net/ubuntu/+source/glibc/+bugs>

看到最后一个/usr/share/i18n?这是您的xx_XX.UTF-8配置文件所在的位置:

$ ls /usr/share/i18n/locales/zh_*
/usr/share/i18n/locales/zh_CN  /usr/share/i18n/locales/zh_HK  /usr/share/i18n/locales/zh_SG  /usr/share/i18n/locales/zh_TW

怎么办 ?我们需要将它们编译为存档二进制文件。一种方式,例如假设我有/usr/share/i18n/locales/en_LOVE,我可以将其添加到编译列表(即/etc/locale-gen文件)中:

$ tail -1 /etc/locale.gen 
en_LOVE.UTF-8 UTF-8

并将其编译为二进制sudo locale-gen

$ sudo locale-gen 
Generating locales (this might take a while)...
  en_AG.UTF-8... done
  en_AU.UTF-8... done
  en_BW.UTF-8... done
  ...
  en_LOVE.UTF-8... done
Generation complete.

且以期望现在更新系统默认的语言环境LANGLC_ALL…等与此update-locale

sudo update-locale LANG=en_LOVE.UTF-8

update-locale实际上还意味着更新此/etc/default/locale文件,该文件将在登录时由系统提供以设置环境变量:

$ head /etc/default/locale 
#  File generated by update-locale
LANG=en_LOVE.UTF-8
LC_NUMERIC="en_US.UTF-8"
...

但是我们可能不想重启才能生效,因此我们可以将其来源到当前shell会话中的环境变量:

$ . /etc/default/locale

怎么sudo dpkg-reconfigure locales样 如果您玩转它,您将知道此命令基本上充当GUI来简化上述步骤,即Edit /etc/locale.gen-> sudo locale-gen->sudo update-locale LANG=en_LOVE.UTF-8

对于python,只要/etc/locale.gen包含该语言环境候选者并locale.gen进行编译,就setlocale(category, locale)可以正常运行locale.Error: unsupoorted locale setting。您可以通过观察文件来检查要在中设置的正确字符串en_US.UTF-8/ ,然后取消注释并根据需要对其进行编译。该文件中不带点的表示正确的字符串是和。en_US/....etcsetlocale()/etc/locale.genzh_CN GB2312zh_CNzh_CN.GB2312

[This answer is target on linux platform only]

The first thing you should know is most of the locale config file located path can be get from localedef --help :

$ localedef --help | tail -n 5
System's directory for character maps : /usr/share/i18n/charmaps
                       repertoire maps: /usr/share/i18n/repertoiremaps
                       locale path    : /usr/lib/locale:/usr/share/i18n
For bug reporting instructions, please see:
<https://bugs.launchpad.net/ubuntu/+source/glibc/+bugs>

See the last /usr/share/i18n ? This is where your xx_XX.UTF-8 config file located:

$ ls /usr/share/i18n/locales/zh_*
/usr/share/i18n/locales/zh_CN  /usr/share/i18n/locales/zh_HK  /usr/share/i18n/locales/zh_SG  /usr/share/i18n/locales/zh_TW

Now what ? We need to compile them into archive binary. One of the way, e.g. assume I have /usr/share/i18n/locales/en_LOVE, I can add it into compile list, i.e. /etc/locale-gen file:

$ tail -1 /etc/locale.gen 
en_LOVE.UTF-8 UTF-8

And compile it to binary with sudo locale-gen:

$ sudo locale-gen 
Generating locales (this might take a while)...
  en_AG.UTF-8... done
  en_AU.UTF-8... done
  en_BW.UTF-8... done
  ...
  en_LOVE.UTF-8... done
Generation complete.

And now update the system default locale with desired LANG, LC_ALL …etc with this update-locale:

sudo update-locale LANG=en_LOVE.UTF-8

update-locale actually also means to update this /etc/default/locale file which will source by system on login to setup environment variables:

$ head /etc/default/locale 
#  File generated by update-locale
LANG=en_LOVE.UTF-8
LC_NUMERIC="en_US.UTF-8"
...

But we may not want to reboot to take effect, so we can just source it to environment variable in current shell session:

$ . /etc/default/locale

How about sudo dpkg-reconfigure locales ? If you play around it you will know this command basically act as GUI to simplify the above steps, i.e. Edit /etc/locale.gen -> sudo locale-gen -> sudo update-locale LANG=en_LOVE.UTF-8

For python, as long as /etc/locale.gen contains that locale candidate and locale.gen get compiled, setlocale(category, locale) should work without throws locale.Error: unsupoorted locale setting. You can check the correct string en_US.UTF-8/en_US/....etc to be set in setlocale(), by observing /etc/locale.gen file, and then uncomment and compile it as desired. zh_CN GB2312 without dot in that file means the correct string is zh_CN and zh_CN.GB2312.


回答 5

对于Dockerfile,这对我有用:

RUN locale-gen en_US.UTF-8  
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8  

如何安装locale-gen?

docker ubuntu / bin / sh:1:locale-gen:找不到

For Dockerfile, this works for me:

RUN locale-gen en_US.UTF-8  
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8  

How to install locale-gen?

docker ubuntu /bin/sh: 1: locale-gen: not found


回答 6

我有同样的问题,对我没有用"export LC_ALL=c"

尝试export LC_ALL="en_US.UTF-8"(它将起作用)。

I had the same problem, and "export LC_ALL=c" didn’t work for me.

Try export LC_ALL="en_US.UTF-8" (it will work).


回答 7

该错误消息表示语言环境设置有问题。要解决此问题(如其他答案所示),您需要修改语言环境。

在Mac OS X Sierra上,我发现最好的方法是~/bash_profile按如下所示修改文件:

export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"

除非您使用以下命令重新加载bash配置文件,否则此更改在当前cli会话中不会立即显现source ~/.bash_profile

这个答案非常接近我发布给其他非相同,非重复的问题(即与pipenv不相关)的答案,但是碰巧需要相同的解决方案。

致主持人:尊敬;由于这个原因,我以前的答案被删除了,但是我觉得这有点愚蠢,因为实际上,无论何时只要错误是“语言环境问题”,这个答案都适用…但是可能会触发多种不同的情况,语言和环境该错误。

因此,A)将问题标记为重复项是没有意义的,而B)调整答案也没有意义,因为解决方法非常简单,在每种情况下都是相同的,并且无法从修饰中受益。

The error message indicates a problem with the locale setting. To fix this as indicated by other answers you need to modify your locale.

On Mac OS X Sierra I found that the best way to do this was to modify the ~/bash_profile file as follows:

export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"

This change will not be immediately evident in your current cli session unless you reload the bash profile by using: source ~/.bash_profile.

This answer is pretty close to answers that I’ve posted to other non-identical, non-duplicate questions (i.e. not related to pipenv) but which happen to require the same solution.

To the moderator: With respect; my previous answer got deleted for this reason but I feel that was a bit silly because really this answer applies almost whenever the error is “problem with locale”… but there are a number of differing situations, languages, and environments which could trigger that error.

Thus it A) doesn’t make sense to mark the questions as duplicates and B) doesn’t make sense to tailor the answer either because the fix is very simple, is the same in each case and does not benefit from ornamentation.


回答 8

Ubuntu:

$ sudo vi /etc/default/locale

在文件末尾添加以下设置。

LC_ALL = en_US.UTF-8

Ubuntu:

$ sudo vi /etc/default/locale

Add below setting at the end of file.

LC_ALL = en_US.UTF-8


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。