I've installed the module pyaudio using pip
. However, when I try to import it, Python says the module is not found:
C:\Users\hp>pip install pyaudio
Requirement already satisfied: pyaudio in c:\users\hp\appdata\local\programs\python\python37\lib\site-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?
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:
python.exe
, is installed in <PYTHON_INSTALL_DIR>
(e.g. C:\Python\3.7
).pip
, pylint
, virtualenv
, PyCrust
, etc., are installed in <PYTHON_INSTALL_DIR>\Scripts
.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
py
command will always be available, even if you did not add any Python installation to your PATH.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
.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.