Installing PyAudio on Windows

Viewed 6606

Installing PyAudio on Windows 10 error

I want to install PyAudio

I have Windows 10, Python 3.7, pip 8.1

I'm installing it in Command Prompt with command pip install PyAudio
But there's an error error: command 'D:\\VisualStudio2017\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX86\\x86\\cl.exe' failed with exit status 2

Command "c:\python37\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\USER\\AppData\\Local\\Temp\\pip-install-is1aobj6\\pyaudio\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\USER\AppData\Local\Temp\pip-record-50bh4kz0\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\USER\AppData\Local\Temp\pip-install-is1aobj6\pyaudio\

Here's a screenshot

Thanks for reading my question. I hope you can help :D

1 Answers

Well, I've had just this problem when I wanted to install pylisten (which has PyAudio as a dependency).

There are two problems here - first, your MSVC can't find portaudio.h and after that there will be a problem with the min macro when building. But first things first.

Get portaudio

This is a C dependency for PyAudio, so you need to have include files, in particular portaudio.h like you see on the screenshot. It's possible to get binaries for Windows, but if your using Anaconda, the simplest way to do it is to install it using conda:

(base) C:\> conda search portaudio
Loading channels: done
# Name                       Version           Build  Channel
portaudio                     19.6.0      h0c8e037_3  pkgs/main
portaudio                     19.6.0      hfa6e2cd_3  pkgs/main

(base) C:\> conda install portaudio
(...)

Next, we would like to know where the files from portaudio library actually went. There is no conda package --list command... (see here: https://github.com/conda/conda/pull/7457), so we need to do some magic found elsewhere on SO:

(base) C:\>dir %CONDA_PREFIX%\conda-meta\portaudio*
(...)
26.01.2020  13:17             5 577 portaudio-19.6.0-hfa6e2cd_3.json

We need to check this JSON and find the files property:

  "extracted_package_dir": "C:\\Users\\(...)\\Anaconda3\\pkgs\\portaudio-19.6.0-hfa6e2cd_3",                             
  "features": "",
  "files": [
    "Library/bin/portaudio.dll",
    "Library/include/pa_asio.h",
    "Library/include/pa_jack.h",
    "Library/include/pa_linux_alsa.h",
    "Library/include/pa_mac_core.h",
    "Library/include/pa_win_ds.h",
    "Library/include/pa_win_wasapi.h",
    "Library/include/pa_win_waveformat.h",
    "Library/include/pa_win_wdmks.h",
    "Library/include/pa_win_wmme.h",
    "Library/include/portaudio.h",
    "Library/lib/portaudio.lib",
    "Library/lib/portaudio_static.lib",
    ".nonadmin"
  ],                                                                                                                  

The extracted_package_dir (username removed above) actually amounts to:

%CONDA_PREFIX%\pkgs\portaudio-19.6.0-hfa6e2cd_3.

So, for this specific build, the necessary include directories (-I) are:

%CONDA_PREFIX%\pkgs\portaudio-19.6.0-hfa6e2cd_3\Library\include

And link directories (-L) are:

%CONDA_PREFIX%\pkgs\portaudio-19.6.0-hfa6e2cd_3\Library\lib

This will be useful below.

Simple way, which doesn't work

Now if your Conda environment has MSVC 14.0 for building native extensions you should be fine with:

(base) C:\> pip install --global-option=build_ext --global-option="-I%CONDA_PREFIX%\pkgs\portaudio-19.6.0-hfa6e2cd_3\Library\include" --global-option="-L%CONDA_PREFIX%\pkgs\portaudio-19.6.0-hfa6e2cd_3\Library\lib" pyaudio

Now, it will find portaudio.h!

But alas, at the time of writing, this fails with a compile error. I still put this here, because maybe the incompatibility with MSVC will be fixed.

The hard, but successful, way

We need to hack the sources of PyAudio. This can be done like so:

(base) C:\>pip download pyaudio

This will download a file like PyAudio-0.2.11.tar.gz (version number may differ in the future). You need to unpack it, using tar. I have it in C:\Windows\system32 - maybe because of WSL. You can also use 7-zip to unpack it. Anyway:

(base) C:\>tar xf PyAudio-0.2.11.tar.gz
(... unpacked ...)
(base) C:\>cd PyAudio-0.2.11
(base) C:\PyAudio-0.2.11>

Now to get the same result as before, you need to pass the special arguments to setup.py (in a different way than it's done for pip, of course):

(base) C:\PyAudio-0.2.11>python setup.py build_ext --include-dirs="%CONDA_PREFIX%\pkgs\portaudio-19.6.0-hfa6e2cd_3\Library\include" --library-dirs="%CONDA_PREFIX%\pkgs\portaudio-19.6.0-hfa6e2cd_3\Library\lib" install
(compile errors: min macro redefinition, __typeof__ undefined)

Now, edit the file src\_portaudiomodule.c, and delete/comment out these lines (right at the top, line ~39):

#define min(a, b)           \
  ({                        \
    __typeof__(a) _a = (a); \
    __typeof__(b) _b = (b); \
    _a < _b ? _a : _b;      \
  })

This trivial macro is the culprit. If you want to submit a fix to Mr Pham (http://people.csail.mit.edu/hubert/pyaudio/), don't delete it, but use something like #ifndef _MSC_VER ... #endif.

Save the file and run this (again):

(base) C:\PyAudio-0.2.11>python setup.py build_ext --include-dirs="%CONDA_PREFIX%\pkgs\portaudio-19.6.0-hfa6e2cd_3\Library\include" --library-dirs="%CONDA_PREFIX%\pkgs\portaudio-19.6.0-hfa6e2cd_3\Library\lib" install

Everything should be fine now.

EDIT: TL;DR easy way

After checking once again, I saw pyaudio in https://repo.anaconda.com/pkgs/main/win-64. This means, if you're using Anaconda you can just do:

conda install pyaudio

I'm leaving the rest of this answer for educational purposes.

Related