Raspberry PIでKivyが使えないトラブルの対処法

# はじめに

Kivyでアプリケーションを開発しようと思ったら起動できなかったので、その解決方法をメモしておきます。

# Kivyのインストール

以下のコマンドを順次実行。

ソフトウェアのデータ更新

```
sudo apt-get update
```

ライブラリのインストール

```
sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev \
   pkg-config libgl1-mesa-dev libgles2-mesa-dev \
   python-setuptools libgstreamer1.0-dev git-core \
   gstreamer1.0-plugins-{bad,base,good,ugly} \
   gstreamer1.0-{omx,alsa} python-dev libmtdev-dev \
   xclip xsel python-pip
```

Cythonのバージョンを指定してインストール

```
sudo pip install -U Cython==0.28.2
```

Kivyのソースをダウンロードしてインストール

```
sudo pip install git+https://github.com/kivy/kivy.git@master
```

インストールが終わったところで、以下のサンプルスクリプトを作成。

```python:main.py
#-*- coding: utf-8 -*-

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

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

TestApp().run()
```

以下のコマンドで実行

```
python main.py
```

実行すると以下のエラーが...

```
[INFO   ] [Logger      ] Record log in /home/pi/.kivy/logs/kivy_19-02-13_1.txt
[INFO   ] [Kivy        ] v1.11.0.dev0, git-233cdd1, 20190213
[INFO   ] [Python      ] v2.7.13 (default, Sep 26 2018, 18:42:22) 
[GCC 6.3.0 20170516]
[INFO   ] [Factory     ] 184 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_gif (img_sdl2, img_pil, img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: egl_rpi
[INFO   ] [GL          ] Using the "OpenGL ES 2" graphics system
[INFO   ] [GL          ] Backend used <sdl2>
Segmentation fault
```

Kivy用の設定を実行

```
export KIVY_GL_BACKEND=gl
```

再度スクリプトを実行

```
python main.py
```

すると、今度は別のエラーが...

```
[INFO   ] [Logger      ] Record log in /home/pi/.kivy/logs/kivy_19-02-13_2.txt
[INFO   ] [Kivy        ] v1.11.0.dev0, git-233cdd1, 20190213
[INFO   ] [Python      ] v2.7.13 (default, Sep 26 2018, 18:42:22) 
[GCC 6.3.0 20170516]
[INFO   ] [Factory     ] 184 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_gif (img_sdl2, img_pil, img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: egl_rpi
[INFO   ] [GL          ] Using the "OpenGL ES 2" graphics system
[INFO   ] [GL          ] Backend used <gl>
[INFO   ] [GL          ] OpenGL version <OpenGL ES 2.0>
[INFO   ] [GL          ] OpenGL vendor <Broadcom>
[INFO   ] [GL          ] OpenGL renderer <VideoCore IV HW>
[INFO   ] [GL          ] OpenGL parsed version: 2, 0
[INFO   ] [GL          ] Shading version <OpenGL ES GLSL ES 1.00>
[INFO   ] [GL          ] Texture max size <2048>
[INFO   ] [GL          ] Texture max units <8>
[INFO   ] [Window      ] Provider: sdl2(['window_egl_rpi'] ignored)
[CRITICAL] [Window      ] Unable to find any valuable Window provider.
egl_rpi - Exception: Unknown <png> type, no loader found.
  File "/usr/local/lib/python2.7/dist-packages/kivy/core/__init__.py", line 67, in core_select_lib
    cls = cls()
  File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/__init__.py", line 968, in __init__
    self.create_window()
  File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/window_egl_rpi.py", line 39, in create_window
    super(WindowEglRpi, self).create_window()
  File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/__init__.py", line 1216, in create_window
    self.render_context = RenderContext()
  File "kivy/graphics/instructions.pyx", line 783, in kivy.graphics.instructions.RenderContext.__init__
  File "/usr/local/lib/python2.7/dist-packages/kivy/core/image/__init__.py", line 561, in __init__
    self.filename = arg
  File "/usr/local/lib/python2.7/dist-packages/kivy/core/image/__init__.py", line 757, in _set_filename
    mipmap=self._mipmap, nocache=self._nocache)
  File "/usr/local/lib/python2.7/dist-packages/kivy/core/image/__init__.py", line 463, in load
    raise Exception('Unknown <%s> type, no loader found.' % ext)

sdl2 - RuntimeError: No available video device
  File "/usr/local/lib/python2.7/dist-packages/kivy/core/__init__.py", line 67, in core_select_lib
    cls = cls()
  File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/window_sdl2.py", line 140, in __init__
    super(WindowSDL, self).__init__()
  File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/__init__.py", line 968, in __init__
    self.create_window()
  File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/window_sdl2.py", line 271, in create_window
    self.fullscreen, resizable, state)
  File "kivy/core/window/_window_sdl2.pyx", line 103, in kivy.core.window._window_sdl2._WindowSDL2Storage.setup_window
  File "kivy/core/window/_window_sdl2.pyx", line 67, in kivy.core.window._window_sdl2._WindowSDL2Storage.die

x11 - ImportError: No module named window_x11
  File "/usr/local/lib/python2.7/dist-packages/kivy/core/__init__.py", line 59, in core_select_lib
    fromlist=[modulename], level=0)

[CRITICAL] [App         ] Unable to get a Window, abort.
```

# 解決方法

以下のコマンドを実行し、PILをインストール

```
sudo apt install -y python-pil
```

...できた(-_-;)

わかれば一瞬ですが、わからないとずっとかかりきりになって、全然他のことが進まない。

# おまけ

```
export KIVY_GL_BACKEND=gl
```

上記コマンドを再起動の度に実行しなくてはならないが、スクリプトを以下のとおり修正することで、上記コマンドの実行が必要なくなります。

```python:main.py
#-*- coding: utf-8 -*-

import os
os.environ['KIVY_GL_BACKEND'] = 'gl'

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

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

TestApp().run()
```

以上、終了。