blog.hekt.org

試行錯誤しつつwxPythonをインストール

PythonのGUIツールキット、wxPythonをインストールしてみました。が、かなり紆余曲折を経てのインストールになったので、同じ事をやろうとする場合は一度最後まで読んでからにしたほうがいいと思います。

インストール

Pythonのモジュールをインストールするときはeasy_installが楽、ってことで実行。

$ easy_install wxPython
 ...
error: Couldn't find a setup script in /var/.../wxPython-src-2.9.1.1.tar.bz2

おや?

ググってみたら、wxPythonのsetup.pyはwxPython-src-x.x.x.x/wxPython/にあるらしいので、直接実行してみる。

$ cd ~/src
$ wget http://downloads.sourceforge.net/wxpython/wxPython-src-2.9.1.1.tar.bz2
$ tar xjf wxPython-src-2.9.1.1.tar.bz2
$ cd wxPython-src-2.9.1.1/wxPython
$ sudo python setup.py install
 ...
error: command '-fno-strict-aliasing' failed with exit status 1

あら……?

よくわからないのでダウンロードページからパッケージを落としてきてインストール。最初からこうすればよかった。

ところで話はそれますが、僕がcURLじゃなくてWget使っているのは、curl http://...とやってしまって(-Oをつけ忘れて)、保存するはずのファイルがコマンドラインに書き出されるという事故が多発したからです。でもcURLは連番([001-009].jpgと書くと001.jpg 002.jpg … 009.jpgを一括で指定できる)に対応しているので、そういう画像とかをダウンロードするときに便利。

使ってみる

閑話休題。wxPython チュートリアルを参考にプログラムを書いてみました。

#! usr/bin/env python
# wxp.py

import wx

app = wx.App()

frame = wx.Frame(None, -1, 'wxp.py')
frame.show(True)

app.MainLoop()

で、動かしてみる。

$ python wxp.py
ImportError: /usr/local/lib/wxPython-unicode-2.8.12.0/lib/python2.6/site-packages/wx-2.8-mac-unicode/wx/_core_.so: no appropriate 64-bit architecture (see "man python" for running in 32-bit mode)

おや? と思ったけど、ちゃんとダウンロードページに書いてありました。

These binaries should work on all versions of OSX from 10.3.9 onwards on either PPC or i386 architectures. Since they use the Carbon API they are limited to running in 32-bit mode.

Carbon APIを使っているので32bitモードでしか動きません、と。バージョン2.9以降はCocoaに対応しているようですが、安定版であるバージョン2.8ではCarbonみたいです。よく見たらeasy_installしようとしたときは2.9.1.1になってますね。

が、その開発版もCocoa対応はPython 2.7からっぽい。僕の環境に入っているPythonは2.6なので使えません。これは困った。

MacPortsでPython 2.7をインストール

しかたがないのでPython 2.7をインストールします。普通にやるといろいろ問題が起こりそうなので、MacPorts経由で。楽だし。

$ sudo port install python27

インストールが完了しても使われるPythonは2.6のままですから、これを2.7に切り替えます。

$ python --version
Python 2.6.1
$ sudo port select --set python python27
Selecting 'python27' for 'python' succeeded. 'python27' is now active.
$ python --version
Python 2.7.2

ちなみに、port selectでは--listというオプションをつけると、現在利用可能なものがリストアップされるみたいです。

$ sudo port select --list python
Available versions for python:
	none
	python25-apple
	python26-apple
	python27 (active)

変更前まではnoneactiveでした。

余談ですが、この作業は最初python_selectというMacPortsでインストールできるスクリプトを使う予定でした。が、インストールしても動かなかったので上記のような手段になりました。なんで動かないんだろう……?

再びwxPythonのインストール

次のインストール手順に失敗しました : run postflight script for wxPython2.9-osx-cocoa-py2.7。ソフトウェアの製造元に問い合わせてください。

インストーラを起動するだけだし余裕だぜ、と思ってたらエラー。エラーメッセージでググると、以下のようなコメントが。

The wxPython Mac installer can only be used with the Pythons from Python.org or Apple’s Python.

wxPythonのインストーラはMacPortsで入れたPythonではうごかねーよ(意訳)、とのこと。2.8の話ですがたぶん2.9でも同じでしょう。

じゃあMacPortsからインストールしてやろう、と思ってport search

$ port search wxpython
py-wxpython @2.8.9.2 (python, graphics)
    Python interface to the wxWindows cross platform GUI

py-wxpython26 @2.6.3.3 (python, graphics)
    Python interface to the wxWindows cross platform GUI

py25-wxpython @2.8.9.1 (python, graphics)
    Python interface to the wxWindows cross platform GUI

py26-wxpython @2.8.10.1 (python, graphics)
    Python interface to the wxWindows cross platform GUI

py27-wxpython @2.8.12.0 (python, graphics)
    Python interface to the wxWindows cross platform GUI

Found 5 ports.

2.9はdev版なのでありません……。

Portfileを利用してMacPortsでインストール

心が折れかけましたが、もう少しがんばります。Portfileというのを使ってインストールする方法があるらしいので、それでやってみましょう。

#27955 (py27-wxpython-devel @2.9.1.1 New port to match proposed wxWidgets-devel @2.9.1) – MacPortsでwxPython 2.9.1.1用のPortfileが公開されていたので、それを使います。

$ mkdir -p ~/macports/python/wxPython-2.9.1.1
$ cd ~/macports/python/wxPython-2.9.1.1
$ wget https://trac.macports.org/raw-attachment/ticket/27955/Portfile
$ cd ~/macports
$ portindex

このへん、実はよくわかっていなくて、Emor.in Software Development » MacPortsでPortfileの作成をなぞっているだけです。こっから先も同様。MacPortsの仕組みもそのうちちゃんと勉強したい。

$ cd /opt/local/etc/macports
$ sudo chmod g+w sources.conf

/opt/local/etc/macports/sources.conf を編集するのですが、adminに書き込み権限がないので、それを与えておいてから、テキストエディタで開き、最後の行にfile:///Users/kaz/macportsと書き加えます。もちろんユーザー名は置き換えてください。また、sources.confのアクセス権は作業が終わったら元に戻しておいたほうが無難だと思います。

#  MacPorts system wide sources configuration file
#  $Id: sources.conf 42662 2008-11-28 23:18:50Z raimue@macports.org $

 ...

#  To get the ports tree from the master MacPorts server in California, USA use:
#      rsync://rsync.macports.org/release/ports/
#  To get it from the mirror in Trondheim, Norway use:
#      rsync://trd.no.rsync.macports.org/release/ports/
#  A current list of mirrors is available at http://trac.macports.org/wiki/Mirrors
rsync://rsync.macports.org/release/ports/ [default]
file:///Users/kaz/macports

一応ちゃんとできているかチェック。

$ port file py27-wxpython-devel
/Users/kaz/macports/python/wxPython-2.9.1.1/Portfile

おk。インストールします。

$ sudo port install py27-wxpython-devel @2.9.1.1

必要なものをあっちこっちから拾ってきてビルドするためかなり時間がかかりましたが、無事完了しました!

こんどこそ動くはず

$ cd ~/python/guitest
$ python wxp.py
Traceback (most recent call last):
  File "wxp.py", line 9, in 
    frame.show(True)
AttributeError: 'Frame' object has no attribute 'show'

ん? ……あ、showじゃなくてShowだった。直して再び。

$ python wxp.py

wxp.py のウィンドウ

きたああああああああああああああああああああああああ!!

……休みつつですが、たぶん数時間は作業したので、さすがに疲れました。今日はここまで。

でも実はまだ少し問題があって、EmacsのshellからPythonを起動すると2.6.1が使われるのですが、これはまた今度。Gitのほうもさっさとやらないと忘れてしまうので早めにやろうと思います。疲れた……。