简体   繁体   English

Mac版本的Python在curses模块中不支持UTF-8吗?

[英]Mac version of Python doesn't support UTF-8 in curses module?

I'm trying to display a lot of unicode text in my curses application. 我正在尝试在curses应用程序中显示很多unicode文本。 My development machine is MacOSx 10.6 and I use the default python shipped with Apple. 我的开发机器是MacOSx 10.6,我使用Apple随附的默认python。

Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin

When I added unicode text to the screen, the screen all messed up. 当我在屏幕上添加unicode文本时,屏幕全部混乱了。 I tried to Google for solution and other people has suggested to link the _curses module with libncursesw library instead of libncurse library. 我尝试去Google寻找解决方案,其他人建议将_curses模块与libncursesw库而不是libncurse库链接。

I checked my path and I found (see below) in /opt/local/lib 我检查了路径,然后在/ opt / local / lib中找到了(见下文)

libncurses++.a
libncurses++w.a
libncurses.5.dylib
libncurses.a
libncurses.dylib
libncursesw.5.dylib
libncursesw.a
libncursesw.dylib

How do I check which library my curses module linked to, and how can I link against other library? 如何检查我的curses模块链接到哪个库,以及如何链接到其他库? Is it possible to do it without recompile my Python? 是否可以在不重新编译Python的情况下做到这一点?


This is kind of embarrassed, but I figure the solution to print unicode properly in my environment. 这有点尴尬,但是我想出了在我的环境中正确打印unicode的解决方案。 I think at some point time I did install curses libraries from Macports and forgot I have it already. 我想有时候我确实从Macports安装了curses库,却忘记了我已经拥有了。

The problem that the text did not display the first time is because I need to set the locale within my python program. 文本第一次不显示的问题是因为我需要在python程序中设置语言环境。 I thought the locale setting would inherit from the shell I'm running, but simply added two lines of code fixed my problem: 我以为语言环境设置将从我正在运行的外壳程序中继承,但是只需添加两行代码即可解决我的问题:

import locale
locale.setlocale(locale.LC_ALL,"")

Though, it's good to know where the python external library lives and how to check them. 不过,很高兴知道python外部库在哪里以及如何检查它们。

To check which other .so sa .so uses, use otool -L -- for example: 要检查其他.so sa .so使用哪个,请使用otool -L例如:

$ otool -L /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/_curses.so
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/_curses.so:
    /Library/Frameworks/Python.framework/Versions/2.6/lib/libncurses.5.dylib (compatibility version 5.0.0, current version 5.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 88.3.10)

This is what I have on my Python 2.6.4 install on OSX 10.5 -- since you're using Apple's own Python on 10.6, the exact location of your _curses.so will be different, just do 这就是我在OSX 10.5上安装的Python 2.6.4上的内容-由于您在10.6上使用了Apple自己的Python,因此_curses.so的确切位置会有所不同,只需执行

$ python
>>> import _curses
>>> _curses.__file__

to see exactly where the _curses.so of interest in, then call otool -L on it. 要查看_curses.so的确切位置,然后在其上调用otool -L

Replacing a .so on the system-installed Python seems fraught with danger to me -- you could break something and end up having to reinstall the OS, etc. Why not install a Python download from python.org instead? 在系统安装的Python上替换.so似乎对我充满危险-您可能会破坏某些东西并最终不得不重新安装OS等。为什么不从python.org安装Python下载呢?

Get both the .dmg and the sources for the most recent release of 2.6 (unless you're so adventurous you want to try a release candidate 2.7;-), then you can install the .dmg (it will go to /usr/local , not overwriting the system Python; set your PATH appropriately in your .bashrc or wherever to have /usr/local/bin in your PATH ahead of /usr/bin ), then, if your problem persists, you can rebuild from sources with whatever options you want, and replace the specific _curses.so in the local install, without disturbing the system directory at all (seems most prudent to me...). 同时获取.dmg和2.6的最新版本的资源(除非您冒险,除非您想尝试2.7版的候选发布;-),然后可以安装.dmg (它将转到/usr/local而不是覆盖系统Python;请在.bashrc或任何地方的PATH中适当地设置PATH,以在/usr/local/bin之前的PATH中包含/usr/local/bin /usr/bin ),然后,如果问题仍然存在,则可以使用任何所需的选项,并在本地安装中替换特定的_curses.so ,而完全不干扰系统目录(对我来说似乎最谨慎...)。

The Apple-supplied Python 2.6 shipped with OS X 10.6 resides here: OS X 10.6随附的Apple提供的Python 2.6位于以下位置:

$ cd /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload
$ otool -L _curses*
_curses.so:
    /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.1)
_curses_panel.so:
    /usr/lib/libpanel.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.1)

It would be a very bad idea to try to modify anything in /System/Library as that could break OS X and/or be wiped out by a system update. 尝试修改/System/Library任何内容将是一个非常糟糕的主意,因为这可能会破坏OS X和/或被系统更新清除。 If you need to relink, build your own Python from scratch or start with Homebrew , MacPorts , or Fink . 如果需要重新链接,请从头开始构建自己的Python或从HomebrewMacPortsFink开始

EDIT: 编辑:

It appears that the current MacPorts Python 2.6 install uses libncursesw so installing it may be the simplest solution: 看来当前的MacPorts Python 2.6安装使用libncursesw因此安装它可能是最简单的解决方案:

$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/
$ otool -L _curses*
_curses.so:
    /opt/local/lib/libncursesw.5.dylib (compatibility version 5.0.0, current version 5.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)
_curses_panel.so:
    /opt/local/lib/libpanelw.5.dylib (compatibility version 5.0.0, current version 5.0.0)
    /opt/local/lib/libncursesw.5.dylib (compatibility version 5.0.0, current version 5.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)

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

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