Hi, I have followed the tutorial from Programming Hero on Youtube, and I was facing some really serious issues, which says that I am using the variable command, before I have declare it, but it clearly shows that I have declared it well before, folliwing the tutorial. I have copied line by line of the code, but I face 3 issues, 2 of them are about the declaration of the variable, and one of them is about the last line where I have initiated the “run_alexa()” function. This is the link for the tutorial, can you please find what actually happened. I have looked at stack overflow, where a person has the same issue, and looked at the 3 answers, and none of them worked for me, instead, it just made the program worse. Can you please have a look at the tutorial, and maybe copy the code by clicking the link on the description? Thank you.
That’s a very bad idea because it swallows any exception that might be occurring.
You should catch only those exceptions that you’re going to handle.
Remove that exception handler for the time being (until you know what you want to catch) and you might get something like this:
Traceback (most recent call last):
File "C:\Python311\Lib\site-packages\speech_recognition\__init__.py", line 108, in get_pyaudio
import pyaudio
ModuleNotFoundError: No module named 'pyaudio'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Projects\test\main.py", line 62, in <module>
run_alexa()
File "C:\Projects\test\main.py", line 37, in run_alexa
command = take_command()
^^^^^^^^^^^^^^
File "C:\Projects\test\main.py", line 23, in take_command
with sr.Microphone() as source:
^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\speech_recognition\__init__.py", line 80, in __init__
self.pyaudio_module = self.get_pyaudio()
^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\speech_recognition\__init__.py", line 110, in get_pyaudio
raise AttributeError("Could not find PyAudio; check installation")
AttributeError: Could not find PyAudio; check installation
Ah, OK, it also requires PyAudio.
As that wasn’t installed, sr.Microphone() raised an exception which was caught and then discarded.
It didn’t get as far as assigning to command, therefore UnboundLocalError when it tried to return the value of command.
And that’s why bare excepts are bad. They hide bugs.
Thank you, I am going to try and let you know. I am going to remove the try and except exception and then run again. Also, I will install the library Pyaudio, and then run it again. Let me know if this is the correct procedure to remove the issue. Thank you.
hey so from the look of things, first things first I checked what @MRAB was trying to raise and it makes sense. So this is the problem the unbound local error you are facing, simply means you are trying to return a variable that has not been initialized or defined. Before you remove the try ... except block initialize the command variable just above the block so that it can store whatever result it gets from the operation… hoping it will take care of your issue.
def take_command():
command = "" # initialize the variable command
try:
with sr.Microphone() as source:
print('listening...')
voice = listener.listen(source)
command = listener.recognize_google(voice)
I have done that, but it keeps repeating the else statement, without listenig to my voice command, it prints listening…, but never listens, it repeat the else statement for 10 times, and it stops, freezing the program and the code editor.
hey danish… ummh so yeah at least the first step is now working no errors… so have you seen this program working with others… are they able to communicate with the assistant… if so I can now delve deeper to why its not listening… kindly confirm if its working as intended with other people
@kyle I have tried to change the position of the “command” variable, to other lines, in the try statement, but the output is the same, the program doesn`t allow the person to speak, and it continiously, repeat the else statement.
Hey there danish I’ve been a bit engaged sorry for the late reply… from what you are saying, don’t change the command variable position that stores the return query to be used by the engine to have something to say… now there are only two options seems the program is working fine but it can’t listen to you… so check your microphone and the second part hope you installed the pyaudio library so that it can listen to voices… if it is possible please run the program in a virtual environment
Hey danish do you want me to use “but it works on my machine” cliche lol… nywy for real it’s working on my machine and even added a condition to stop alexa.
Aha so lets do this…first of all did you run it on a virtual environment this time around? And am hoping you know virtual environments… if you run the program does it trigger the microphone on the taskbar, you should see something like this
…
does the print stmt listening show on the console when you run?
finally paste your installed libraries so that we see the packages, run pip freeze on the console… either screenshot or pip freeze > requirements.txt to write the result to a text file then we pick up from there
You are the second person, who says that is working on their machine . I don’t know what is wrong. I run it through VS code environment, on my Windows 10 PC. I can see the microphone activating and disactivating, as well as, I can see the listening… text printed on the screen, but if a user doesn’t reply or talk with the assistant for 2 or more seconds, then it shows the error, abd sometimes, it sjpws as soon as I press the run button.
Here is the screenshot of the ‘pip freeze’ command in cmd. I am sorry if there are soo many libraries. I had to include multiple screenshots, as it says that I am a new user, so I can’t upload more than one file.