Göran Kempe Göran Kempe - 23 days ago 6
Linux Question

Cant find module even though its installed and can run

I've looked into Python's speech recognition and it's working great so far.
It offers a wide range of software, but I'll be using

CMUSphinx
, since it works offline.
CMUSphinx
is installed and working correctly when started through their own program
cmupshinx_continous
. But when trying to write my own Python-script, it cannot find the module
speech_recognition
. This is odd because when I run:

python -m speech_recognition


it works perfectly fine. But when I start my script:

import speech_recognition as sr

# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)

# recognize speech using Sphinx
try:
print("Sphinx thinks you said " + r.recognize_sphinx(audio))
except sr.UnknownValueError:
print("Sphinx could not understand audio")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))


I get the error message that the module:
speech_recognition
cannot be found.
When using

pip install SpeechRecognition


I first used
sudo pip install
but when it didn't work I thought it might have been because I installed it in root. So I uninstalled it and used
pip install --user SpeechRecognition
instead to no avail.
I also tried adding something with
PYTHONPATH
, but I have no idea what I tried to do.

So right now I'm very stuck. Please keep in mind that I'm incredibly new to both Linux and Python.

Any ideas on what I can try?

Answer

Almost always when you can access a module from one point but not another (think of script/program, terminal/editor, ...) you have multiple python versions and only installed the module in one of them.

In your case IDLE uses a different python version than you on the terminal. You have multiple possibilities:

  • configure IDLE to use the version you use on the terminal
  • use the version that IDLE uses (and install all modules again)
  • don't use IDLE to run your scripts, instead use python myscript.py

You mentioned that IDLE is python3, normally in the terminal python is python2. You can try python --version to show which version of python you use. python3 might be the python you use in IDLE, the pip would probably be pip3.

Because the library does support python3 I would advise to use python3 instead of python2.

IDLE unfortunately isn't that great of an editor, I recommend something else like notepad++, pycharm, vim or emacs.