ModuleNotFoundError:没有名为"pyaudio"的模块(Windows) [英] ModuleNotFoundError: No module named 'pyaudio' (Windows)

查看:1947
本文介绍了ModuleNotFoundError:没有名为"pyaudio"的模块(Windows)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用pip安装了模块pyaudio.但是,当我尝试导入它时,Python表示找不到该模块:

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'

为什么Python无法找到已安装的模块?

Why can't Python find the installed module?

推荐答案

经常发生某人使用pip安装Python程序包,但随后似乎无法将其导入Python的情况.要了解为什么会发生这种情况,您必须知道Windows如何找到要运行的可执行文件以及Python软件的安装方式.基础知识:

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:

  • 运行命令时,Windows在环境变量PATH中搜索可执行文件.它执行找到的第一个.
  • Python解释器python.exe安装在<PYTHON_INSTALL_DIR>中(例如C:\Python\3.7).
  • Python工具,例如pippylintvirtualenvPyCrust等,已安装在<PYTHON_INSTALL_DIR>\Scripts中.
  • 用于Windows的Python启动器py.exe已安装在Windows系统目录(例如C:\Windows)中.
  • pythonpip命令使用在其安装目录中找到的模块,而不查看PATH.
  • 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.

因此,假设您具有以下Python版本:

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

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

,并且您的PATH环境包含以下目录:

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

因此,在运行pip时,可能会将软件包安装在另一个Python版本中,而不是在运行python时将获得的版本中.

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.

要查看(正确)在系统上安装了哪些版本,请运行py -0p.输出示例:

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

常规解决方案(适用于Windows)

最好的是不要依赖您的系统PATH.使用py启动器选择所需的版本.要运行与您要使用的Python版本相对应的pip模块,请将pip作为模块而不是可执行文件启动. 因此,代替:

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>

运行:

py -3.6 -m pip install <package>

要查看已为该Python版本安装了哪些Python软件包,请使用:

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

py -3.6 -m pip freeze

一些补充说明

  • 在安装过程中是否可以选择是否将Python安装添加到您的PATH中.如果已添加,则将其添加到PATH的开头,因此将首先选择最新安装的Python版本.
  • Windows系统目录应始终位于PATH中,因此即使未将任何Python安装添加到PATH中,py命令也将始终可用.
  • 如果您从Windows资源管理器中双击.py文件,或直接在命令提示符下作为命令键入文件名(例如test.py),则该操作由Windows注册表确定.该文件可能会在您的IDE中打开,或者使用Python解释器执行.在这种情况下,它可能是最新安装的Python版本.命令python test.py使用的Python版本可能不同于命令test.py.
  • 某些安装程序还包含名为python2/python3的可执行文件(不适用于Windows),pip3/pip3.7(也适用于Windows)等.这也将允许您指定要使用的版本.这些将在存在这些二进制文件且位于路径中的系统上很有用.
  • 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.

这篇关于ModuleNotFoundError:没有名为"pyaudio"的模块(Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆