使用speech_recognition模块遇aifc模块缺失错误求助
排查speech_recognition模块导入时的aifc缺失问题
问题描述
使用speech_recognition模块开发语音识别功能,运行代码时触发报错ModuleNotFoundError: No module named 'aifc',已尝试重新安装该库及Python,但问题仍未解决。
相关代码
import speech_recognition as sr r = sr.Recognizer() with sr.Microphone() as source: print("Talk") audio_text = r.listen(source) print("Time over, thanks") try: print("Text: "+r.recognize_google(audio_text)) except: print("Sorry, I did not get that")
报错信息
Traceback (most recent call last): File "c:\Users\LEGION\Desktop\sp.py", line 1, in <module> import speech_recognition as sr File "C:\Users\LEGION\AppData\Roaming\Python\Python313\site-packages\speech_recognition\__init__.py", line 7, in <module> import aifc ModuleNotFoundError: No module named 'aifc'
解决方案
- 降级Python版本到3.12或更低:
aifc是Python标准库模块,但在Python3.13中被移除,降级到3.12及以下版本后,标准库会自带该模块,可直接解决问题。 - 修改speech_recognition的导入代码:找到对应路径
C:\Users\LEGION\AppData\Roaming\Python\Python313\site-packages\speech_recognition\__init__.py,将第7行的import aifc替换为以下代码:try: import aifc except ImportError: from wave import aifc - 安装第三方兼容包:执行命令
pip install aifc安装第三方替代包,填补Python3.13中缺失的aifc模块(注意该包非官方,需确认安全性后使用)。
内容的提问来源于stack exchange,提问作者Budithi Sai Sudhanshu
相关产品推荐
相关产品推荐

