python locale奇怪的错误。这到底发生了什么?

时间:2009-12-02 02:57:28

标签: python macos osx-snow-leopard locale

所以今天我升级到了集市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.
很奇怪,因为我的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

导出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.

然而,这解决了问题

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')

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

3 个答案:

答案 0 :(得分:17)

2016更新:事实证明至少从2013年开始this is a Python bug,非常可能更早,包括Python对非GNU语言环境的反应不好 - 就像在Mac OS X和BSD中找到的那样。该漏洞在2016年9月仍然开放,并影响每个Python版本。


如果没有设置LANG环境变量,则可能有LC_CTYPE(键变量)或LC_ALL(如果设置则覆盖)环境变量设置为{{ 1}},这不是有效的OS X语言环境。使用Apple提供的UTF-8或自定义python(如您的情况)使用10.6 SDK(可能还有10.5 SDK)构建,很容易。你将无法用python.org python以这种方式重现它;它们目前使用10.4 SDK构建,其中区域设置API的行为方式不同。

/usr/bin/python

编辑:

这个谜题可能还有另外一块。快速查看我已安装的$ 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') >>> 2.0.1表示,只有当bzr引发locale.getpreferredencoding()时,您引用的邮件才会显示。可能发生的一种方法是,如果无法加载python locale.Error C扩展,并且如果存在权限问题,则可能发生这种情况。例如,目前已知MacPorts具有problems setting permissions if you have a customized umask;我自己被这个问题烧伤了。检查python _locale.so目录中_locale.so的权限,并确保它是lib/python2.5/lib-dynload。 MacPorts的完整路径应为:

755

答案 1 :(得分:7)

我遇到了同样的问题。当我运行locale时,我注意到LANGLC_ALL未设置。所以我通过在.bash_profile文件中添加以下行来修复此问题:

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

然后我就跑了:

source ~/.bash_profile 

此问题已在我的Mac上修复。

答案 2 :(得分:4)

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

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