This kind of error throwing AttributeError: __enter__

0 votes

Traceback (most recent call last):
  File "c:/Users/User/Downloads/pyth/pm.py", line 49, in <module>
    takecommand()
  File "c:/Users/User/Downloads/pyth/pm.py", line 31, in takecommand
    with sr.Microphone as source:
AttributeError: __enter_
_

Plzzz Help to solve this error

Jul 5 in Python by APT
• 120 points
161 views

Hii  APT,

 Can you please provide more Information or paste your snippet here.

2 answers to this question.

0 votes

Hi, @APT,

You need to implement __enter__ in your class and return self in it. Could you please provide your code snippet so that we can dig further with your query. 

answered Jul 6 by Gitika
• 32,810 points
import pyttsx3

import speech_recognition as sr

import pyaudio

import datetime

import wikipedia

import webbrowser

engine = pyttsx3.init('sapi5')

voices = engine.getProperty('voices')

# print(voices[0].id)

engine.setProperty('voices', voices[0].id)

def speak(audio):

    engine.say(audio)

    engine.runAndWait()

def wishMe():

    hour =int(datetime.datetime.now().hour)

    if hour>=0 and hour<12:

        speak("Good Morning!")

    elif hour<12 and hour>18:

        speak("Good evening!")

    else:

        speak("Good afternoon!")

speak("lokesh, I am Jarvis, your assistant, I am ready, how may I help you?")

def takecommand():

    #it takes microphone input from the user and returns string output

    r = sr.Recognizer()

    with sr.Microphone as source:

        print("listening....")

        r.pause_threshold = 1

        audio = r.listen(source)

        try:

            print("recognising......")

            query = r.recognize_google(audio, language='en-in')

            print(f"user said: {query}/n")

        except Exception as e:

    # print(e)

            print("say that again please......")

            return "none"

            return query

if __name__ == '__main__':

    wishMe()

    while True:

        query = takecommand().lower()

        #logic on executive tasks based on queries

        if "wikipedia" in query:

            speak('searching wikipedia.......')

            query = query.replace("wikipedia","")

            results = wikipedia.summary(query, sentences=2)

            speak("according to wikipedia")

            print(results)

            speak(results)

        elif "open youtube" in query:

            webbrowser.open("youtube.com")

the above code when run shows an error:

Traceback (most recent call last):

  File "c:\Users\sonu\Desktop\New folder\jarvis.py", line 54, in <module>

    query = takecommand().lower()

  File "c:\Users\sonu\Desktop\New folder\jarvis.py", line 35, in takecommand

    with sr.Microphone as source:

AttributeError: __enter__

please tell me its solution..........

+1 vote

I had recently found its solution and the solution is:

in case of this:

with sr.Microphone as source:

you should write this:

with sr.Microphone() as source:

the thing to do is just you have to add () after sr.microphone.

hope it works......

answered 3 days ago by lokesh
Hi @lokesh, thank you for your contribution to the Edureka Community.

Register/Sign up on the community to gain points for further contributions. You may ask questions, answer, comment, upvote, and downvote an answer. Each of these would fetch you points and you could be among the top contributors and win exciting merchandise from Edureka.

Cheers!

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

Getting this error Error sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type

Found out that the problem was that ...READ MORE

answered Oct 1, 2018 in Python by Priyaj
• 57,550 points
4,265 views
0 votes
1 answer

Using Python what kind of applications can be built?

http://en.wikipedia.org/wiki/List_of_Python_software  follow the link and You will see ...READ MORE

answered Nov 14, 2018 in Python by Theodor
• 740 points
74 views
+1 vote
6 answers
0 votes
1 answer

Getting AttributeError: 'module' object (scipy) has no attribute 'misc' in Python. How to solve this?

>>> import scipy >>> scipy.misc Traceback (most recent call ...READ MORE

answered Dec 24, 2018 in Python by Nymeria
• 3,520 points
3,589 views
0 votes
1 answer

Python Pandas error: AttributeError: 'DataFrame' object has no attribute 'rows'

There is no attribute called “rows”. The ...READ MORE

answered Mar 28, 2019 in Python by Shri
36,065 views
0 votes
3 answers

TypeError: Index(...) must be called with a collection of some kind, 'Email' was passed

It is not working because your code ...READ MORE

answered May 12 in Python by anonymous

edited May 13 by Gitika 18,374 views
0 votes
1 answer
0 votes
1 answer

Python error "AttributeError: incompatible shape for a non-contiguous array"

It is not always possible to change ...READ MORE

answered May 29, 2019 in Python by Imran
282 views
0 votes
1 answer

Python error saying "AttributeError: can't set attribute"

change your code to the following: @x.setter def x(self, ...READ MORE

answered May 30, 2019 in Python by Imran
8,315 views