0

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

Tauseef_Ahmed
  • 343
  • 3
  • 8
  • 18
  • 1
    as far as I can tell `forecast` is a variable which is dependent on other variables, not a function which is what you want to be importing, also talk about circular dependency, each file is importing something from the other one according to you, that's a big no no – gold_cy Sep 19 '17 at 19:22
  • 1
    `speech.py` includes `weather.py`. `weather.py` includes `speech.py`. Then, `speech.py` includes `weather.py`. Okay, the 5th line in the latter includes `speech.py`, which includes `weather.py`, which includes `speech.py`... – ForceBru Sep 19 '17 at 19:24
  • A big mistake is return statement outside of a function! – Kotauskas Sep 19 '17 at 19:26
  • 1
    All the circular stuff aside, you need to do more than import the file; you must instantiate an object from the imported module to access any variables. Also, your program could not possibly work with this code. There are too many errors, like `city_ID = sentence.split()`, which will produce `TypeError: expected a string or other character buffer object` –  Sep 19 '17 at 19:30
  • Even if your pseudocode works for this: `city_to_find = city_ID[-1] `, it would produce just "York" and your "if ... == "New York" would not evaluate as true. –  Sep 19 '17 at 19:39
  • https://stackoverflow.com/questions/419163/what-does-if-name-main-do – matias elgart Sep 19 '17 at 19:45
  • It worked. Thank you all for response – Tauseef_Ahmed Sep 20 '17 at 12:28

2 Answers2

1

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.

kip
  • 1,120
  • 7
  • 11
0

define a function in weather.py and call it from speech.py

forecast = weather.forecast(city_to_find)
S_Ymln
  • 421
  • 3
  • 14