smellman's Broken Diary

クソみたいなもんです

MacPorts + Python + Virtualenv + RunSnake (というかwxPython)

gdal2tilesがクソ時間かかるのでプロファイルをしていました。

最初はline_profilerを使ってみたんだけど、 @profile とか書くと普通に動かした時に動かなくなって面倒なので、素直にcProfileを使いました。

% python -m cProfile -o ./profiler_result/1.prof gdal2tiles.py -v -z 0 ~/sampledata/all/temp.vrt ~/sampledata/all/tmp5

これで1.profっていう名前で結果が吐出されるんですが、やっぱりグラフィカルに見たいなーと思って探してみたら RunSnakeRun が良さそう。

とりあえずセットアップします。

% sudo port install py27-wxpython-3.0
% virtualenv-2.7 snake --system-site-packages
% source snake/bin/activate
% pip install SquareMap RunSnakeRun
% rehash

そして動かしてみます。

% runsnake ./profiler_result/1.prof
This program needs access to the screen.
Please run with a Framework build of python, and only when you are
logged in on the main display of your Mac.

怒られました。
これはどうやらpythonwが無いくさいという感じです。

% which python
/Users/btm/(内緒)/dev/snake/bin/python
% which pythonw
/opt/local/bin/pythonw

調べてみたらStack Overflow で osx - Why are Python builds suddenly not Framework builds when using virtualenv? - Stack Overflow という質問があり回答が書いてありました。

% wget --no-check-certificate https://raw.github.com/gldnspud/virtualenv-pythonw-osx/master/install_pythonw.py
% wget --no-check-certificate https://raw.github.com/gldnspud/virtualenv-pythonw-osx/master/pythonw.c
% python install_pythonw.py `which python`/../..
finished!  App bundle created at:  /Users/btm/(内緒)/dev/snake/Python.app

あとは実行してみます。

% runsnake ./profiler_result/1.prof

f:id:smellman:20140206135807p:plain

で、このテクニックなんですが、上記のFixを行う前だと、

>>> import wx
>>> wx.App()
This program needs access to the screen.
Please run with a Framework build of python, and only when you are
logged in on the main display of your Mac.

こうなってしまうので、wxPythonではまってる人も同じ問題で解決できるのではないかと思います。