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

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

问题描述

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

C:Usershp>pip install pyaudio要求已经满足: pyaudio in c:usershpappdatalocalprogramspythonpython37libsite-packages (0.2.11)

<预><代码>>>>导入pyaudio回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.ModuleNotFoundError:没有名为pyaudio"的模块

为什么Python找不到安装的模块?

(我使用的是 Python 3.7.)

解决方案

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

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

那么,假设您有以下 Python 版本:

C:Python2.7C:Python3.6C:Python3.7

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

C:Python2.7C:Python3.6Scripts

然后,看到以下输出:

C:>python -V蟒蛇 2.7.16C:>pip -V来自 c:python3.6libsite-packagespip (python 3.6) 的 pip 19.1.1C:>py -V蟒蛇 3.7.3

因此,当运行 pip 时,这些包可能安装在另一个 Python 版本中,然后是您在运行 python 时获得的版本.

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

C:>py -0p由 py Launcher for Windows 找到的已安装 Python-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

通用解决方案(适用于 Windows)

最好不要依赖您的系统路径.使用 py 启动器来选择你想要的版本.要运行与您要使用的 Python 版本相对应的 pip 模块,请将 pip 作为模块而不是可执行文件启动.所以,而不是:

pip install 

运行:

py -3.6 -m pip install 

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

py -3.6 -m pip freeze

一些补充说明

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

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.)

解决方案

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).
  • 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: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 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天全站免登陆