【问题标题】:python can't find module speech_recognitionpython找不到模块speech_recognition
【发布时间】:2018-04-11 12:34:59
【问题描述】:

所以我安装了 speech_recognition 库,但是当我尝试导入它时,它说找不到它。这是我正在使用的代码。

import speech_recognition as sr

r = sr.Recognizer()
with sr.Microphone() as source:
    audio = r.listen(source)

try:
    print("You said: " + r.recognize(audio))
except:
    print("i didn't quite get that")

但是当我运行它时它会显示这个。

Traceback(最近一次调用最后一次):文件“C:\python 项目\test.py",第 1 行,在 import speech_recognition as sr ModuleNotFoundError: No module named 'speech_recognition'

我还测试了它:“python -m speech_recognition” 而且效果很好。

并且库安装在:Python36\Lib\site-packages\speech_recognition

【问题讨论】:

  • 看起来该模块已安装在 Python 3 中。也许您的脚本是通过 Python 2 运行的?
  • 我通过 IDLE 运行它并且发生了同样的问题

标签: python speech-recognition


【解决方案1】:

你写python test.py或者

pathToYourPython36Executable/python.exe test.py 进入你的命令行?

如果是这样,您可能使用的 IDLE 版本与您安装模块的位置不同。

【讨论】:

  • 所以当我在 cmd 中输入“python test.py”时似乎可以工作,谢谢。
【解决方案2】:

尝试安装SpeechRecognition 而不是speech_recognition

python3 -m SpeechRecognition

【讨论】:

    【解决方案3】:

    你自己的Alexa?呵呵。 在 mac bigsur OS 上,这些都不适合我 sudo pip3 install --升级语音识别 sudo pip3 安装语音识别 pip3 安装语音识别

    我修复了系统检查我安装 pkg 的位置的问题

    python3 --version
    Python 3.8.2
    

    检查路径:

    python3 -c "import sys; print(sys.path)"
    ['', '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python38.zip', '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8', '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/lib-dynload', '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages']
    

    检查 pkg 在哪里:

    python3 -m SpeechRecognition
    /Applications/Xcode.app/Contents/Developer/usr/bin/python3: No module named SpeechRecognition
    

    使用 pop install 我明白了,不同的路径

    sudo pip3 install --upgrade speechrecognition
    Requirement already satisfied: speechrecognition in /Users/paul/Library/Python/3.7/lib/python/site-packages (3.8.1)
    

    所以我手动将 pkgs 复制到我需要的路径:

    sudo -H pip3 install --upgrade speechrecognition
    Requirement already satisfied: speechrecognition in /Library/Python/3.7/site-packages (3.8.1)
    paul@MacBook-Air-de-Paul git % ls -lstr /Library/Python/3.7/site-packages
    total 0
    0 drwxr-xr-x    9 root  wheel   288 Jan 30 00:12 Pillow-8.1.0.dist-info
    0 drwxr-xr-x  103 root  wheel  3296 Jan 30 00:12 PIL
    0 drwxr-xr-x    9 root  wheel   288 Jan 30 00:15 pip-21.0.1.dist-info
    0 drwxr-xr-x    7 root  wheel   224 Jan 30 00:15 pip
    0 drwxr-xr-x   10 root  wheel   320 Mar  3 11:56 speech_recognition
    0 drwxr-xr-x   12 root  wheel   384 Mar  3 11:56 SpeechRecognition-3.8.1.dist-info
    
    sudo cp -r /Library/Python/3.7/site-packages/speech_recognition /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages/
    
    sudo cp -r /Library/Python/3.7/site-packages/SpeechRecognition-3.8.1.dist-info /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages/
    

    【讨论】:

      猜你喜欢
      • 2019-04-10
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 2015-03-12
      相关资源
      最近更新 更多