ModuleNotFoundError: No module named 'pyaudio' (Windows)

It happens quite often that someone installs a Python package using pip, but then can't seem to import it in Python. To understand why this happens, you must know how Windows finds executables to run, and how the Python software is installed. The basics:

  • When running a command, Windows searches for an executable in the environment variable PATH. It executes the first one found.
  • The Python interpreter, python.exe, is installed in <PYTHON_INSTALL_DIR> (e.g. C:\Python\3.7).
  • Python tools such as pip, pylint, virtualenv, PyCrust, etc., are installed in <PYTHON_INSTALL_DIR>\Scripts.
  • The Python launcher for Windows, py.exe, is installed in your Windows system directory (e.g. C:\Windows).
  • python and pip commands use the modules found in the directory their installed in, they do not look at PATH.

So, let's say you have the following Python versions:

C:\Python\2.7
C:\Python\3.6
C:\Python\3.7

and your PATH environment contains the following directories:

C:\Python\2.7
C:\Python\3.6\Scripts

then, see the following output:

C:\>python -V
Python 2.7.16

C:\>pip -V
pip 19.1.1 from c:\python\3.6\lib\site-packages\pip (python 3.6)

C:\>py -V
Python 3.7.3

So, when running pip, it is possible that the packages are installed in another Python version then the version you'll get when running python.

To see which versions are (correctly) installed on your system, run py -0p. Example output:

C:\>py -0p
Installed Pythons found by py Launcher for Windows
 -3.7-64        C:\Python\3.7-64\python.exe *
 -3.7-32        C:\Python\3.7-32\python.exe
 -3.6-64        C:\Python\3.6-64\python.exe
 -2.7-64        C:\Python\2.7-64\python.exe
 -2.7-32        C:\Python\2.7-32\python.exe

General solution (for Windows)

The best thing is not to rely on your system PATH. Use the py launcher to select the version you want. To run the pip module corresponding to the Python version you want to use, start pip as a module instead of executable. So instead of:

pip install <package>

run:

py -3.6 -m pip install <package>

To see which Python packages you have installed for that Python version, use:

py -3.6 -m pip freeze

Some additional remarks

  • Whether a Python installation is added to your PATH or not, is an option during the installation. If it is added, it is added at the beginning of the PATH, so the most recently installed Python version will be selected first.
  • The Windows system directory should always be in your PATH, so the py command will always be available, even if you did not add any Python installation to your PATH.
  • If you double-click on a .py file from Windows Explorer, or type the filename directly as a command in a Command Prompt (e.g. test.py), then the action is determined from the Windows registry. It is possible that the file will be opened in your IDE, or that it's executed using a Python interpreter. In that case, it is probably the most recently installed Python version. It's possible that the command python test.py, uses a different Python version than the command test.py.
  • Some installations also include executables named python2/python3 (not on Windows), pip3/pip3.7 (also on Windows), etc. This would also allow you to specify which version to use. These would be useful on systems where these binaries exist and are in the path.

You'd think python -m pip install pyaudio would solve the problem but it does not.

To install pyaudio you'd first need to install pipwin using:

pip install pipwin

pipwin is a complementary tool for pip on Windows. pipwin installs unofficial python packages.

Next, use pipwin to install pyaudio,

pipwin install pyaudio