7

I have visited many forums, tried diffrent methods like brew, pip, port and many more but still am facing the same error.

View this Image for more detail

src/_portaudiomodule.c:29:10: fatal error: 'portaudio.h' file not found
    #include "portaudio.h"
             ^~~~~~~~~~~~~
    1 error generated.
    error: command '/usr/bin/gcc' failed with exit code 1

Can anyone help?

Vishal Kumar
  • 73
  • 1
  • 1
  • 3

8 Answers8

33

These steps worked on M1 Pro chips

  1. Install portaudio
brew install portaudio
  1. Link portaudio
brew link portaudio
  1. Copy the path where portaudio was installed (use it in the next step)
brew --prefix portaudio
  1. Create .pydistutils.cfg in your home directory
sudo nano $HOME/.pydistutils.cfg

then paste the following

[build_ext]
include_dirs=<PATH FROM STEP 3>/include/
library_dirs=<PATH FROM STEP 3>/lib/

  1. Install pyaudio
pip install pyaudio

or

pip3 install pyaudio
selected
  • 764
  • 2
  • 10
  • 19
12

For me it was:

brew install portaudio
python -m pip install --global-option='build_ext' --global-option='-I/opt/homebrew/Cellar/portaudio/19.7.0/include' --global-option='-L/opt/homebrew/Cellar/portaudio/19.7.0/lib' pyaudio
FRV
  • 691
  • 5
  • 4
6

This Solution is tested on M1 Macs[Please do check with other].

After the installation of HomeBrew on your system, perform the installation of PortAudio. Next follow the steps mentioned below:

Use the command to install PortAudio

sudo brew install portaudio

After successful installation of PortAudio, enter the following command.

sudo nano $HOME/.pydistutils.cfg

Next, enter the following lines in the opened window

[build_ext]
include_dirs=/Users/<enter-your-system-username>/homebrew/Cellar/portaudio/19.20140130/include/
include_dirs=/Users/<enter-your-system-username>/homebrew/Cellar/portaudio/19.20140130/lib/

Note: PortAudio location may be different for you and also don't forget to replace your PC username.

Finally run the command

pip install pyaudio

or

pip3 install pyaudio

vkshah
  • 76
  • 3
  • 2
    This solution worked for me, but at first I was getting this error: `pydistutils.cfg' [line 3]: option 'include_dirs' in section 'build_ext' already exists`. After the first `include_dirs`, I added a new section `build_ext_1` for the second `include_dirs` and it worked. – Shend Nov 04 '21 at 08:45
  • @shend What was the lines you wrote? I'm not sure I understand but same error as you. – DoPeT Mar 30 '22 at 00:31
  • You can't run home brew as root anymore. So first follow this solution, https://stackoverflow.com/a/34175205/3940670 . Then try the brew commands with no sudo. – M.Hossein Rahimi Aug 27 '23 at 22:36
3

The main challenge we're encountering here is that pyaudio doesn't know where to find the portaudio libraries since, according to the source, it only looks in these locations for darwin platform installations:

include_dirs += ['/usr/local/include', '/usr/include']
external_libraries_path += ['/usr/local/lib', '/usr/lib']

This won't do if you're using a homebrew installation of portaudio, particularly so with Apple Silicon since there isn't a pre-built wheel in PyPi. This means setuptools/pip will need to build the package from source.

There are a few ways of configuring setuptools to use additional paths, including passing arguments, using a .pydistutils.cfg file, among others, but the second option worked for me given I'm installing pyaudio to a few Python environments.

Building off of some of the answers above, you can copy the following below to install pyaudio in one go.

# Install portaudio, will print list of paths where it's been installed if already done
brew list portaudio || brew install portaudio
# Link portaudio, will print a warning if already linked
brew link portaudio
# Create a pydistutils config file with the portaudio lib paths set correctly (no copying/pasting paths required)
cat <<EOF >> .pydistutils.cfg
[build_ext]
include_dirs=`brew --prefix portaudio`/include/
library_dirs=`brew --prefix portaudio`/lib/
EOF
# install (build) package with pip
python -m pip install pyaudio

All we're doing is telling setuptools to also look for C/C++ headers where portaudio has been installed by homebrew.

  • After so many trials I finally found something that worked! I tried with the global config for pip, but id didn't work. This solution seems to work instead – Mattia Surricchio Sep 29 '22 at 20:05
  • after running this script, unfortunately I get this error on m1. src/_portaudiomodule.c:31:10: fatal error: 'portaudio.h' file not found #include "portaudio.h" – Felipe Valdes Oct 01 '22 at 23:44
  • if anybody knows how to fix, that help would be, well... helpful. – Felipe Valdes Oct 01 '22 at 23:44
2

vkshah has an error in his second line. It should ready library_dirs instead of include_dirs:

include_dirs=/Users/<enter-your-system-username>/homebrew/Cellar/portaudio/19.20140130/include/
library_dirs=/Users/<enter-your-system-username>/homebrew/Cellar/portaudio/19.20140130/lib/```
MSP
  • 41
  • 5
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31594722) – Arnie97 Apr 27 '22 at 16:35
  • It does "provide answers that don't require a clarification from the asker", namely that the answer given by vkshah had a typo that would have made it incorrect. On top of the code snippets, I gave an explanation that was pretty darned clear (library_dirs instead of include_dirs). So, would you rather have the vkshah's original answer stand (which I am very thankful for, as I was completely stuck, and this got me 95% there), or have the correct answer that would actually solve users' problems? – MSP May 20 '22 at 19:27
  • Hi, I'm sorry for the words above. I didn't expect that my choice in the moderator review queue would trigger some rude automatic bot reply like that. I fully agree and appreciate your errata about vkshah's answer. However, I would suggest to use the "edit" or "improve this answer" link under vkshah's answer instead, which is more friendly to future readers. After the community volunteers accept your improvements, you will be credited in the original answer as an editor, and this separate answer entry could be deleted then. – Arnie97 May 30 '22 at 09:13
1

For me, I am running Big Sur on an M1 Mac. I followed all the instructions mentioned in other threads to:

  1. install portaudio

    arch -arm64 brew install portaudio

  2. start a venv

    python3 -m venv env

    source env/bin/activate

  3. install pyaudio

    arch -arm64 pip install --no-cache-dir --global-option='build_ext' --global-option='-I/opt/homebrew/include' --global-option='-L/opt/homebrew/lib' pyaudio==0.2.11

TIP #1

If you see error on portaudio.h file not found, the solution is specifying the pyaudio version at the end. Because it kept trying to install version '0.2.12' which failed. I specify the version of pyaudio to '0.2.11' (see above).

TIP #2

After successfully installing pyaudio, you may see error PyAudio C module _portaudio, that is due to pip built it(_portaudio.cpython-36m-darwin.so) in x86_64 architecture. It's not compatible with arm64 portaudio, in this case, make sure to include arch -arm64 in front of pip to build the correct binary.

TIP #3

Use an older version of python, I personally used version 3.6.

Oh, and make sure the single quote is actually ' if you copied the command from a text editor. Hope this helps!

cliuny
  • 11
  • 2
0

first you need to download portaudio using homebrew

brew install portaudio

Then try to install pyaudio directly

pip install pyaudio

if you are getting error while installing follow below step

Or

if you are facing any error mentioned bellow you can follow these steps


error 1

option 'include_dirs' in section 'build_ext' already exists


error 2

Could not build wheels for pyaudio, which is required to install pyproject.toml-based projects


error 3

#include "portaudio.h" ^~~~~~~~~~~~~
1 error generated. error: command '/usr/bin/clang' failed with exit code 1


solution start here

after installing homebrew you have to link it

brew link portaudio

in some cases it will say that it has been already linked then simply ignore

we need path of portaudio where it is exactly installed

brew --prefix portaudio

it will gives you path of portaudio

then you have to check if pydistutils.cfg file exists or not

sudo cat $HOME/.pydistutils.cfg

if file exists then it will open else create it will below command

sudo cat $HOME/.pydistutils.cfg

so in these file we have to define proper path here

[build_ext]
include_dirs=/--PATH--/include/
library_dirs=/--PATH--/lib/

you can get that path by running below command

brew --prefix portaudio

if that path is not working then try below path

[build_ext]
include_dirs=/Users/<username>/homebrew/Cellar/portaudio/19.20140130/include/
library_dirs=/Users/<username>/homebrew/Cellar/portaudio/19.20140130/lib/
akash
  • 3
  • 4
0

If you use mac os

  1. Install Homebrew (insert link into the terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" )

  1. Insert command into the terminal:

brew install portaudio

  1. Insert command into the terminal:

pip install pyaudio

p.s. sometimes you need upgrade your pip installer

These actions solved my problem (mac os Mojave 10.14.4)

Tony
  • 1