我尝试编译和安装支持curses的Python,但是一直失败了。我已经尝试了各种编译标志等迭代操作,但似乎无法使其正常工作。
这是在Solaris 11上,Python版本为3.4.3:
首先,这里是问题所在:
Python 3.4.3 (default, Mar 3 2015, 14:43:41)
[GCC 4.5.2] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import curses
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/pkg/python/3.4.3/lib/python3.4/curses/__init__.py", line 13, in <module>
from _curses import *
ImportError: No module named '_curses'
一些谷歌用户告诉我,造成这个问题的原因是Python编译时未找到ncurses库。
所以我从ftp://ftp.gnu.org/gnu/ncurses/(5.9)安装了curses到/usr/local/pkg/ncurses/5.9,并尝试重新编译Python,并传入ncurses的位置:
export LDFLAGS=-L/usr/local/pkg/ncurses/5.9/lib
export CPPFLAGS="-I/usr/local/pkg/ncurses/5.9/include -I/usr/local/pkg/ncurses/5.9/include/ncurses"
./configure --enable-shared --prefix=/usr/local/pkg/python/3.4.3
make
make install
看起来应该可以了。config.log 没有显示任何失败。
checking curses.h usability... yes
checking curses.h presence... yes
checking for curses.h... yes
checking ncurses.h usability... yes
checking ncurses.h presence... yes
checking for ncurses.h... yes
我也尝试使用Solaris packaging工具OpenCSW来安装这些软件包:
libncurses5 CSWlibncurses5 5.9,REV=2011.11.21 298.2 KB
libncurses_dev CSWlibncurses-dev 5.9,REV=2011.11.21 258.4 KB
libncursesw5 CSWlibncursesw5 5.9,REV=2011.11.21 341.5 KB
mutt_ncurses CSWmutt-ncurses 1.5.23,REV=2014.03.14 484.4 KB
ncurses CSWncurses 5.9,REV=2011.11.21 123.1 KB
接下来我尝试了:
export LDFLAGS=-L/opt/csw/lib
export CPPFLAGS="-I/opt/csw/include -I/opt/csw/include/ncursesw"
./configure --enable-shared --prefix=/usr/local/pkg/python/3.4.3
make
make install
无论哪种情况,情况都是一样的:
ImportError: No module named '_curses'
编辑更新:
我刚意识到 curses 模块是在 "make install" 期间构建的,而不是在 "make" 期间,因此我错过了一些关键错误。特别是在使用 /usr/local/pkg 安装的 ncurses 时:
building '_curses' extension
gcc -fPIC -Wno-unused-result -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Werror=declaration-after-statement -I./Include -I. -IInclude -I/usr/local/pkg/ncurses/5.9/include -I/usr/local/pkg/ncurses/5.9/include/ncurses -I/usr/local/include -I/tmp/Python-3.4.3/Include -I/tmp/Python-3.4.3 -c /tmp/Python-3.4.3/Modules/_cursesmodule.c -o build/temp.solaris-2.11-i86pc.32bit-3.4/tmp/Python-3.4.3/Modules/_cursesmodule.o
gcc -shared -L/usr/local/pkg/ncurses/5.9/lib -L/usr/local/pkg/ncurses/5.9/lib -L/usr/local/pkg/ncurses/5.9/lib -I/usr/local/pkg/ncurses/5.9/include -I/usr/local/pkg/ncurses/5.9/include/ncurses build/temp.solaris-2.11-i86pc.32bit-3.4/tmp/Python-3.4.3/Modules/_cursesmodule.o -L. -L/usr/local/pkg/python/3.4.3/lib -L/usr/local/pkg/ncurses/5.9/lib -L/usr/local/lib -lncurses -lpython3.4m -o build/lib.solaris-2.11-i86pc.32bit-3.4/_curses.so
Text relocation remains referenced
against symbol offset in file
.rodata (section) 0x50 /usr/local/pkg/ncurses/5.9/lib/libncurses.a(lib_color.o)
... (followed by hundreds more lines like the above)
ld: fatal: relocations remain against allocatable but non-writable sections
collect2: ld returned 1 exit status
Failed to build these modules:
_curses _curses_panel readline
特别是在使用OpenCSW安装ncurses时:
building '_curses' extension
gcc -fPIC -Wno-unused-result -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Werror=declaration-after-statement -DHAVE_NCURSESW=1 -I/usr/include/ncursesw -I./Include -I. -IInclude -I/opt/csw/include -I/opt/csw/include/ncursesw -I/usr/local/include -I/tmp/Python-3.4.3/Include -I/tmp/Python-3.4.3 -c /tmp/Python-3.4.3/Modules/_cursesmodule.c -o build/temp.solaris-2.11-i86pc.32bit-3.4/tmp/Python-3.4.3/Modules/_cursesmodule.o
/tmp/Python-3.4.3/Modules/_cursesmodule.c:281:29: error: expected declaration specifiers or ‘...’ before ‘cchar_t’
/tmp/Python-3.4.3/Modules/_cursesmodule.c: In function ‘PyCurses_ConvertToCchar_t’:
/tmp/Python-3.4.3/Modules/_cursesmodule.c:299:16: error: ‘wch’ undeclared (first use in this function)
/tmp/Python-3.4.3/Modules/_cursesmodule.c:299:16: note: each undeclared identifier is reported only once for each function it appears in
/tmp/Python-3.4.3/Modules/_cursesmodule.c: In function ‘curses_window_addch_impl’:
(...and many more messages like this)
Failed to build these modules:
_curses _curses_panel readline
cd /usr/lib; ls -l libncurses*
的输出是什么? - l'L'l-I/usr/local
添加到CPPFLAGS
,并将-L/usr/lib
添加到LDFLAGS
;看起来你已经合理地设置好了一切。 - l'L'l