🐕

fatal error: 'sndfile.h' file not found

2023/04/12に公開

macOS環境でpythonのsndfileモジュールのインストールに失敗する

> pip install sndfile

Collecting sndfile                                                                                                                                                                           
  Using cached sndfile-0.2.0.tar.gz (4.3 kB)                                                                                                                                                 
  Preparing metadata (setup.py) ... done                                                                                                                                                     
Requirement already satisfied: cffi>=1.0.0 in /Users/kstg/.asdf/installs/python/3.8.16/lib/python3.8/site-packages (from sndfile) (1.15.1)                                                   
Requirement already satisfied: pycparser in /Users/kstg/.asdf/installs/python/3.8.16/lib/python3.8/site-packages (from cffi>=1.0.0->sndfile) (2.21)                                          
Installing collected packages: sndfile                                                                                                                                                       
  DEPRECATION: sndfile is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enf
orce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559                                
  Running setup.py install for sndfile ... error                                                                                                                                             
  error: subprocess-exited-with-error                                                                                                                                                        
                                                                                                                                                                                             
  × Running setup.py install for sndfile did not run successfully.                                                                                                                           
  │ exit code: 1                                                                                                                                                                             
  ╰─> [23 lines of output]                                                                                                                                                                   
      running install                                                                                                                                                                        
      running build                                                                                                                                                                          
      running build_py                                                                                                                                                                       
      creating build                                                                                                                                                                         
      creating build/lib.macosx-13.2-arm64-3.8                                                                                                                                               
      creating build/lib.macosx-13.2-arm64-3.8/sndfile                                                                                                                                       
      copying sndfile/build.py -> build/lib.macosx-13.2-arm64-3.8/sndfile                                                                                                                    
      copying sndfile/io.py -> build/lib.macosx-13.2-arm64-3.8/sndfile                                                                                                                       
      copying sndfile/vio.py -> build/lib.macosx-13.2-arm64-3.8/sndfile                                                                                                                      
      copying sndfile/__init__.py -> build/lib.macosx-13.2-arm64-3.8/sndfile                                                                                                                 
      copying sndfile/formats.py -> build/lib.macosx-13.2-arm64-3.8/sndfile                                                                                                                  
      running build_ext                                                                                                                                                                      
      generating cffi module 'build/temp.macosx-13.2-arm64-3.8/sndfile._sndfile.c'
      creating build/temp.macosx-13.2-arm64-3.8
      building 'sndfile._sndfile' extension
      creating build/temp.macosx-13.2-arm64-3.8/build
      creating build/temp.macosx-13.2-arm64-3.8/build/temp.macosx-13.2-arm64-3.8
      clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -DOPENSSL_NO_SSL3 -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Users/kstg/.asdf/installs/python/3.8.16/include/python3.8 -c build/temp.macosx-13.2-arm64-3.8/sndfile._sndfile.c -o build/temp.macosx-13.2-arm64-3.8/build/temp.macosx-13.2-arm64-3.8/sndfile._sndfile.o
      build/temp.macosx-13.2-arm64-3.8/sndfile._sndfile.c:571:10: fatal error: 'sndfile.h' file not found
      #include <sndfile.h>
               ^~~~~~~~~~~
      1 error generated.
      error: command 'clang' failed with exit status 1
      [end of output]
   
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> sndfile

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

sndfileはhomebrewで入れた

brew install libsndfile

ヘッダファイルの位置を確認

find /opt/homebrew/include -name sndfile.h                                               
/opt/homebrew/include/sndfile.h

コンパイルオプションで渡す

> pip install sndfile --global-option=build_ext --global-option="-I/opt/homebrew/include

Discussion