Help us understand the problem. What is going on with this article?

macOSにpyaudioをインストールする

More than 3 years have passed since last update.

イントロダクション

PyAudioはクロスプラットフォームのオーディオI/Oライブラリ「PortAudio」に対するPython用のバインディングです。Google Cloud Speech APIをはじめ、Pythonでサンプルが提供されている音声認識ライブラリなどを実行する際には必須のライブラリですが、HomebrewでPortAudioをインストールした環境において、PyAudioのインストール中に失敗することがあります。以下、その問題に対する解決法を紹介します。

問題

まだPortAudioをまだインストールしていない場合には以下のようにしてインストールします。

$ brew install portaudio
Warning: You are using OS X 10.12.
We do not provide support for this pre-release version.
You may encounter build failures or other breakages.
==> Downloading http://www.portaudio.com/archives/pa_stable_v19_20140130.tgz
Already downloaded: /Library/Caches/Homebrew/portaudio-19.20140130.tgz
==> ./configure --prefix=/usr/local/Cellar/portaudio/19.20140130 --enable-mac-un
==> make install
🍺  /usr/local/Cellar/portaudio/19.20140130: 32 files, 449.3K, built in 54 seconds

次に、pyaudioをインストールします。しかしながら、この時portaudio.hがみつからないとうエラーでインストールに失敗することがあります。

$ pip install pyaudio
Collecting pyaudio
  Using cached PyAudio-0.2.10.tar.gz
Building wheels for collected packages: pyaudio
  Running setup.py bdist_wheel for pyaudio ... error
  Complete output from command /Users/mayfair/Documents/development/python-docs-samples/speech/grpc/env/bin/python3.5 -u -c "import setuptools, tokenize;__file__='/private/var/folders/1n/gqy2kcyn1j95_cb2vjxr3l2w0000gn/T/pip-build-iwktd6rr/pyaudio/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /var/folders/1n/gqy2kcyn1j95_cb2vjxr3l2w0000gn/T/tmpq4e0nbhhpip-wheel- --python-tag cp35:
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-10.11-x86_64-3.5
  copying src/pyaudio.py -> build/lib.macosx-10.11-x86_64-3.5
  running build_ext
  building '_portaudio' extension
  creating build/temp.macosx-10.11-x86_64-3.5
  creating build/temp.macosx-10.11-x86_64-3.5/src
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DMACOSX=1 -I/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/include/python3.5m -c src/_portaudiomodule.c -o build/temp.macosx-10.11-x86_64-3.5/src/_portaudiomodule.o
  src/_portaudiomodule.c:29:10: fatal error: 'portaudio.h' file not found
  #include "portaudio.h"
           ^
  1 error generated.
  error: command 'clang' failed with exit status 1

  ----------------------------------------
  Failed building wheel for pyaudio
(以下略)

解決法

以下のようにしてbrewでインストールされたライブラリ及びインクルードファイルの位置を指定した上でpip install pyaudioを実行します。これにより、無事にpyaudioがインストールできるようになります。

$ sudo env LDFLAGS="-L/usr/local/lib" CFLAGS="-I/usr/local/include" pip install pyaudio
Password:
The directory '/Users/mayfair/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/mayfair/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pyaudio
  Downloading PyAudio-0.2.10.tar.gz (287kB)
    100% |████████████████████████████████| 296kB 2.0MB/s 
Installing collected packages: pyaudio
  Running setup.py install for pyaudio ... done
Successfully installed pyaudio-0.2.10
mayfair
博士(メディアデザイン学・慶應義塾大学大学院メディアデザイン研究科)。1993年より電子楽器メーカーに勤務した後2004年よりIAMAS。多様なスキル、視点、経験を持つ人々が共にイノベーションを創出するための手法や、その過程で生まれる知的財産を扱うのに適切なルールを探求。著書に『Prototyping Lab』『アイデアスケッチ』など。Ogaki Mini Maker Faire総合ディレクター。
https://note.mu/_kotobuki_
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした