Installing PyAudio for Python 3 on OS X - macos

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
✘
pi#piBookAir.local ~ /Users/pi:
⤐ pip install --allow-external pyaudio
You must give at least one requirement to install (see "pip help install")
✔
pi#piBookAir.local ~ /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?

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).

(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"

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.

Today I also encountered this problem and searched a lot. Finally I found a solution:
brew install portaudio
pip install pyaudio

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.

Related

Installing Cairo on Mac - Could not find libpng in the pkg-config search path

checking whether cairo's PNG functions feature could be enabled... no
I have been following the setup for configuring node canvas. Cairo is needed but i am having issues trying to set it up. https://github.com/Automattic/node-canvas/wiki/Installation---OSX
If libpng fails and you get the error "error: recommended PNG functions feature could not be enabled", try installing this binary and re-running the configure step.
I followed above by installing the binaries on Mac OS 9 and run the configuration command and still no luck. I am running the install shell script in node-canvas repository:
https://github.com/Automattic/node-canvas/blob/master/install
In my /usr/local/lib I currently have:
dtrace libjpeg.8.dylib libpng.a libpng14.a
libfreetype.6.dylib libjpeg.a libpng.dylib libpng14.dylib
libfreetype.a libjpeg.dylib libpng.la libpng14.la
libfreetype.dylib libjpeg.la libpng12.0.dylib libpng15.15.dylib
libfreetype.la libpixman-1.0.dylib libpng12.0.dylib.dSYM libpng15.15.dylib.dSYM
libgif.4.1.6.dylib libpixman-1.a libpng12.a libpng15.a
libgif.4.dylib libpixman-1.dylib libpng12.dylib libpng15.dylib
libgif.a libpixman-1.la libpng12.la libpng15.la
libgif.dylib libpng.3.dylib libpng14.14.dylib node_modules
libgif.la libpng.3.dylib.dSYM libpng14.14.dylib.dSYM pkgconfig
Libpng-1.6.10 has been replaced by libpng-1.6.12.
In your install script, change
LIBPNG="http://downloads.sourceforge.net/project/libpng/libpng16/1.6.10/libpng-1.6.10.tar.gz"
to
LIBPNG="http://downloads.sourceforge.net/project/libpng/libpng16/1.6.12/libpng-1.6.12.tar.gz"
I have a set of detailed instructions for building cairo here:
https://github.com/soegaard/racket-osx-libs
There resulting libraries are here:
https://github.com/soegaard/racket-osx-libs/tree/master/lib

libpng version incompatibility in fresh installation of IPython

I used this guide to install the "scientific stack" for Python (OSX 10.9.2, brewed Python 2.7.6, IPython 2.0, matplotlib 1.3.1, libpng 1.6.10). Everything was looking good.
However, trying to run a simple plot in IPython's notebook environment with --pylab=inline gives me this error:
/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/formatters.py:239: FormatterWarning: Exception in image/png formatter: Could not create write struct
FormatterWarning,
And in the terminal it says: libpng warning: Application built with libpng-1.5.17 but running with 1.6.10
I have no other libpng installed as far as I can tell. I tried deleting all files beginning with libpng from /usr/local/ and reinstalling everything, to no avail. The output from building matplotlib (pip install matplotlib) contains:
BUILDING MATPLOTLIB
matplotlib: yes [1.3.1]
python: yes [2.7.6 (default, Mar 16 2014, 15:04:47) [GCC
4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.38)]]
platform: yes [darwin]
REQUIRED DEPENDENCIES AND EXTENSIONS
numpy: yes [version 1.8.1]
dateutil: yes [using dateutil version 2.2]
tornado: yes [using tornado version 3.2]
pyparsing: yes [using pyparsing version 2.0.1]
pycxx: yes [Couldn't import. Using local copy.]
libagg: yes [pkg-config information for 'libagg' could not
be found. Using local copy.]
freetype: yes [version 17.2.11]
png: yes [version 1.6.10]
OPTIONAL SUBPACKAGES
sample_data: yes [installing]
toolkits: yes [installing]
tests: yes [using nose version 1.3.1]
OPTIONAL BACKEND EXTENSIONS
macosx: yes [installing, darwin]
qt4agg: yes [installing, Qt: 4.8.6, PyQt4: 4.10.4]
gtk3agg: no [Requires pygobject to be installed.]
gtk3cairo: no [Requires cairo to be installed.]
gtkagg: no [Requires pygtk]
tkagg: yes [installing, version 81008]
wxagg: no [requires wxPython]
gtk: no [Requires pygtk]
agg: yes [installing]
cairo: no [cairo not found]
windowing: no [Microsoft Windows only]
OPTIONAL LATEX DEPENDENCIES
dvipng: no
ghostscript: no
latex: no
pdftops: no
So it seems to me matplotlib should be compiled against libpng 1.6.10? Can someone help me figure out what's going on?
An expansion of the answer provided by #glenn-randers-pehrson:
pip uninstall matplotlib
cd /opt/X11/include/libpng15
mv png.h _png.h
mv pngconf.h _pngconf.h
mv pnglibconf.h _pnglibconf.h
pip install matplotlib
(if needed remove the old directory or use the force option)
(now move the .h files back to their original locations)
Look for old header files beginning with "png" (png.h, pngconf.h, perhaps others) and remove them.
For the sake of documentation (following on from above comments):
Remove X11
launchctl unload /Library/LaunchAgents/org.macosforge.xquartz.startx.plist
sudo launchctl unload /Library/LaunchDaemons/org.macosforge.xquartz.privileged_startx.plist
sudo rm -rf /opt/X11* /Library/Launch*/org.macosforge.xquartz.* /Applications/Utilities/XQuartz.app /etc/*paths.d/*XQuartz
sudo pkgutil --forget org.macosforge.xquartz.pkg
Then,
if you have matplotlib / python installed, run the following as appropriate:
pip uninstall matplotlib
pip uninstall ipython
Then,
pip install matplotlib
pip install ipython
If necessary, XQuartz can be re-installed from https://www.macupdate.com/app/mac/26593/xquartz.
[Note: This does re-add the /opt/X11/include/libpng15/png.h etc files, but ipython worked fine afterwards.]
While it is a bit ridiculous having multiple copies of libpng around, this problem results from the path for the pip compiler and python being different. You could also fix this problem through the path, and then recompiling, but the above solutions work too.

GTK+ installed with brew is not found when I try to compile other applications

I installed GTK with brew install gtk+ as I need it to compile xchat from source on my Mac. When I try to install it tough, it says Cannot find GTK! Not building GTK FrontEnd.
I suspect I should've updated the environment variables once installed gtk+ but I don't know how.
iMac:xchat-2.8.8 Domenico$ brew info gtk+
gtk+: stable 2.24.20
http://gtk.org/
/usr/local/Cellar/gtk+/2.24.20 (1184 files, 52M) * // here is the path!!!
Built from source
From: https://github.com/mxcl/homebrew/commits/master/Library/Formula/gtk+.rb
==> Dependencies
Build: pkg-config, xz
Required: glib, jpeg, libtiff, gdk-pixbuf, pango, atk, cairo
Optional: jasper
==> Options
--with-jasper
Build with jasper support
The path is listed in the code above. I tried adding all kind of things related to it (e.g. /usr/local/Cellar/gtk+/2.24.20/bin or /usr/local/Cellar) to $LD_LIBRARY_PATH but it didn't work. Any ideas?
I had smiliar issues with after brew install gtk+. Specifically, I got:
Package xcb-shm was not found in the pkg-config search path.
Perhaps you should add the directory containing `xcb-shm.pc'
to the PKG_CONFIG_PATH environment variable
Package 'xcb-shm', required by 'cairo', not found
app.c:1:10: fatal error: 'gtk/gtk.h' file not found
#include <gtk/gtk.h>
^
1 error generated.
make: *** [app] Error 1
while running make for my app and this command solved my problem:
export PKG_CONFIG_PATH=/opt/X11/lib/pkgconfig
The extended answer on this problem has been already answered here: https://stackoverflow.com/a/23988317/4694621.
This might be an issue since gtk+ depends on these libraries (source):
Glib
Pango
ATK
GDK
GdkPixbuf
Cairo

How to install Scrapy on Mac OS X 10.7 ? (lxml error)

I'm a total newbie concerning Python and I have trouble installing Scrapy on Mac OS X 10.7. I've tried a lot of different things, but in summary, when I run:
scrapy startproject tutorial
I've got these errors :
ImportError: dlopen(/Library/Python/2.7/site-packages/lxml/etree.so, 2): Symbol not found: ___xmlStructuredErrorContext
Referenced from: /Library/Python/2.7/site-packages/lxml/etree.so
Expected in: flat namespace
in /Library/Python/2.7/site-packages/lxml/etree.so
After a lot of googling, I've followed this post and tried:
python setup.py build --static-deps --libxml2-version=2.7.8 && pip install lxml
but it tells me:
RuntimeError: ERROR: Trying to build without Cython, but pre-generated 'src/lxml/lxml.etree.c' is not available (pass --without-cython to ignore this error).
And if I run the same command with the —without-cython option, I get:
error: command 'llvm-gcc-4.2' failed with exit status 1
(though I've installed XCode command line tools)
Thanks in advance for your help !
(fyi, I am using MacPorts and pip install…)
You mention Macports at the end and also pip. I think you are mixing up packaging and installation methods.
The first import error should not be seen using a macports python as it puts its libraries under /opt/
As you have started on macports I would use the easy way
port install py27-scrapy
This will install all the needed packages (including python and lxml) to get scrapyto work.
To run scrapy you would need to use the macports python in /opt/local/bin/python2.7. This can be set as the default python if /opt/local/bin is on your path and you run port select to choose this as the python found. The scrapy executable script is /opt/local/bin/scrapy-2.7 Version numbers are used because you can have multiple versions of python working at once.

Installing Haskell packages on Mac

I can't seem to get a few Haskell packages to install on my Mac (10.6.8). I first tried Happstack and it failed and then I tried Snap.
Sometimes when I run ghci I get a segmentation fault.
Other times it works and goes like this:
GHCi, version 7.4.2: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude>
When trying to get Snap:
....
....
....
Loading package vector-0.10.0.1 ... linking ... done.
Loading package zlib-0.5.4.0 ... linking ... done.
Loading package zlib-bindings-0.1.1.3 ... linking ... done.
Loading package zlib-enum-0.2.3 ... linking ... done.
Loading package snap-core-0.9.3.1 ... linking ... done.
Loading package snap-server-0.9.3.3 ... linking ... done.
Loading package directory-tree-0.11.0 ... linking ... done.
cabal: Error: some packages failed to install:
snap-0.11.2 failed during the building phase. The exception was:
ExitFailure 11
I have seen this problem a few times but nothing really solved it for me. Any ideas?
Updated May 22, 2021
Have you tried using homebrew? It handles the dependencies for you.
Using homebrew, you can use the brew command to install ghc and cabal-install.
$ brew install ghc cabal-install
==> Downloading https://ghcr.io/v2/homebrew/core/ghc/manifests/8.10.4
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/ghc/blobs/sha256:965f94c14b56e3db7b239860e0a1d577be0b27caf8adb6212710a7430ce723d3
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:965f94c14b56e3db7b239860e0a1d577be0b27caf8adb6212710a7430ce723d3?se=2021-05-23T05%3A15%3A00Z&sig=fkH7sUY44VCLV8hI%2BtZT%2B
######################################################################## 100.0%
==> Pouring ghc--8.10.4.big_sur.bottle.tar.gz
==> /usr/local/Cellar/ghc/8.10.4/bin/ghc-pkg recache
🍺 /usr/local/Cellar/ghc/8.10.4: 6,907 files, 1.5GB
==> Downloading https://ghcr.io/v2/homebrew/core/cabal-install/manifests/3.4.0.0
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/cabal-install/blobs/sha256:2c0c5cc90d4739515721557f8e9c02783b3b5f106033c5c09241657b4418b21f
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:2c0c5cc90d4739515721557f8e9c02783b3b5f106033c5c09241657b4418b21f?se=2021-05-23T05%3A15%3A00Z&sig=O7ylxHgq42YVADlzTacdRfY7W
######################################################################## 100.0%
==> Pouring cabal-install--3.4.0.0.big_sur.bottle.tar.gz
🍺 /usr/local/Cellar/cabal-install/3.4.0.0: 7 files, 39.6MB
Once installed, you should be able to run:
$ ghc
ghc: no input files
Usage: For basic information, try the `--help' option.
As well as the Haskell interpreter, ghci:
$ ghci
GHCi, version 8.10.4: https://www.haskell.org/ghc/ :? for help
Prelude>
Hope this helps!
brew install haskell-platform is no longer supported. You may want to
Error: No available formula for haskell-platform
We no longer package haskell-platform. Consider installing ghc and cabal-install instead:
brew install ghc cabal-install
Since haskell-platform on homebrew for Mac is deprecated, you can use brew install ghc cabal-install instead. I did it on Yosemite and it took about one minute…
> $ brew install ghc cabal-install
==> Downloading https://homebrew.bintray.com/bottles/ghc-7.10.1_1.yosemite.bottl
######################################################################## 100.0%
==> Pouring ghc-7.10.1_1.yosemite.bottle.tar.gz
🍺 /usr/local/Cellar/ghc/7.10.1_1: 5423 files, 821M
==> Downloading https://homebrew.bintray.com/bottles/cabal-install-1.22.2.0.yose
######################################################################## 100.0%
==> Pouring cabal-install-1.22.2.0.yosemite.bottle.1.tar.gz
==> Caveats
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
==> Summary
🍺 /usr/local/Cellar/cabal-install/1.22.2.0: 6 files, 19M
This is an old question but given it comes quiet high on Google search. This is how I got here.
$ brew update
$ brew install haskell-stack
$ stack --version
$ stack --help # "man stack" gives something completely different
$ stack setup # Install GHC
$ stack ghci
Source: http://seanhess.github.io/2015/08/04/practical-haskell-getting-started.html
Why stack and not cabal?
Edit:
I found annoying to have to write "stack ghci" each time I wanted to use "ghci". A solution to this was to simply add these lines to the ~/.bash_aliases file:
alias sghc='stack ghc'
alias sghci='stack ghci'
alias srunhaskell='stack runhaskell'
Then source the file to apply:
$ source ~/.bash_aliases
Then I can run ghci and / or haskell running these commands, and it will use the stack version:
$ sghc [filename]
$ sghci
$ srunhaskell
Update: February 2016: Whilst other answers were helpful, I didn't find a complete, up-to-date answer.
The install is easy:
$ brew install ghc cabal-install
An extra step I needed to perform post-install was:
$ cabal update
followed by:
$ cabal install ghc-mod
See here for details.
Alternatively, instead of using homebrew for the install, visit Haskell Platform for the installer disk image - note that Haskell Platform is only compatible with OS X 10.6 and later.
Although, as a general rule I always use homebrew to manage my dependencies, when it comes to Haskell my recommendation (unless you really know what you are doing) is to just install this binary. This avoids any surprises further down the road. There is excellent documentation post install locally at: file:///Library/Haskell/doc/start.html
The popular answer to this question (using brew to install haskell-platform) is no longer supported.
Unfortunately, GHC 7.4 isn't supported on OS X 10.6; the download page only has an installer for 10.7, and as you can see, they mean it. I know your problem from bitter experience—I was in exactly your boat for a good long while, and I never found a solution other than upgrading Mac OS X (or, presumably, downgrading GHC, but I didn't really want to do that). A colleague of mine did manage to get 7.4.1 running on OS X 10.5, but it took nontrivial effort on her part (she had to use a different version of GMP, namely 5.0.5, and edit GHC's configure script), and I never tried to replicate it myself.
In the end, you have four options, in roughly decreasing order of simplicity:
Use an older Haskell Platform. This means downgrading to GHC 7.0.x (not GHC 7.2.x—that was never in the Platform); there's not a good record of which GHC versions were in which package versions, though. Wikipedia implies that 2011.2 was the first version with GHC 7.0.x and that 2012.2 was the first version with GHC 7.4.x, so I'd try 2011.4.0.0 first.
Install GHC 7.2 (which is supported on OS X) on its own, and forgo the Haskell Platform. This can cause headaches, but is possible. Don't forget to install Cabal, cabal-install, all the packages that come with the platform, etc.
Upgrade to OS X 10.7 or 10.8, if you feel like a more global (and pricier) change.
Try to replicate what my colleague did. (Best of luck, but it's definitely possible.)
Late 2017 answer. I'm running macOS Sierra -- 10.12.6 (16G29). This worked successfully with an install time on my MacBook Air of under five minutes:
brew cask install haskell-platform
Then ghc --version reported:
The Glorious Glasgow Haskell Compilation System, version 8.2.1

Resources