Last modified: Apr 07, 2025 By Alexander Williams
Install SpeechRecognition in Python Step by Step
SpeechRecognition is a Python library for performing speech recognition. It supports multiple engines and APIs. This guide will help you install it easily.
Table Of Contents
Prerequisites
Before installing SpeechRecognition, ensure you have Python installed. You can check by running:
python --version
If you get a ModuleNotFoundError, refer to this guide on solving ModuleNotFoundError.
Install SpeechRecognition
Use pip
to install SpeechRecognition. Run the following command in your terminal:
pip install SpeechRecognition
This will download and install the latest version. Wait for the process to complete.
Verify Installation
To ensure SpeechRecognition is installed, run Python and import it:
import speech_recognition as sr
print(sr.__version__)
If no errors appear, the installation was successful.
Basic Usage Example
Here’s a simple script to recognize speech from a microphone:
# Import the library
import speech_recognition as sr
# Initialize recognizer
recognizer = sr.Recognizer()
# Use microphone as source
with sr.Microphone() as source:
print("Speak something...")
audio = recognizer.listen(source)
# Recognize speech using Google Speech Recognition
try:
text = recognizer.recognize_google(audio)
print("You said: " + text)
except Exception as e:
print("Error: " + str(e))
This code listens to your voice and prints the recognized text.
Handling Errors
If you encounter errors, ensure your microphone works. Also, check your internet connection if using online APIs.
For ModuleNotFoundError, revisit the installation steps or check the linked guide.
Conclusion
Installing SpeechRecognition in Python is simple. Follow these steps to start building voice-enabled apps. Experiment with different APIs for better results.