M1Pro MacBook Pro安装PyAudio失败求助
在M1 Pro MacBook Pro上安装PyAudio失败的求助
我在M1 Pro MacBook Pro上始终无法完成PortAudio相关的PyAudio安装。试过直接pip install pyaudio、带/不带sudo的指定编译参数命令(如下方代码),甚至直接编译PyAudio源码,都出现相同错误——明明已经确认/opt/homebrew/include路径下存在PortAudio,但编译时还是报错找不到portaudio.h,求解决办法。
执行的安装命令
sudo -H pip install pyaudio --global-option="build_ext" --global-option="-I/opt/homebrew/include" --global-option="-L/opt/homebrew/lib"
错误输出日志
WARNING: Disabling all use of wheels due to the use of --build-option / --global-option / --install-option. Collecting pyaudio Downloading PyAudio-0.2.12.tar.gz (42 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.9/42.9 kB 872.0 kB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Installing backend dependencies ... done Preparing metadata (pyproject.toml) ... done Building wheels for collected packages: pyaudio Building wheel for pyaudio (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for pyaudio (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [16 lines of output] running bdist_wheel running build running build_py creating build creating build/lib.macosx-12-arm64-cpython-39 copying src/pyaudio.py -> build/lib.macosx-12-arm64-cpython-39 running build_ext building '_portaudio' extension creating build/temp.macosx-12-arm64-cpython-39 creating build/temp.macosx-12-arm64-cpython-39/src clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -DMACOSX=1 -I/usr/local/include -I/usr/include -I/opt/homebrew/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c src/_portaudiomodule.c -o build/temp.macosx-12-arm64-cpython-39/src/_portaudiomodule.o src/_portaudiomodule.c:31:10: fatal error: 'portaudio.h' file not found #include "portaudio.h" ^~~~~~~~~~~~~ 1 error generated. error: command '/usr/bin/clang' failed with exit code 1 [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for pyaudio Failed to build pyaudio ERROR: Could not build wheels for pyaudio, which is required to install pyproject.toml-based projects
解决建议
确认PortAudio实际路径
先定位portaudio.h的真实位置:find /opt/homebrew -name portaudio.h如果路径不是
/opt/homebrew/include(比如在/opt/homebrew/Cellar/portaudio/[具体版本号]/include),安装命令里的-I参数要替换成这个实际路径。用Homebrew直接安装适配版本
跳过pip编译环节,直接通过Homebrew安装适配M1架构的包:brew install portaudio brew install pyaudio安装完成后在Python中验证:
import pyaudio设置环境变量引导编译器
先配置环境变量让clang找到PortAudio,再执行pip安装:export CFLAGS="-I/opt/homebrew/include" export LDFLAGS="-L/opt/homebrew/lib" pip install pyaudio注意不要加sudo,避免权限冲突,同时确保使用的是当前用户的Python环境(而非系统默认Python)。
检查Python架构匹配度
M1是arm64架构,需确认使用的Python是原生arm64版本,而非Rosetta转译的x86_64版本:python3 -c "import platform; print(platform.machine())"若输出为
x86_64,需重新安装适配arm64的Python(推荐用Homebrew安装)。更新Xcode命令行工具
确保编译工具为最新版本:xcode-select --install sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
内容的提问来源于stack exchange,提问作者DirtOnYourJhin
相关产品推荐
相关产品推荐

