M.B.
M.B.

Reputation: 1

ImportError: cannot import name 'UserRequest'

I am developing a chatbot and I can't make it print a custom message before printing the search result. My custom messages are in difrent folder than the main script. The terminal prints "ImportError: cannot import name 'UserRequest'".

Here is the main script:

from google import search import requests from bs4 import BeautifulSoup from RespondsToSearchQueries import UserRequest

def searchResults(query,start_num): """takes an input query, and returns number of websites and descriptions"""

for url in search(query, tld='com', lang='en', num = start_num,stop=5, pause = 3.0):
#for each URL found in google search
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
    metas = soup.find_all('meta')
    metasList = list(meta.attrs['content'] for meta in metas if 'name' in meta.attrs and meta.attrs['name'] == 'description')
    #retrieve description of URL
    desc = ''.join(metasList)
    #convert list to string 

    url_and_desc = url,"\n",desc,"\n"
    yield url_and_desc

query = input("What would you like to look for?") responses = search_results(query,1) for url_and_desc in responses: print(UserRequest(query)) print(url_and_desc)

and here is the script I am trying to import from:

from google_rewrite import *

def UserRequest(text):

text = text.lower()
for word in text:
    if "boneless pizza" in text:
        response = ""
        search_results(word)
    elif "pizza" in text:
        response = ""
    elif "weather" in text:
        response = ""
        #location = "Where are you right now?"
        response += weather_Lookup(location)
    elif "music" in text:
        response = ""
        response += search_results(response)
    elif "google" in text:
        reponse = ""
        response += search_results(reponse)
    elif "food" in text:
        response = ""
        response += search_results(response)
    elif "sport" or "sports" in text:
        rsponse = ""
        rsponse += search_results(response)
    elif "news" in text:
        rsponse = ""
        response += search_results(response)
    elif "sing" in text:
        response = ""
        response += search_results(response)
    #elif "joke" in text or "jokes" in text:
     #   response = "Jokes?! I know some, what to hear one?"
      #  if
      #  else:
      #  response += search_results(response)
    else:
        response = "Couldn't understand what are you looking for m8?"

return response

Upvotes: 0

Views: 330

Answers (1)

srowland
srowland

Reputation: 1705

Do you have an __init__.py in the same folder as RespondsToSearchQueries.py? This is needed to make the folder a package from which you can import. __init__.py can be empty.

It's probably worth just trying this on the command line to make sure it's visible - start in the directory of your main script, start the interpreter and just type from RespondsToSearchQueries import UserRequest. You can then tinker with the __init__.py (and other factors until it works)?

Might be helpful if you show us your directory and file structure for this app so we can help diagnose import problem.

Upvotes: 0

Related Questions