Skip to content

[已解決] fatal error: portaudio.h: No such file or directory 9 | #include "portaudio.h" | ^~~~~~~~~~~~~ compilation terminated

Last Updated on 2023-12-24 by Clay

問題描述

今天當我在一台新的 Linux 筆電上想要安裝 pyaudio(Python 中經常用於錄音的套件)時,我遇到了之前沒有遇過的錯誤:

  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                                                                                                                                                                              
  ╰─> [18 lines of output]                                                                                                                                                                    
      running bdist_wheel                                                                                                                                                                     
      running build                                                                                                                                                                           
      running build_py                                                                                                                                                                        
      creating build                                                                                                                                                                          
      creating build/lib.linux-x86_64-cpython-310                                                                                                                                             
      creating build/lib.linux-x86_64-cpython-310/pyaudio                                                                                                                                     
      copying src/pyaudio/__init__.py -> build/lib.linux-x86_64-cpython-310/pyaudio                                                                                                           
      running build_ext                                                                                                                                                                       
      building 'pyaudio._portaudio' extension                                                                                                                                                 
      creating build/temp.linux-x86_64-cpython-310                                                                                                                                            
      creating build/temp.linux-x86_64-cpython-310/src                                                                                                                                        
      creating build/temp.linux-x86_64-cpython-310/src/pyaudio                                                                                                                                
      gcc -pthread -B /home/clay/Tools/anaconda3/envs/llms/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /home/clay/Tools/anaconda3/envs/llm
s/include -fPIC -O2 -isystem /home/clay/Tools/anaconda3/envs/llms/include -fPIC -I/usr/local/include -I/usr/include -I/home/clay/Tools/anaconda3/envs/llms/include/python3.10 -c src/pyaudio/d
evice_api.c -o build/temp.linux-x86_64-cpython-310/src/pyaudio/device_api.o                                                                                                                   
      src/pyaudio/device_api.c:9:10: fatal error: portaudio.h: No such file or directory                                                                                                      
          9 | #include "portaudio.h"                                                                                                                                                          
            |          ^~~~~~~~~~~~~                                                                                                                                                          
      compilation terminated.                                                                                                                                                                 
      error: command '/usr/bin/gcc' 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

解決方法

我們可以定位到編譯錯誤的位置:9 | #include "portaudio.h",這代表當前系統可能缺乏相關的函式庫。

在查詢了相關的 Stackoverflow 討論後,我根據我的作業系統 Debian/Ubuntu 發行板,進行了portaudio19-dev 的安裝 —— 這將安裝 PortAudio 的開發文件以及剛剛編譯錯誤中提示所缺乏的標頭文件 portaudio.h

sudo apt install portaudio19-dev


之後,我們便可以再次嘗試安裝 pyaudio 套件。

pip3 install pyaudio

References


Read More

Leave a Reply