macos python语言环境奇怪的错误。这是怎么回事?

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

python locale strange error. what's going on here exactly?

pythonmacososx-snow-leopardlocale

提问by Stefano Borini

So today I upgraded to bazaar 2.0.2, and I started receiving this message (I'm on snow leopard, btw):

所以今天我升级到bazaar 2.0.2,我开始收到这条消息(我在雪豹上,顺便说一句):

bzr: warning: unknown locale: UTF-8
  Could not determine what text encoding to use.
  This error usually means your Python interpreter
  doesn't support the locale set by $LANG (en_US.UTF-8)
  Continuing with ascii encoding.

very strange, since my LANG is actually empty. Similar thing happen when I try to tinker with the locale module

很奇怪,因为我的 LANG 实际上是空的。当我尝试修改语言环境模块时会发生类似的事情

Python 2.5.4 (r254:67916, Nov 30 2009, 14:09:22) 
[GCC 4.3.4] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.getdefaultlocale()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sbo/runtimes/lib/python2.5/locale.py", line 443, in getdefaultlocale
    return _parse_localename(localename)
  File "/Users/sbo/runtimes/lib/python2.5/locale.py", line 375, in _parse_localename
    raise ValueError, 'unknown locale: %s' % localename
ValueError: unknown locale: UTF-8

exporting LANG does not help

导出 LANG 没有帮助

sbo@dhcp-045:~ $ export LANG=en_US.UTF-8
sbo@dhcp-045:~ $ bzr
bzr: warning: unknown locale: UTF-8
  Could not determine what text encoding to use.
  This error usually means your Python interpreter
  doesn't support the locale set by $LANG (en_US.UTF-8)
  Continuing with ascii encoding.

However, this solved the problem

然而,这解决了问题

sbo@dhcp-045:~ $ export LANG=en_US.UTF-8
sbo@dhcp-045:~ $ export LC_ALL=en_US.UTF-8

Python 2.5.4 (r254:67916, Nov 30 2009, 14:09:22) 
[GCC 4.3.4] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.getdefaultlocale()
('en_US', 'UTF8')

Could you please explain what's going on here, for better googlability ?

你能解释一下这里发生了什么,以获得更好的可搜索性吗?

采纳答案by Ned Deily

2016 UPDATE: Turns out that this is a Python bugsince at least 2013, very probably earlier too, consisting in Python not reacting well to non-GNU locales - like those found in Mac OS X and the BSDs. The bug is still open as of September 2016, and affects every Python version.

2016 年更新:事实证明,这是一个 Python 错误,至少自 2013 年以来,很可能也更早,包括 Python 对非 GNU 语言环境的反应不佳 - 就像在 Mac OS X 和 BSD 中发现的那些。截至 2016 年 9 月,该错误仍然存​​在,并影响每个 Python 版本。



If there was no LANGenvironment variable set, chances are you had either an LC_CTYPE(the key variable) or LC_ALL(which overrides if set) environment variable set to UTF-8, which is not a valid OS X locale. It's easy enough to reproduce with the Apple-supplied /usr/bin/pythonor with a custom python, as in your case, that was built with the 10.6 SDK (probably also the 10.5 SDK). You won't be able to reproduce it that way with a python.org python; they are currently built with the 10.4 SDK where the locale APIs behave differently.

如果没有LANG设置环境变量,则您可能将LC_CTYPE(关键变量)或LC_ALL(如果设置则覆盖)环境变量设置为UTF-8,这不是有效的 OS X 语言环境。使用 Apple 提供的/usr/bin/python或自定义的 Python进行复制很容易,就像您的情况一样,它是使用 10.6 SDK(可能也是 10.5 SDK)构建的。您将无法使用 python.org python 以这种方式重现它;它们目前是使用 10.4 SDK 构建的,其中语言环境 API 的行为有所不同。

$ unset LANG
$ env | grep LC_
$ export LC_CTYPE="UTF-8"
$ /usr/bin/python  # Apple-supplied python
Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale ; locale.getdefaultlocale()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/locale.py", line 459, in getdefaultlocale
    return _parse_localename(localename)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/locale.py", line 391, in _parse_localename
    raise ValueError, 'unknown locale: %s' % localename
ValueError: unknown locale: UTF-8
^D
$ /usr/local/bin/python2.6   # python.org python
Python 2.6.4 (r264:75821M, Oct 27 2009, 19:48:32) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale ; locale.getdefaultlocale()
(None, 'mac-roman')
>>> 

EDIT:

编辑:

There may be another piece to the puzzle. A quick look at the bzr2.0.1 I have installed indicates that the message you cite should only show up if locale.getpreferredencoding()raises a locale.Error. One way that can happen is if the python _locale.soC extension can't be loaded and that can happen if there are permission problems on it. For example, MacPorts currently is known to have problems setting permissions if you have a customized umask; I've been burned by that issue myself. Check the permissions of _locale.soin the python lib/python2.5/lib-dynloaddirectory and ensure it is 755. The full path for MacPorts should be:

可能还有另一块拼图。快速浏览一下bzr我安装的2.0.1 表明您引用的消息应该只在locale.getpreferredencoding()引发locale.Error. 可能发生的一种情况是,如果_locale.so无法加载python C 扩展,并且如果存在权限问题,就会发生这种情况。例如,如果您有自定义的 umask,MacPorts 目前已知在设置权限时出现问题;我自己也被这个问题烧焦了。检查_locale.sopythonlib/python2.5/lib-dynload目录中的权限并确保它是755. MacPorts 的完整路径应该是:

/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload/

回答by Archit Kapoor

I faced the same problem. When I ran locale, I noticed that the LANGand LC_ALLwere unset. So I fixed this by adding the following lines in the .bash_profilefile:

我遇到了同样的问题。当我运行时locale,我注意到LANG和 未LC_ALL设置。所以我通过在.bash_profile文件中添加以下几行来解决这个问题:

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

Then I simply ran:

然后我简单地跑了:

source ~/.bash_profile 

And this issue was fixed thereafter on my Mac.

此问题此后在我的 Mac 上得到解决。

回答by u0b34a0f6ae

It's a Mac OS X problem. To see your locale settings, run localein terminal. locale -ashould list all locales that you have defined (that you may use as argument to LC_ALL).

这是一个 Mac OS X 问题。要查看您的区域设置,请locale在终端中运行。locale -a应该列出您定义的所有语言环境(您可以将其用作 LC_ALL 的参数)。

Notice that LC_ALLand other LC_*variables take precedence over LANGwhen defined.

请注意,LC_ALL和其他LC_*变量LANG在定义时优先。