mainvisual

みなさんは、Python のGUIライブラリはどのようなものを使っていますか?

PythonのGUIライブラリといえば、

  • wxPython
  • PyQt
  • PyGTK
  • TkInter
  • Pyglet
  • panda3d
  • OpenCV
  • Kivy

といったものがあります。

Pyhonで画像処理関連のソフト作成の際に、GUIを使いたいなーと思っています。 ですので今回、調査ということで、Kivyをローカルにインストールしました。

結構ハマったところがあって、みなさんと共有できればと思い、記事にしました。

※ 2016/6/20 編集:
  • 流れが掴みにくかったので、各項目の順番を変更しました。
  • brew, python, pipのバージョン情報を追加しました。

前提

  • brew, python, pip がインストールされていること

この時使用したbrew, python, pipのバージョンは以下の通りです。

$ brew --version
Homebrew 0.9.9 (git revision b2c96; last commit 2016-06-02)
Homebrew/homebrew-core (git revision e4f3; last commit 2016-06-02)

$ python --version
Python 2.7.11

$ python pip --version
pip 8.1.2 from /usr/local/lib/python2.7/site-packages (python 2.7)

インストール

基本的には、以下のコマンドでインストールが解決します。 何かエラーが起きて、インストールできない時は、以下の「トラブルシューティング」の項目を見ると、 解決できるかもしれません。

$ pip install kivy

kivyを実行を確認するためのpythonスクリプト

自分は、kivy_main.pyで保存しました

import kivy
kivy.require('1.9.0')

from kivy.app import App
from kivy.uix.label import Label

class HelloApp(App):

    def build(self):
        return Label(text='Hello World')

if __name__ == '__main__':
    HelloApp().run()

実行します。

$ python kivy_main.py

このような画面が出ればインストール終了です。

kivy Hello World

補足:Kivyチュートリアル

Kivyチュートリアルは、
http://qiita.com/Ds110/items/2988646cb6622648b463
がとてもわかり易いです。

トラブルシューティング

kivyインストール時にエラー

$ pip install kivy
Collecting kivy
  Downloading kivy-1.9.1.tar.gz (16.4MB)
    100% |████████████████████████████████| 16.4MB 71kB/s
    Complete output from command python setup.py egg_info:
    Using distutils

    Cython is missing, its required for compiling kivy !


    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/q3/4_lj9g8n6cgcwxh7m5ndp7pr0000gn/T/pip-build-DOiHCN/kivy/setup.py", line 184, in <module>
        from Cython.Distutils import build_ext
    ImportError: No module named Cython.Distutils

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/q3/4_lj9g8n6cgcwxh7m5ndp7pr0000gn/T/pip-build-DOiHCN/kivy/

原因

Cythonなど、pipの依存パッケージば不足している

解決法

from http://montblanc6615.blog90.fc2.com/blog-entry-113.html

$ pip install Cython numpy scipy matplotlib pandas

成功時の出力

Collecting Cython
  Downloading Cython-0.24-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.8MB)
    100% |████████████████████████████████| 3.9MB 287kB/s
Requirement already satisfied (use --upgrade to upgrade): numpy in /usr/local/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): scipy in /usr/local/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): matplotlib in /usr/local/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): pandas in /usr/local/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): pytz in /usr/local/lib/python2.7/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /usr/local/lib/python2.7/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): cycler in /usr/local/lib/python2.7/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): pyparsing!=2.0.0,!=2.0.4,>=1.5.6 in /usr/local/lib/python2.7/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in /usr/local/lib/python2.7/site-packages (from python-dateutil->matplotlib)
Installing collected packages: Cython
Successfully installed Cython-0.24

kivy実行時にエラー

$ python kivy_main.py
[WARNING] [Config      ] Older configuration version detected (0 instead of 14)
[WARNING] [Config      ] Upgrading configuration in progress.
[INFO   ] [Logger      ] Record log in /Users/k4zzk/.kivy/logs/kivy_16-05-08_0.txt
[INFO   ] [Kivy        ] v1.9.1
[INFO   ] [Python      ] v2.7.11 (default, Jan 22 2016, 08:28:37)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)]
[INFO   ] [Factory     ] 179 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_imageio, img_dds, img_gif (img_pygame, img_pil, img_ffpyplayer ignored)
[CRITICAL] [Text        ] Unable to find any valuable Text provider at all!
pygame - ImportError: No module named pygame
  File "/usr/local/lib/python2.7/site-packages/kivy/core/__init__.py", line 59, in core_select_lib
    fromlist=[modulename], level=0)
  File "/usr/local/lib/python2.7/site-packages/kivy/core/text/text_pygame.py", line 12, in <module>
    import pygame

pil - ImportError: No module named PIL
  File "/usr/local/lib/python2.7/site-packages/kivy/core/__init__.py", line 59, in core_select_lib
    fromlist=[modulename], level=0)
  File "/usr/local/lib/python2.7/site-packages/kivy/core/text/text_pil.py", line 8, in <module>
    from PIL import Image, ImageFont, ImageDraw

[CRITICAL] [App         ] Unable to get a Text provider, abort.

原因

pygameがインストールされていない

解決法

from http://blog.elliptium.net/2012/01/Mac-PyGame

$ brew install sdl sdl_image sdl_mixer sdl_ttf portmidi
$ brew install --HEAD smpeg
$ pip install hg+http://bitbucket.org/pygame/pygame

成功時の出力

$ python kivy_main.py
[INFO   ] [Logger      ] Record log in /Users/k4zzk/.kivy/logs/kivy_16-05-08_4.txt
[INFO   ] [Kivy        ] v1.9.1
[INFO   ] [Python      ] v2.7.11 (default, Jan 22 2016, 08:28:37)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)]
[INFO   ] [Factory     ] 179 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_imageio, img_dds, img_gif, img_pygame (img_pil, img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: pygame
[INFO   ] [Window      ] Provider: pygame
[INFO   ] [GL          ] OpenGL version <2.1 INTEL-10.6.33>
[INFO   ] [GL          ] OpenGL vendor <Intel>
[INFO   ] [GL          ] OpenGL renderer <Intel(R) Iris(TM) Graphics 6100>
[INFO   ] [GL          ] OpenGL parsed version: 2, 1
[INFO   ] [GL          ] Shading version <1.20>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <16>
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [OSC         ] using <multiprocessing> for socket
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
[INFO   ] [Base        ] Leaving application in progress...

自分のmacにkivyを実行するまでの道のり(エラーを含む)

googleで検索をかけた時にヒットするように、全過程を掲載します。

pip install kivyでインストール

$ pip install kivy
Collecting kivy
  Downloading kivy-1.9.1.tar.gz (16.4MB)
    100% |████████████████████████████████| 16.4MB 71kB/s
    Complete output from command python setup.py egg_info:
    Using distutils

    Cython is missing, its required for compiling kivy !


    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/q3/4_lj9g8n6cgcwxh7m5ndp7pr0000gn/T/pip-build-DOiHCN/kivy/setup.py", line 184, in <module>
        from Cython.Distutils import build_ext
    ImportError: No module named Cython.Distutils

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/q3/4_lj9g8n6cgcwxh7m5ndp7pr0000gn/T/pip-build-DOiHCN/kivy/
$ pip install Cython numpy scipy matplotlib pandas
Collecting Cython
  Downloading Cython-0.24-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.8MB)
    100% |████████████████████████████████| 3.9MB 287kB/s
Requirement already satisfied (use --upgrade to upgrade): numpy in /usr/local/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): scipy in /usr/local/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): matplotlib in /usr/local/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): pandas in /usr/local/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): pytz in /usr/local/lib/python2.7/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /usr/local/lib/python2.7/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): cycler in /usr/local/lib/python2.7/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): pyparsing!=2.0.0,!=2.0.4,>=1.5.6 in /usr/local/lib/python2.7/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in /usr/local/lib/python2.7/site-packages (from python-dateutil->matplotlib)
Installing collected packages: Cython
Successfully installed Cython-0.24
$ pip install kivy
Collecting kivy
  Using cached kivy-1.9.1.tar.gz
Collecting Kivy-Garden>=0.1.4 (from kivy)
  Downloading kivy-garden-0.1.4.tar.gz
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/local/lib/python2.7/site-packages (from Kivy-Garden>=0.1.4->kivy)
Building wheels for collected packages: kivy, Kivy-Garden
  Running setup.py bdist_wheel for kivy ... done
  Stored in directory: /Users/k4zzk/Library/Caches/pip/wheels/45/ef/1e/12e29951308821d5c80cbd14b3aef28f76a0380309885a4adf
  Running setup.py bdist_wheel for Kivy-Garden ... done
  Stored in directory: /Users/k4zzk/Library/Caches/pip/wheels/27/08/88/88938a7cf5b20073ff1f0432b7c0dd172531185cc74d97f5da
Successfully built kivy Kivy-Garden
Installing collected packages: Kivy-Garden, kivy
Successfully installed Kivy-Garden-0.1.4 kivy-1.9.1

python kivy.py を実行して、循環参照になって、エラーになる

$ python3 kivy.py
Traceback (most recent call last):
  File "kivy.py", line 1, in <module>
    import kivy
  File "/Users/k4zzk/Desktop/kivy.py", line 2, in <module>
    kivy.require('1.9.0')
AttributeError: module 'kivy' has no attribute 'require'
$ python                              1 ↵
Python 2.7.11 (default, Jan 22 2016, 08:28:37)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import kivy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "kivy.py", line 2, in <module>
    kivy.require('1.9.0')

ファイル名を修正

$ rm kivy.pyc
$ mv kivy.py kivy_main.py

pygameが無くて、怒られた

$ python kivy_main.py
[WARNING] [Config      ] Older configuration version detected (0 instead of 14)
[WARNING] [Config      ] Upgrading configuration in progress.
[INFO   ] [Logger      ] Record log in /Users/k4zzk/.kivy/logs/kivy_16-05-08_0.txt
[INFO   ] [Kivy        ] v1.9.1
[INFO   ] [Python      ] v2.7.11 (default, Jan 22 2016, 08:28:37)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)]
[INFO   ] [Factory     ] 179 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_imageio, img_dds, img_gif (img_pygame, img_pil, img_ffpyplayer ignored)
[CRITICAL] [Text        ] Unable to find any valuable Text provider at all!
pygame - ImportError: No module named pygame
  File "/usr/local/lib/python2.7/site-packages/kivy/core/__init__.py", line 59, in core_select_lib
    fromlist=[modulename], level=0)
  File "/usr/local/lib/python2.7/site-packages/kivy/core/text/text_pygame.py", line 12, in <module>
    import pygame

pil - ImportError: No module named PIL
  File "/usr/local/lib/python2.7/site-packages/kivy/core/__init__.py", line 59, in core_select_lib
    fromlist=[modulename], level=0)
  File "/usr/local/lib/python2.7/site-packages/kivy/core/text/text_pil.py", line 8, in <module>
    from PIL import Image, ImageFont, ImageDraw

[CRITICAL] [App         ] Unable to get a Text provider, abort.

pygameをインストールするための依存ライブラリをごそっとインストール

$ brew install sdl sdl_image sdl_mixer sdl_ttf portmidi
==> Downloading https://homebrew.bintray.com/bottles/sdl-1.2.15.yosemite.bottle.
######################################################################## 100.0%
==> Pouring sdl-1.2.15.yosemite.bottle.2.tar.gz
🍺  /usr/local/Cellar/sdl/1.2.15: 224 files, 1.5M
==> Installing dependencies for sdl_image: webp
==> Installing sdl_image dependency: webp
==> Downloading https://homebrew.bintray.com/bottles/webp-0.5.0.yosemite.bottle.
######################################################################## 100.0%
==> Pouring webp-0.5.0.yosemite.bottle.tar.gz
🍺  /usr/local/Cellar/webp/0.5.0: 36 files, 1.8M
==> Installing sdl_image
==> Downloading https://homebrew.bintray.com/bottles/sdl_image-1.2.12_2.yosemite
######################################################################## 100.0%
==> Pouring sdl_image-1.2.12_2.yosemite.bottle.tar.gz
🍺  /usr/local/Cellar/sdl_image/1.2.12_2: 9 files, 111.2K
==> Downloading https://homebrew.bintray.com/bottles/sdl_mixer-1.2.12.yosemite.b
######################################################################## 100.0%
==> Pouring sdl_mixer-1.2.12.yosemite.bottle.1.tar.gz
🍺  /usr/local/Cellar/sdl_mixer/1.2.12: 9 files, 390.3K
==> Downloading https://homebrew.bintray.com/bottles/sdl_ttf-2.0.11.yosemite.bot
######################################################################## 100.0%
==> Pouring sdl_ttf-2.0.11.yosemite.bottle.2.tar.gz
🍺  /usr/local/Cellar/sdl_ttf/2.0.11: 9 files, 70.3K
==> Downloading https://homebrew.bintray.com/bottles/portmidi-217.yosemite.bottl
######################################################################## 100.0%
==> Pouring portmidi-217.yosemite.bottle.1.tar.gz
🍺  /usr/local/Cellar/portmidi/217: 7 files, 88.4K
$ brew install --HEAD smpeg
Error: No head is defined for smpeg

pygame インストール

$ pip install hg+http://bitbucket.org/pygame/pygame
Collecting hg+http://bitbucket.org/pygame/pygame
  Cloning hg http://bitbucket.org/pygame/pygame to /var/folders/q3/4_lj9g8n6cgcwxh7m5ndp7pr0000gn/T/pip-UqYBiZ-build
Installing collected packages: pygame
  Running setup.py install for pygame ... done
Successfully installed pygame-1.9.2.dev1

kivy実行できた

$ python kivy_main.py
[INFO   ] [Logger      ] Record log in /Users/k4zzk/.kivy/logs/kivy_16-05-08_4.txt
[INFO   ] [Kivy        ] v1.9.1
[INFO   ] [Python      ] v2.7.11 (default, Jan 22 2016, 08:28:37)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)]
[INFO   ] [Factory     ] 179 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_imageio, img_dds, img_gif, img_pygame (img_pil, img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: pygame
[INFO   ] [Window      ] Provider: pygame
[INFO   ] [GL          ] OpenGL version <2.1 INTEL-10.6.33>
[INFO   ] [GL          ] OpenGL vendor <Intel>
[INFO   ] [GL          ] OpenGL renderer <Intel(R) Iris(TM) Graphics 6100>
[INFO   ] [GL          ] OpenGL parsed version: 2, 1
[INFO   ] [GL          ] Shading version <1.20>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <16>
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [OSC         ] using <multiprocessing> for socket
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
[INFO   ] [Base        ] Leaving application in progress...