-2

I'm using python's Speach Recognition and my code is giving me an AttributeError:

import os
import pyaudio
import speech_recognition as sr

def excel():
        os.system("start excel.exe")

def internet():
        os.system("start chrome.exe")

def media():
        os.system("start wmplayer.exe")

def mainfunction(source):
    audio = r.listen(source)
    user = r.recognize_google(audio)
    print(user)
    if user == "Excel":
        excel()
    elif user == "Internet":
        internet()
    elif user == "music":
        media()

if __name__ == "__main__":
    r = sr.Recognizer()    #this is the line that fails
    with sr.Microphone() as source:
        while 1:
            mainfunction(source)

the error message is:

AttributeError: 'module' object has no attribute 'Recognizer'
(test-dev-pro)➜  ~  python speech_recognition.py
Traceback (most recent call last):
  File "speech_recognition.py", line 26, in <module>
    r = sr.Recognizer()
AttributeError: 'module' object has no attribute 'Recognizer'

but Recognizer is used in all the examples I have looked at so I don't understand why I am getting this error.

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
Akash Wankhede
  • 618
  • 6
  • 15
  • Show your code, What exactly you are trying to do – AlokThakur Feb 23 '16 at 11:42
  • And format your code as explained in the edit view. You can edit your question by clicking an the **edit** link below your question. Also please read the [tour](http://stackoverflow.com/tour). – Martin Zabel Feb 23 '16 at 12:01
  • I am trying to do a simple speech recognition python program. i am new to python so please consider my mistakes. Thanks in Advance. – Akash Wankhede Feb 23 '16 at 14:18
  • did you call your file `speech_recognition` and try to import a module called `speech_recognition` from a library? if that is the case you need to rename your file because the import will just import it's own file. – Tadhg McDonald-Jensen Feb 23 '16 at 14:45
  • to test my theory add `print ("TEST")` right below `import speech_recognition as sr` and if it prints twice then you are importing your own file, which I can explain in an answer. – Tadhg McDonald-Jensen Feb 23 '16 at 14:48
  • I renamed my file as test123.py , even though i am getting the same error.please help someone – Akash Wankhede Feb 24 '16 at 06:52
  • You would want to look for and delete speach_recognition.pyc located in either the same folder or a subfolder called __pycache__. – Tadhg McDonald-Jensen Feb 24 '16 at 19:34
  • Yeah..now it works....Thank uu sooo much.... – Akash Wankhede Feb 25 '16 at 06:42
  • Anyone who finds this question, the generic solution is answered inL [Importing installed package from script raises "AttributeError: module has no attribute" or "ImportError: cannot import name"](http://stackoverflow.com/questions/36250353/importing-installed-package-from-script-raises-attributeerror-module-has-no-at) – Tadhg McDonald-Jensen Jun 15 '16 at 20:23

3 Answers3

4

You are having the same issue as this: Importing installed package from script raises "AttributeError: module has no attribute" or "ImportError: cannot import name"

by naming your file speach_recognition you end up importing your own file instead of the library file. To fix this just change the name of your file and you'll be good to go! :)

Edit: be sure to look at the comments on that question as well for info cleaning up the .pyc file as well.

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
0

I think your source file name is speech_recognition.py, This is conflict the methods with in the speech recognition package. So just change the name of the file. If occurs another error like

AttributeError: 'Recognizer' object has no attribute 'recognize'

then just change the recognize() method as google_recognize()

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
0

People on Windows may face problem with pyaudio I faced the same

pip install pipwin

After installing pipwin

pipwin install pyaudio

It will help you

Mohnish
  • 1,010
  • 1
  • 12
  • 20
senthil
  • 7
  • 1
  • 3