Python で curses を使いたいんですが

powered by Fotopedia

Pythoncurses を使いたく、試してみたら import でエラーになっちゃいました
こんな感じです 

[mitsuaki@localhost curses_test]$ cat cursestest.py 
# -*- coding: utf-8 -*-
import curses
[mitsuaki@localhost curses_test]$ python cursestest.py 
Traceback (most recent call last):
  File "cursestest.py", line 2, in 
    import curses
  File "/usr/local/lib/python2.7/curses/__init__.py", line 15, in 
    from _curses import *
ImportError: No module named _curses
[mitsuaki@localhost curses_test]$  

はい、残念!

調べたら ncurses-devel が入ってないからでした
OS は CentOS 5.7 x86_64 です

[root@localhost ~]# yum list | grep curses
ncurses.i386                             5.5-24.20060715               installed
ncurses.x86_64                           5.5-24.20060715               installed
ncurses-devel.i386                       5.5-24.20060715               base     
ncurses-devel.x86_64                     5.5-24.20060715               base     
php-ncurses.x86_64                       5.1.6-27.el5_7.5              updates  
[root@localhost ~]# 

 

試しに Python を make し直してみると、しっかり _curses が build できませんでしたとメッセージが出ていました。
そんなの全然目に入っていなかったw

m -f Lib/lib2to3/*Grammar*.pickle
[root@localhost Python-2.7.2]# make
gcc -pthread -c -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes  -I. -IInclude -I./Include   -DPy_BUILD_CORE -o Modules/python.o ./Modules/python.c
(コメント: 〜 省略 〜)
Python build finished, but the necessary bits to build these modules were not found:
_curses            _curses_panel      _tkinter        
bsddb185           dl                 gdbm            
imageop            readline           sunaudiodev     
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

running build_scripts
[root@localhost Python-2.7.2]# 

 

早速、 ncurses-devel を入れます

[root@localhost ~]# yum install ncurses-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: rsync.atworks.co.jp
 * extras: rsync.atworks.co.jp
 * updates: rsync.atworks.co.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package ncurses-devel.i386 0:5.5-24.20060715 set to be updated
---> Package ncurses-devel.x86_64 0:5.5-24.20060715 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================================================================================
 Package                                                   Arch                                               Version                                                     Repository                                        Size
=================================================================================================================================================================================================================================
Installing:
 ncurses-devel                                             i386                                               5.5-24.20060715                                             base                                             1.6 M
 ncurses-devel                                             x86_64                                             5.5-24.20060715                                             base                                             1.7 M

Transaction Summary
=================================================================================================================================================================================================================================
Install       2 Package(s)
Upgrade       0 Package(s)

Total download size: 3.3 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): ncurses-devel-5.5-24.20060715.i386.rpm                                                                                                                                                             | 1.6 MB     00:00     
(2/2): ncurses-devel-5.5-24.20060715.x86_64.rpm                                                                                                                                                           | 1.7 MB     00:00     
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                            2.6 MB/s | 3.3 MB     00:01     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : ncurses-devel                                                                                                                                                                                             1/2 
  Installing     : ncurses-devel                                                                                                                                                                                             2/2 

Installed:
  ncurses-devel.i386 0:5.5-24.20060715                                                                           ncurses-devel.x86_64 0:5.5-24.20060715                                                                          

Complete!
[root@localhost ~]# yum list | grep curses
ncurses.i386                             5.5-24.20060715               installed
ncurses.x86_64                           5.5-24.20060715               installed
ncurses-devel.i386                       5.5-24.20060715               installed
ncurses-devel.x86_64                     5.5-24.20060715               installed
php-ncurses.x86_64                       5.1.6-27.el5_7.5              updates  
[root@localhost ~]# 

再び Python を configure, make, make install。今度の make では、_curses が build できませんでしたのメッセージがなくなりました

(コメント: 〜 省略 〜)
Python build finished, but the necessary bits to build these modules were not found:
_tkinter           bsddb185           dl              
gdbm               imageop            readline        
sunaudiodev                                           
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

running build_scripts
[root@localhost Python-2.7.2]# 

やっとこさ curses が import ができました

[mitsuaki@localhost curses_test]$ python
Python 2.7.2 (default, Feb 22 2012, 21:16:51) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[mitsuaki@localhost curses_test]$ cat cursestest.py 
# -*- coding: utf-8 -*-
import curses
print("You could import curses module")
[mitsuaki@localhost curses_test]$ python cursestest.py 
You could import curses module
[mitsuaki@localhost curses_test]$ 

 めでたし、めでたし