简体   繁体   English

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

[英]pip install - locale.Error: unsupported locale setting

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.在同一台服务器上,我使用 python 2.7.x 成功运行了pip install virtualenv

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安装了 python3.4 curl https://bootstrap.pypa.io/get-pip.py | python3.4 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 virtualenv throws the same error too pip uninstall virtualenv抛出同样的错误

The root cause is: your environment variable LC_ALL is 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 .bashrc file.如果您在新的终端窗口中不断收到错误消息,请将其添加到.bashrc文件的底部。

Long answer-长答案-

Here is my locale settings:这是我的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

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

Someone may find it useful.有人可能会发现它很有用。 You could put those locale settings in .bashrc file, which usually located in the home directory.您可以将这些语言环境设置放在 .bashrc 文件中,该文件通常位于主目录中。
Just add this command in .bashrc:只需在 .bashrc 中添加此命令:
export LC_ALL=C
then type source .bashrc然后输入source .bashrc
Now you don't need to call this command manually every time, when you connecting via ssh for example.现在你不需要每次都手动调用这个命令,例如当你通过 ssh 连接时。

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

[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
                       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 ?看到最后的/usr/share/i18n吗? This is where your xx_XX.UTF-8 config file located:这是您的 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, eg assume I have /usr/share/i18n/locales/en_LOVE , I can add it into compile list, ie /etc/locale-gen file:一种方法,例如假设我有/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-locale actually also means to update this /etc/default/locale file 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 ? sudo dpkg-reconfigure locales怎么样? If you play around it you will know this command basically act as GUI to simplify the above steps, ie Edit /etc/locale.gen -> sudo locale-gen -> sudo update-locale LANG=en_LOVE.UTF-8如果你玩过它,你就会知道这个命令基本上充当 GUI 来简化上述步骤,即编辑/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 .对于 python,只要/etc/locale.gen包含候选语言环境并且locale.gen被编译, setlocale(category, locale)应该可以正常工作而不会抛出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.您可以通过观察/etc/locale.gen文件来检查要在setlocale()设置的正确字符串en_US.UTF-8 / en_US/....etc ,然后根据需要取消注释并编译它。 zh_CN GB2312 without dot in that file means the correct string is zh_CN and zh_CN.GB2312 .该文件中没有点的zh_CN GB2312表示正确的字符串是zh_CNzh_CN.GB2312

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: 未找到

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" (它会起作用)。

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:在 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 (ie 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) 定制答案也没有意义,因为修复非常简单,在每种情况下都是相同的,并且不会从装饰中受益。

Ubuntu: Ubuntu:

$ sudo vi /etc/default/locale

Add below setting at the end of file.在文件末尾添加以下设置。

LC_ALL = en_US.UTF-8

Had a similar problem on Fedora:在 Fedora 上也有类似的问题:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.10/locale.py", line 620, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

It turned out that the "glibc-all-langpacks" package was missing.结果发现“glibc-all-langpacks”包丢失了。

dnf install -y glibc-all-langpacks

solved it.解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM