python如果需要使用到pyaudio的功能,是需要手动把pyaudio模块导入进来的,我现在使用的版本是Python 3.8.2 win32,当你想直接使用
pip install pyaudio
导入pyaudio模块的时候,你会发现一堆下面这样的错误:
ERROR: Command errored out with exit status 1:
command: 'D:\software\Python38\python.exe' -u -c 'import io, os, sys, setup
tools, tokenize; sys.argv[0] = '"'"'C:\\Users\\xxx\\AppData\\Local\\Temp\\p
ip-install-08c89m1g\\pyaudio_63ed7884d1264496bca792f0a55a3efd\\setup.py'"'"'; __
file__='"'"'C:\\Users\\xxx\\AppData\\Local\\Temp\\pip-install-08c89m1g\\pya
udio_63ed7884d1264496bca792f0a55a3efd\\setup.py'"'"';f = getattr(tokenize, '"'"'
open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'fro
m setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"',
'"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install
--record 'C:\Users\xxx\AppData\Local\Temp\pip-record-p1w3bksk\install-recor
d.txt' --single-version-externally-managed --compile --install-headers 'D:\softw
are\Python38\Include\pyaudio'
cwd: C:\Users\xxx\AppData\Local\Temp\pip-install-08c89m1g\pyaudio_
63ed7884d1264496bca792f0a55a3efd\
Complete output (9 lines):
running install
running build
running build_py
creating build
creating build\lib.win-amd64-3.8
copying src\pyaudio.py -> build\lib.win-amd64-3.8
running build_ext
building '_portaudio' extension
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Micros
oft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
----------------------------------------
ERROR: Command errored out with exit status 1: 'D:\software\Python38\python.exe'
-u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\
xxx\\AppData\\Local\\Temp\\pip-install-08c89m1g\\pyaudio_63ed7884d1264496bc
a792f0a55a3efd\\setup.py'"'"'; __file__='"'"'C:\\Users\\xxx\\AppData\\Local
\\Temp\\pip-install-08c89m1g\\pyaudio_63ed7884d1264496bca792f0a55a3efd\\setup.py
'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__
file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code =
f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __f
ile__, '"'"'exec'"'"'))' install --record 'C:\Users\xxx\AppData\Local\Temp\
pip-record-p1w3bksk\install-record.txt' --single-version-externally-managed --co
mpile --install-headers 'D:\software\Python38\Include\pyaudio' Check the logs fo
r full command output.
这个错误表明你是无法直接导入pyaudio模块的,我们可以通过命令
python
可以查看到我们python所在的版本及环境,然后通过https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio 这个地址去找到我们python版本及环境所对应的pyaudio,我对应的版本是PyAudio-0.2.11-cp38-cp38-win32.whl,下载下来后进入对应的文件夹,通过cmd命令行的方式进入后,如果直接使用以下命令
pip install PyAudio-0.2.11-cp38-cp38-win32.whl
进行安装,你可能会得到以下错误
ERROR: PyAudio-0.2.11-cp38-cp38m-win32.whl is not a supported wheel on this platform.
当你在网上去查询原因的时候,很多地方都会让你执行下面类似的命令来查看
>>> import pip
>>> print(pip.pep425tags.get_supported())
或者
>>> import pip._internal.pep425tags
>>> print(pip._internal.pep425tags.get_supported())
但是你可能会得到
AttributeError: module 'pip' has no attribute 'pep425tags'
我也根据网上的介绍去D:\software\Python38\Lib\site-packages\pip\_internal路径或者整个python路径下面都没有找到pep425tags.py文件,不知道我的这个版本这个东西跑哪里去了,还好找到了另外一种方式来看我们版本可以执行的whl格式的匹配版本,如下命令
python -m pip debug --verbose
通过该命令可以看到如下信息
pip version: pip 21.1.1 from D:\software\Python38\lib\site-packages\pip (python3.8)
sys.version: 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
sys.executable: D:\software\Python38\python.exe
sys.getdefaultencoding: utf-8
sys.getfilesystemencoding: utf-8
locale.getpreferredencoding: cp936
sys.platform: win32
sys.implementation:
name: cpython
'cert' config value: Not specified
REQUESTS_CA_BUNDLE: None
CURL_CA_BUNDLE: None
pip._vendor.certifi.where(): D:\software\Python38\lib\site-packages\pip\_vendor
certifi\cacert.pem
pip._vendor.DEBUNDLED: False
vendored library versions:
appdirs==1.4.4
CacheControl==0.12.6
colorama==0.4.4
distlib==0.3.1
distro==1.5.0 (Unable to locate actual module version, using vendor.txt speci
ied version)
html5lib==1.1
msgpack==1.0.2 (Unable to locate actual module version, using vendor.txt spec
fied version)
packaging==20.9
pep517==0.10.0
progress==1.5
pyparsing==2.4.7
requests==2.25.1
certifi==2020.12.05
chardet==4.0.0
idna==3.1
urllib3==1.26.4
resolvelib==0.7.0
setuptools==44.0.0 (Unable to locate actual module version, using vendor.txt
pecified version)
six==1.15.0
tenacity==7.0.0 (Unable to locate actual module version, using vendor.txt spe
ified version)
toml==0.10.2
webencodings==0.5.1 (Unable to locate actual module version, using vendor.txt
specified version)
Compatible tags: 30
cp38-cp38-win_amd64
cp38-abi3-win_amd64
cp38-none-win_amd64
cp37-abi3-win_amd64
cp36-abi3-win_amd64
cp35-abi3-win_amd64
cp34-abi3-win_amd64
cp33-abi3-win_amd64
cp32-abi3-win_amd64
py38-none-win_amd64
py3-none-win_amd64
py37-none-win_amd64
py36-none-win_amd64
py35-none-win_amd64
py34-none-win_amd64
py33-none-win_amd64
py32-none-win_amd64
py31-none-win_amd64
py30-none-win_amd64
cp38-none-any
py38-none-any
py3-none-any
py37-none-any
py36-none-any
py35-none-any
py34-none-any
py33-none-any
py32-none-any
py31-none-any
py30-none-any
在后面我们看到了py38-none-any的模式,我们只需要把我们下载下来的PyAudio-0.2.11-cp38-cp38-win32.whl修改为PyAudio-0.2.11-cp38-none-any.whl,然后通过命令
pip install PyAudio-0.2.11-cp38-none-any.whl
就可以成功导入了,这样我们就可以正常使用pyaudio模块了。