I’ve installed the module pyaudio using pip. However, when I try to import it, Python says the module is not found:
C:Usershp>pip install pyaudio Requirement already satisfied: pyaudio in c:usershpappdatalocalprogramspythonpython37libsite-packages (0.2.11)
>>> import pyaudio Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'pyaudio'
Why can’t Python find the installed module?
(I am using Python 3.7.)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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:Python3.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). pythonandpipcommands 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:Python2.7
C:Python3.6
C:Python3.7
and your PATH environment contains the following directories:
C:Python2.7
C:Python3.6Scripts
then, see the following output:
C:>python -V
Python 2.7.16
C:>pip -V
pip 19.1.1 from c:python3.6libsite-packagespip (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:Python3.7-64python.exe *
-3.7-32 C:Python3.7-32python.exe
-3.6-64 C:Python3.6-64python.exe
-2.7-64 C:Python2.7-64python.exe
-2.7-32 C:Python2.7-32python.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
pycommand 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 commandpython test.py, uses a different Python version than the commandtest.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.
Method 2
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0