PyAudio 安装总是在 Mac 上失败

PyAudio installation always fails on Mac

首先,我尝试了

pip install pyaudio

但这没有用...我发现我需要 PortAudio,所以用 brew:

安装了它
brew install portaudio

成功了,然后我重新启动了 Mac。

但是 pip install pyaudio 仍然不起作用,我收到以下错误消息:

ERROR: Command errored out with exit status 1: command: /Applications/Xcode.app/Contents/Developer/usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/12/44t6pktj77xbqmyp7x368tdr0000gn/T/pip-install-gvxfbjmp/pyaudio/setup.py'"'"'; file='"'"'/private/var/folders/12/44t6pktj77xbqmyp7x368tdr0000gn/T/pip-install-gvxfbjmp/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 /private/var/folders/12/44t6pktj77xbqmyp7x368tdr0000gn/T/pip-wheel-gvmpobvy cwd: /private/var/folders/12/44t6pktj77xbqmyp7x368tdr0000gn/T/pip-install-gvxfbjmp/pyaudio/ Complete output (16 lines): running bdist_wheel running build running build_py creating build creating build/lib.macosx-10.14-x86_64-3.7 copying src/pyaudio.py -> build/lib.macosx-10.14-x86_64-3.7 running build_ext building '_portaudio' extension creating build/temp.macosx-10.14-x86_64-3.7 creating build/temp.macosx-10.14-x86_64-3.7/src xcrun -sdk macosx clang -arch x86_64 -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -iwithsysroot/System/Library/Frameworks/System.framework/PrivateHeaders -iwithsysroot/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/Headers -DMACOSX=1 -I/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/include/python3.7m -c src/_portaudiomodule.c -o build/temp.macosx-10.14-x86_64-3.7/src/_portaudiomodule.o src/_portaudiomodule.c:29:10: fatal error: 'portaudio.h' file not found #include "portaudio.h" ^~~~~~~~~~~~~ 1 error generated. error: command 'xcrun' failed with exit status 1 ---------------------------------------- ERROR: Failed building wheel for pyaudio

我也试过pip3 install pyaudio,但没有成功。

顺便说一句,我这样做是因为我想使用 Python 语音识别 API,我认为 pyaudio 是必要的...是吗?

你的问题是 Xcode 命令行。使用此重新安装

xcode-select --install

在 xcode 修复后,命令 ('xcrun') 应该能够 运行 所以只需重用

pip3 install pyaudio

您很可能需要指定编译器可以找到源程序的目录路径,例如 portaudio.h

假设 headers 在 /usr/local/include 目录中,尝试 运行 在终端中执行以下命令:

pip install --global-option='build_ext' --global-option='-I/usr/local/include' --global-option='-L/usr/local/lib' pyaudio

您可能需要升级 pip python3 -m pip install --upgrade pip

-- 成功安装pip-21.2.4

and/or 调用 pip3 pip3 install ...