Python pip install - locale.Error: 不支持的区域设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36394101/
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-19 17:49:46  来源:igfitidea点击:

pip install - locale.Error: unsupported locale setting

pythonpython-3.xcentospip

提问by ericn

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 virtualenvwith python 2.7.x.

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

Now, I've just installed python3.4 using curl https://bootstrap.pypa.io/get-pip.py | 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 virtualenvthrows the same error too

pip uninstall virtualenv也抛出同样的错误

回答by ROY

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

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

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 .bashrcfile.

如果您在新的终端窗口中不断收到错误消息,请将其添加到.bashrc文件底部。

Long answer-

长答案-

Here is my localesettings:

这是我的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

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

回答by Rahi.Shah

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

回答by Ivan Semochkin

Someone may find it useful. You could put that locale settings in .bashrc file, which usually locate in 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.

有人可能会发现它很有用。您可以将该区域设置放在 .bashrc 文件中,该文件通常位于主目录中。
只需在 .bashrc 中添加此命令:
export LC_ALL=C
然后键入source .bashrc
Now 您不需要每次都手动调用此命令,例如,当您通过 ssh 连接时。

回答by Marco Lavagnino

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

回答by Fruit

[This answer is target on linux platform only]

[此答案仅针对 linux 平台]

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

您应该知道的第一件事是大多数语言环境配置文件所在的路径都可以从localedef --help

$ localedef --help | tail -n 5
System's directory for character maps : /usr/share/i18n/charmaps
                       reperttheitroade maps: /usr/share/i18n/reperttheitroademaps
                       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:

看到最后了/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

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-genfile:

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

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

And compile it to binary with sudo locale-gen:

并使用以下命令将其编译为二进制文件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:

现在更新系统默认语言环境所需的LANGLC_ALL...等与此update-locale

sudo update-locale LANG=en_LOVE.UTF-8

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

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"
...

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

但是我们可能不希望重启生效,所以我们可以在当前 shell 会话中将其 source 到环境变量:

$ . /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

怎么样sudo dpkg-reconfigure locales?如果你玩过它,你就会知道这个命令基本上充当 GUI 来简化上述步骤,即编辑/etc/locale.gen-> sudo locale-gen->sudo update-locale LANG=en_LOVE.UTF-8

For python, as long as /etc/locale.gencontains that locale candidate and locale.genget 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/....etcto be set in setlocale(), by observing /etc/locale.genfile, and then uncomment and compile it as desired. zh_CN GB2312without dot in that file means the correct string is zh_CNand zh_CN.GB2312.

对于 python,只要/etc/locale.gen包含该候选语言环境并被locale.gen编译,就setlocale(category, locale)应该可以在没有 throws 的情况下工作locale.Error: unsupoorted locale setting。您可以通过观察文件来检查要设置的正确字符串en_US.UTF-8/ ,然后根据需要取消注释并编译它。该文件中没有点表示正确的字符串是and 。en_US/....etcsetlocale()/etc/locale.genzh_CN GB2312zh_CNzh_CN.GB2312

回答by Ben

For Dockerfile, this works for me:

对于 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  

How to install locale-gen?

如何安装locale-gen?

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

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

回答by Benyamin Jafari

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

我有同样的问题,对我不起作用"export LC_ALL=c"

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

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

回答by geoidesic

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_profilefile as follows:

在 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"

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

除非您使用以下命令重新加载 bash 配置文件,否则此更改不会在您当前的 cli 会话中立即显现: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.

这个答案与我发布到其他不相同、不重复的问题(即与 pipenv 无关)但恰好需要相同解决方案的答案非常接近。

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.

因此,A) 将问题标记为重复是没有意义的,B) 定制答案也没有意义,因为修复非常简单,在每种情况下都是相同的,并且不会从装饰中受益。

回答by saneryee

Ubuntu:

Ubuntu:

$ sudo vi /etc/default/locale

$ sudo vi /etc/default/locale

Add below setting at the end of file.

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

LC_ALL = en_US.UTF-8

LC_ALL = en_US.UTF-8