P i
P i

Reputation: 30684

Installing PyAudio for Python 3 on OS X

I would like to construct a "Hello, World!" of audio for OS X/Python 3 that populates a buffer with a sine wave and plays it through the speakers.

My basic options are listed in Audio in Python.

Playing Music with Python demonstrates several of these, but it omits details of installation.

I'm trying PyAudio first.

PyAudio has a section on OS X, saying "For Python 3 support, first install MacPython 3.3"

This confuses me greatly. OS X ships with Python. Also, I have used Homebrew to install Python 3. I've never come across the term MacPython, so I'm not sure if what I have is MacPython or not. And if it isn't, then I want to install PyAudio for the Python I currently have. I don't want to have to download some special Python to use it. That would be completely defeating the purpose.

I also tried "pip3 install pyaudio", with the following (negative) results:

⤐  pip3 install pyaudio
 Collecting pyaudio
   Could not find a version that satisfies the requirement pyaudio (from versions: )
   Some externally hosted files were ignored as access to them may be unreliable (use --allow-external pyaudio to allow).
 No matching distribution found for pyaudio
  ✘

 [email protected] ~ /Users/pi:
⤐  pip install --allow-external pyaudio
 You must give at least one requirement to install (see "pip help install")
  ✔

 [email protected] ~ /Users/pi:
⤐  pip install pyaudio --allow-external pyaudio
 Collecting pyaudio
   Could not find a version that satisfies the requirement pyaudio (from versions: )
   Some insecure and unverifiable files were ignored (use --allow-unverified pyaudio to allow).
 No matching distribution found for pyaudio
  ✘

It's frustrating that it is so awkward to even get onto the first rung of the ladder.

How can I correctly install PyAudio for Python 3 on my (up-to-date) OS X? Should I use Homebrew? Should I use pip3?

PS: PyAudio with Homebrew -- I hope I don't have to follow this solution

PPS: Music software written in Python lists a daunting number of possible solution paths -- I'm not at all certain PyAudio is the best path. Is there a better one?

Upvotes: 2

Views: 6496

Answers (5)

mommi84
mommi84

Reputation: 654

Unfortunately, 王淳龙's solution did not work for me (macOS v10.15.6 (Catalina), Python 3.8.5). The following errors persisted:

  gcc-5 -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/tom/anaconda3/include -arch x86_64 -I/Users/tom/anaconda3/include -arch x86_64 -DMACOSX=1 -I/Users/tom/anaconda3/include/python3.6m -c src/_portaudiomodule.c -o build/temp.macosx-10.9-x86_64-3.6/src/_portaudiomodule.o
  In file included from src/_portaudiomodule.c:33:0:
  /usr/local/include/pa_mac_core.h:48:33: fatal error: AudioUnit/AudioUnit.h: No such file or directory
  compilation terminated.
  error: command 'gcc-5' failed with exit status 1

Therefore, I had to link the missing libraries manually:

cd /usr/local/include/
ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/AudioUnit.framework/Versions/A/Headers AudioUnit
ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/AudioToolbox.framework/Versions/A/Headers AudioToolbox
ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/CoreAudioTypes.framework/Versions/A/Headers CoreAudioTypes
ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/CoreFoundation.framework/Versions/A/Headers CoreFoundation
ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/CoreAudio.framework/Versions/A/Headers CoreAudio

This solved the dependencies, but not the following compiler problem:

    In file included from /usr/local/include/AudioToolbox/AUComponent.h:65:0,
                     from /usr/local/include/AudioUnit/AUComponent.h:1,
                     from /usr/local/include/AudioUnit/AudioUnit.h:11,
                     from /usr/local/include/pa_mac_core.h:48,
                     from src/_portaudiomodule.c:33:
    /usr/local/include/AudioToolbox/AudioComponent.h:509:39: error: expected ')' before '^' token
                                     void (^inCompletionHandler)(AudioComponentInstance __nullable, OSStatus))
                                           ^

Forcing the use of system-wide gcc (Apple Clang version 11.0.3) instead of gcc-5 solved the problem. I did that by uninstalling gcc@5 and linking gcc-5 to gcc. However, there must be a more elegant way.

Upvotes: 2

Miruska Malyschka
Miruska Malyschka

Reputation: 43

(This works on Mac OS X)

At first PyAudio has a portaudio dependency:

brew install portaudio

And then run:

pip3 install pyaudio --global-option="build_ext" --global-option="-I/usr/local/include" --global-option="-L/usr/local/lib"

Or sometimes magic happens, if you use:

python3 -m pip install pyaudio --global-option="build_ext" --global-option="-I/usr/local/include" --global-option="-L/usr/local/lib"

Upvotes: 2

rongerZeng
rongerZeng

Reputation: 21

Today I also encountered this problem and searched a lot. Finally I found a solution:

  1. brew install portaudio
  2. pip install pyaudio

Upvotes: 1

王淳龙
王淳龙

Reputation: 61

Use Homebrew to install the prerequisite portaudio library, then install PyAudio using pip:

brew install portaudio 

pip install pyaudio

Notes:

If not already installed, download Homebrew. pip will download the PyAudio source and build it for your version of Python. Homebrew and building PyAudio also require installing the Command Line Tools for Xcode (more information).

Upvotes: 6

l'L'l
l'L'l

Reputation: 47189

Python vs MacPython:

AFAIK there is no such thing as MacPython — or at least not from the developers of Python. I can only assume whoever created the link on the PyAudio page tried to get creative with the name and were actually meaning the "Mac OS X 64-bit/32-bit installer containing Python v3.3".

The link to the mysteriously named MacPython leads to the main Python download page that features the most recent stable versions (which as of today is v3.4.3):

https://www.python.org/downloads/release/python-343/

PyAudio:

PyAudio can be compiled from source, outlined here, which might yield better results.

Upvotes: 0

Related Questions