pip install すると「error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1」エラー

<h2>fatal error: Python.h: そのようなファイルやディレクトリはありません</h2>

`$ pip install` や `$ python setup.py install` すると、`arm-linux-gnueabihf-gcc` コマンドのエラーが発生する。

```shell-session:エラー概要（en）
$ sudo pip install amazon-dash
（略）
_posixsubprocess.c:16:20: fatal error: Python.h: No such file or directory
#include "Python.h"
                   ^
compilation terminated.
error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
（略）
```

## TL;DR

> **`python-dev` もインストールする**必要があります。

これは ARM アーキテクチャ用のパッケージ（Python エクステンション）が見つからない場合に、ソースから [`gcc`](https://ja.wikipedia.org/wiki/GNU%E3%82%B3%E3%83%B3%E3%83%91%E3%82%A4%E3%83%A9%E3%82%B3%E3%83%AC%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3) でビルドするために必要なヘッダファイル（`Python.h`）が不足しているためです。このファイルは `python-dev` に含まれています。

```bash
sudo apt install python-dev
```

## TS;DR（作業ログの詳細）

[Amazon Dash Button のハック](https://www.google.co.jp/search?q=site%3Aqiita.com+Dasher+%22Amazon+Dash+Button%22)で有名な Node.js の「[Dasher](https://github.com/maddox/dasher/)」が [2018/07/27 付けで開発を終了](https://github.com/maddox/dasher/commit/e5e7a92c48c873444ecbf80c1c3c5a8b59b09974)したので、代わりにオススメされている Python の「[Nekmo/amazon-dash](https://github.com/Nekmo/amazon-dash)」を試そうとしたら表題のエラーがでました。

```shell-session:エラー概要（ja）
$ sudo pip install amazon-dash
（略）
_posixsubprocess.c:16:20: fatal error: Python.h: そのようなファイルやディレクトリはありません
#include "Python.h"
                   ^
compilation terminated.
error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
(略)
```

<details><summary>NG ログの詳細</summary><div>

```shell-session:bash
$ sudo pip install amazon-dash
Downloading/unpacking amazon-dash
  Downloading amazon-dash-1.3.1.tar.gz
  Running setup.py (path:/tmp/pip-build-3up3dZ/amazon-dash/setup.py) egg_info for package amazon-dash
Downloading/unpacking PyYAML>=3.0 (from amazon-dash)
  Downloading PyYAML-3.13.tar.gz (270kB): 270kB downloaded
  Running setup.py (path:/tmp/pip-build-3up3dZ/PyYAML/setup.py) egg_info for package PyYAML
Downloading/unpacking jsonschema (from amazon-dash)
  Downloading jsonschema-2.6.0-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/lib/python2.7/dist-packages (from amazon-dash)
Downloading/unpacking click (from amazon-dash)
  Downloading Click-7.0-py2.py3-none-any.whl (81kB): 81kB downloaded
Downloading/unpacking click-default-group (from amazon-dash)
  Downloading click-default-group-1.2.tar.gz
  Running setup.py (path:/tmp/pip-build-3up3dZ/click-default-group/setup.py) egg_info for package click-default-group
Downloading/unpacking scapy (from amazon-dash)
  Downloading scapy-2.4.0.tar.gz (3.1MB): 3.1MB downloaded
  Running setup.py (path:/tmp/pip-build-3up3dZ/scapy/setup.py) egg_info for package scapy
Downloading/unpacking subprocess32 (from amazon-dash)
  Downloading subprocess32-3.5.3.tar.gz (96kB): 96kB downloaded
  Running setup.py (path:/tmp/pip-build-3up3dZ/subprocess32/setup.py) egg_info for package subprocess32
    /usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires'
      warnings.warn(msg)
    no previously-included directories found matching 'build'
    no previously-included directories found matching 'dist'
    no previously-included directories found matching 'config.*'
    no previously-included directories found matching '*.pyc'
Downloading/unpacking functools32 (from jsonschema->amazon-dash)
  Downloading functools32-3.2.3-2.tar.gz
  Running setup.py (path:/tmp/pip-build-3up3dZ/functools32/setup.py) egg_info for package functools32
    warning: no files found matching '*.txt'
    no previously-included directories found matching 'build'
    no previously-included directories found matching 'dist'
    no previously-included directories found matching '.git*'
Installing collected packages: amazon-dash, PyYAML, jsonschema, click, click-default-group, scapy, subprocess32, functools32
  Running setup.py install for amazon-dash
    changing mode of build/scripts-2.7/amazon-dash from 644 to 755
    changing mode of /usr/local/bin/amazon-dash to 755
  Running setup.py install for PyYAML
    checking if libyaml is compilable
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c build/temp.linux-armv7l-2.7/check_libyaml.c -o build/temp.linux-armv7l-2.7/check_libyaml.o
    build/temp.linux-armv7l-2.7/check_libyaml.c:2:18: fatal error: yaml.h: そのようなファイルやディレクトリはありません
     #include <yaml.h>
                      ^
    compilation terminated.
    libyaml is not found or a compiler error: forcing --without-libyaml
    (if libyaml is installed correctly, you may need to
     specify the option --include-dirs or uncomment and
     modify the parameter include_dirs in setup.cfg)
  Running setup.py install for click-default-group
  Running setup.py install for scapy
    changing mode of build/scripts-2.7/scapy from 644 to 755
    changing mode of build/scripts-2.7/UTscapy from 644 to 755
    changing mode of /usr/local/bin/UTscapy to 755
    changing mode of /usr/local/bin/scapy to 755
  Running setup.py install for subprocess32
    /usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires'
      warnings.warn(msg)
    checking for gcc... gcc
    checking whether the C compiler works... yes
    checking for C compiler default output file name... a.out
    checking for suffix of executables...
    checking whether we are cross compiling... no
    checking for suffix of object files... o
    checking whether we are using the GNU C compiler... yes
    checking whether gcc accepts -g... yes
    checking for gcc option to accept ISO C89... none needed
    checking how to run the C preprocessor... gcc -E
    checking for grep that handles long lines and -e... /bin/grep
    checking for egrep... /bin/grep -E
    checking for ANSI C header files... yes
    checking for sys/types.h... yes
    checking for sys/stat.h... yes
    checking for stdlib.h... yes
    checking for string.h... yes
    checking for memory.h... yes
    checking for strings.h... yes
    checking for inttypes.h... yes
    checking for stdint.h... yes
    checking for unistd.h... yes
    checking for unistd.h... (cached) yes
    checking fcntl.h usability... yes
    checking fcntl.h presence... yes
    checking for fcntl.h... yes
    checking signal.h usability... yes
    checking signal.h presence... yes
    checking for signal.h... yes
    checking sys/cdefs.h usability... yes
    checking sys/cdefs.h presence... yes
    checking for sys/cdefs.h... yes
    checking for sys/types.h... (cached) yes
    checking for sys/stat.h... (cached) yes
    checking sys/syscall.h usability... yes
    checking sys/syscall.h presence... yes
    checking for sys/syscall.h... yes
    checking for dirent.h that defines DIR... yes
    checking for library containing opendir... none required
    checking for pipe2... yes
    checking for setsid... yes
    checking whether dirfd is declared... yes
    configure: creating ./config.status
    config.status: creating _posixsubprocess_config.h
    building '_posixsubprocess32' extension
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c _posixsubprocess.c -o build/temp.linux-armv7l-2.7/_posixsubprocess.o
    _posixsubprocess.c:16:20: fatal error: Python.h: そのようなファイルやディレクトリはありません
     #include "Python.h"
                        ^
    compilation terminated.
    error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
    Complete output from command /usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip-build-3up3dZ/subprocess32/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-ALUjcF-record/install-record.txt --single-version-externally-managed --compile:
    /usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires'
  warnings.warn(msg)
running install
running build
running build_py
creating build
creating build/lib.linux-armv7l-2.7
copying subprocess32.py -> build/lib.linux-armv7l-2.7
running build_ext
running build_configure
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for unistd.h... (cached) yes
checking fcntl.h usability... yes
checking fcntl.h presence... yes
checking for fcntl.h... yes
checking signal.h usability... yes
checking signal.h presence... yes
checking for signal.h... yes
checking sys/cdefs.h usability... yes
checking sys/cdefs.h presence... yes
checking for sys/cdefs.h... yes
checking for sys/types.h... (cached) yes
checking for sys/stat.h... (cached) yes
checking sys/syscall.h usability... yes
checking sys/syscall.h presence... yes
checking for sys/syscall.h... yes
checking for dirent.h that defines DIR... yes
checking for library containing opendir... none required
checking for pipe2... yes
checking for setsid... yes
checking whether dirfd is declared... yes
configure: creating ./config.status
config.status: creating _posixsubprocess_config.h
building '_posixsubprocess32' extension
creating build/temp.linux-armv7l-2.7
arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c _posixsubprocess.c -o build/temp.linux-armv7l-2.7/_posixsubprocess.o
_posixsubprocess.c:16:20: fatal error: Python.h: そのようなファイルやディレクトリはありません
 #include "Python.h"
                    ^
compilation terminated.
error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
----------------------------------------
Cleaning up...
Command /usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip-build-3up3dZ/subprocess32/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-ALUjcF-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip-build-3up3dZ/subprocess32
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    load_entry_point('pip==1.5.6', 'console_scripts', 'pip')()
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 248, in main
    return command.main(cmd_args)
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 161, in main
    text = '\n'.join(complete_log)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 75: ordinal not in range(128)
```
</div></details>

<details><summary>OK ログの詳細</summary><div>

```shell-session:bash
$ sudo apt install python-dev
（略）
$ 
$ sudo pip install amazon-dash
Requirement already satisfied (use --upgrade to upgrade): amazon-dash in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied (use --upgrade to upgrade): PyYAML>=3.0 in /usr/local/lib/python2.7/dist-packages (from amazon-dash)
Requirement already satisfied (use --upgrade to upgrade): jsonschema in /usr/local/lib/python2.7/dist-packages (from amazon-dash)
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/lib/python2.7/dist-packages (from amazon-dash)
Requirement already satisfied (use --upgrade to upgrade): click in /usr/local/lib/python2.7/dist-packages (from amazon-dash)
Requirement already satisfied (use --upgrade to upgrade): click-default-group in /usr/local/lib/python2.7/dist-packages (from amazon-dash)
Requirement already satisfied (use --upgrade to upgrade): scapy in /usr/local/lib/python2.7/dist-packages (from amazon-dash)
Downloading/unpacking subprocess32 (from amazon-dash)
  Downloading subprocess32-3.5.3.tar.gz (96kB): 96kB downloaded
  Running setup.py (path:/tmp/pip-build-7VXPxl/subprocess32/setup.py) egg_info for package subprocess32
    /usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires'
      warnings.warn(msg)
    
    no previously-included directories found matching 'build'
    no previously-included directories found matching 'dist'
    no previously-included directories found matching 'config.*'
    no previously-included directories found matching '*.pyc'
Installing collected packages: subprocess32
  Running setup.py install for subprocess32
    /usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires'
      warnings.warn(msg)
    checking for gcc... gcc
    checking whether the C compiler works... yes
    checking for C compiler default output file name... a.out
    checking for suffix of executables...
    checking whether we are cross compiling... no
    checking for suffix of object files... o
    checking whether we are using the GNU C compiler... yes
    checking whether gcc accepts -g... yes
    checking for gcc option to accept ISO C89... none needed
    checking how to run the C preprocessor... gcc -E
    checking for grep that handles long lines and -e... /bin/grep
    checking for egrep... /bin/grep -E
    checking for ANSI C header files... yes
    checking for sys/types.h... yes
    checking for sys/stat.h... yes
    checking for stdlib.h... yes
    checking for string.h... yes
    checking for memory.h... yes
    checking for strings.h... yes
    checking for inttypes.h... yes
    checking for stdint.h... yes
    checking for unistd.h... yes
    checking for unistd.h... (cached) yes
    checking fcntl.h usability... yes
    checking fcntl.h presence... yes
    checking for fcntl.h... yes
    checking signal.h usability... yes
    checking signal.h presence... yes
    checking for signal.h... yes
    checking sys/cdefs.h usability... yes
    checking sys/cdefs.h presence... yes
    checking for sys/cdefs.h... yes
    checking for sys/types.h... (cached) yes
    checking for sys/stat.h... (cached) yes
    checking sys/syscall.h usability... yes
    checking sys/syscall.h presence... yes
    checking for sys/syscall.h... yes
    checking for dirent.h that defines DIR... yes
    checking for library containing opendir... none required
    checking for pipe2... yes
    checking for setsid... yes
    checking whether dirfd is declared... yes
    configure: creating ./config.status
    config.status: creating _posixsubprocess_config.h
    building '_posixsubprocess32' extension
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c _posixsubprocess.c -o build/temp.linux-armv7l-2.7/_posixsubprocess.o
    arm-linux-gnueabihf-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-z,relro -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-armv7l-2.7/_posixsubprocess.o -o build/lib.linux-armv7l-2.7/_posixsubprocess32.so
    
    no previously-included directories found matching 'build'
    no previously-included directories found matching 'dist'
    no previously-included directories found matching 'config.*'
    no previously-included directories found matching '*.pyc'
Successfully installed subprocess32
Cleaning up...
```

</div></details>

```shell-session:python-devがインストール済みか確認する方法
$ dpkg -l | grep python-dev
ii  libpython-dev:armhf              2.7.9-1                                         armhf        header files and a static library for Python (default)
ii  python-dev                       2.7.9-1                                         armhf        header files and a static library for Python (default)
```

## 検証環境

```shell-session:環境情報
$ cat /etc/os-release | head -1
PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
$ 
$ pip --version
pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7)
$ 
$ arm-linux-gnueabihf-gcc --version | head -1
arm-linux-gnueabihf-gcc (Raspbian 4.9.2-10+deb8u1) 4.9.2
```

## 参考文献

- 「[No module named subprocess32](https://github.com/Nekmo/amazon-dash/issues/72#issuecomment-411901963) | Issue #72 comment」@ Nekmo/amazon-dash @ GitHub
- 「[What is python-dev package used for](https://stackoverflow.com/questions/31002091/what-is-python-dev-package-used-for)」@ StackOverflow
- 「[CompileError: command 'arm-linux-gnueabihf-gcc' failed with exit status 4](https://github.com/lhelontra/tensorflow-on-arm/issues/14) | Issue #14 」@ lhelontra/tensorflow-on-arm @ GitHub
- 「[Python.h が見つからない場合](https://qiita.com/dz_/items/4e858e4d50db19b57491#pythonh-%E3%81%8C%E8%A6%8B%E3%81%A4%E3%81%8B%E3%82%89%E3%81%AA%E3%81%84%E5%A0%B4%E5%90%88) | Check! Raspberry Pi で Bluetooth 経由のセンサーデータを取得する 〜 準備編」 @ Qiita