Python error NameError name sr is not defined

0 votes

I am running a python script for speech-recognition:

def readAudioFile(audio_file):
    r = sr.Recognizer()
    file = sr.AudioFile(audio_file)
    with file as source:

        audio = r.record(source)
        type(audio)
    try:
        message = r.recognize_google(audio)
        print(message)
        print("Data extracted from phone conversation:\n" + message)
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")

But when I run this, I get NameError: name 'sr' is not defined  error. How to solve this?

Nov 28, 2018 in Python by digger
• 26,740 points
42,578 views

3 answers to this question.

0 votes

I guess you are using speech_recognition module and missing the import. Include this line at the beginning of your script and see if it works:

import speech_recognition as sr


Hope this helps!!

If you need to know more about Python, join Python certification course today.

Thanks!

answered Nov 28, 2018 by Omkar
• 69,220 points
0 votes
Did you import the module?
answered Jun 4, 2019 by anonymous
Hi. Which module should I import?
Are you talking about the speech_recognition module as mentioned by @Omkar?
0 votes

NameError: name 'xx' is not defined

Python knows the purposes of certain names (ex. built-in functions ). Other names are defined within the program (ex. variables). If Python encounters a name that it doesn't recognize, you'll probably get NameError: global name 'xx' is not defined error. In most cases, this error is triggered when Python sees a variable name (Global or Local) and doesn't know what it's for. These errors can happen if you forget to initialize a variable , if you misspell a variable, or if you misspell a reserved word such as "True". Before you use the global variable in your function for reading, it must be first initialized somewhere: either outside of the function or inside it.

answered Mar 19, 2020 by rahul
• 360 points
While creating a chatbot using flask its throwing an error name is not defined

app = Flask(_name_)
Hi, @There,

Could you please post your error here so that I can get to the root cause of why you are facing error and what are you trying to do? Please post your workaround.

Related Questions In Python

0 votes
1 answer

Python error "Python NameError: global name 'Thread' is not defined"

There is a built-in function with the ...READ MORE

answered May 31, 2019 in Python by Riya
7,301 views
–1 vote
1 answer

Python error saying "NameError: name 'email' is not defined"

you need to define the variable email READ MORE

answered Nov 29, 2019 in Python by Casper
• 160 points
6,836 views
0 votes
1 answer

Python Error saying "NameError: global name 'true' is not defined"

It's a very small mistake. Change true ...READ MORE

answered Jun 17, 2019 in Python by Vinayak

edited Oct 7, 2021 by Sarfaraz 16,195 views
0 votes
1 answer

Error:Python NameError: name 'include' is not defined

Hello @kartik, On the basis of your information provided ...READ MORE

answered Jul 1, 2020 in Python by Niroj
• 82,840 points
9,523 views
0 votes
2 answers

Error: Speech to Text Codec cannot decode the bytes in position

import speech_recognition as sr r = sr.Recognizer() audio ='C\Users\Desktop\audiofile1.wav' with ...READ MORE

answered Nov 28, 2018 in Python by Nabarupa Das
2,940 views
0 votes
1 answer

Need help with Python Text-to-Speech usage

Hi. Just before I begin my answer I ...READ MORE

answered Jan 16, 2019 in Python by Nymeria
• 3,560 points
1,030 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer

Python argparse error "NameError: name 'file' is not defined"

The right datatype for destination in argpasrse module ...READ MORE

answered Nov 28, 2018 in Python by Omkar
• 69,220 points
13,706 views
0 votes
1 answer

How to print a message or the error if a file is not found?

To print the message that file is not ...READ MORE

answered Jan 2, 2019 in Python by Omkar
• 69,220 points
2,868 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP