Reputation: 545
I have 2 python files. One is speech.py and other is weather.py What I want to do is, type a question (i.e How is weather in New York) and then pass the city name to weather.py file. Which would find the weather for that city and return the result back to speech.py and result will be printed by speech.py (which is kind of my main file). Both files give error saying that the other file has no attribute named forecast and city_to_find respectively. But I can clearly see the variables in both files.
speech.py
from weather import forecast
if i['tag'] == 'weather':
sentence = 'How is weather in New York'
print('Okay, Let me search for weather')
city_ID = sentence.split()
city_to_find = city_ID[-1]
engine.say(forecast)
weather.py
import urllib.request
from bs4 import BeautifulSoup
import urllib.parse
from urllib.parse import urljoin
from speech import city_to_find
city= city_to_find
weather_page = 'https://weather.com/en-IN/weather/today/l/'
NY ='USNY0996:1:US'
if city == 'New York':
weather_page+=NY
#print(weather_page)
weather = urllib.request.urlopen(weather_page)
weather_data = BeautifulSoup(weather, 'html.parser')
temperature = weather_data.find('div', attrs={'class': 'today-daypart-temp'})
temp = temperature.text.strip() # strip() is used to remove starting and trailing
weather_condition = weather_data.find('span', attrs={'class': 'today-daypart-wxphrase'})
update = weather_condition.text.strip() # strip() is used to remove starting and trailing
forecast = 'Right now in '+city+' it is '+temp+' and '+update
return forecast
I also tried importing the entire files into each other (i.e import weather and import speech respectively). But No luck.
Can anyone help me figure out my mistake?
Note: Both programs are working fine separately
Upvotes: 0
Views: 93
Reputation: 421
define a function in weather.py and call it from speech.py
forecast = weather.forecast(city_to_find)
Upvotes: 0
Reputation: 1140
Try
weather.py
import urllib.request
from bs4 import BeautifulSoup
import urllib.parse
from urllib.parse import urljoin
def forecast(city_to_find):
city = city_to_find
weather_page = 'https://weather.com/en-IN/weather/today/l/'
NY ='USNY0996:1:US'
if city == 'New York':
weather_page+=NY
#print(weather_page)
weather = urllib.request.urlopen(weather_page)
weather_data = BeautifulSoup(weather, 'html.parser')
temperature = weather_data.find('div', attrs={'class': 'today-daypart-temp'})
temp = temperature.text.strip() # strip() is used to remove starting and trailing
weather_condition = weather_data.find('span', attrs={'class': 'today-daypart-wxphrase'})
update = weather_condition.text.strip() # strip() is used to remove starting and trailing
forecast = 'Right now in '+city+' it is '+temp+' and '+update
return forecast
And you can call the function
speech.py
from weather import forecast
engine.say(forecast(city_to_find))
I modify the structure, I send the city to the function, not import this data from speech.py to weather.py and the function return the result of the process in speech.py.
Upvotes: 1