Question

I've looked through the questions and I couldn't seem to solve the issue. Here's my file structures: Test.py imports yahoo_weather and from it hopes to use the class with functions within the class YWeather.

test/
    test.py
        yahoo_weather/
            __init__.py
            codes.csv
            yahoo_weather.py

Here is the code in yahoo_weather.py

import urllib.request
import xml.etree.ElementTree as etree
import csv
import os


class YWeather(object):
    def __init__(self):
        self.codes = self.readDict('codes.csv')

    def translate_code(code):
        return self.codes[code]

    def weather_data(self, woeid, unit):
        if unit != 'c' or unit !='f':
            print('Weather unit: ' + unit + ' is not a valid unit type')
            print('Weather unit must either be "c" or "f" for celcius or farenheit')
            print('Assuming Farenheit')

        url = 'http://weather.yahooapis.com/forecastrss?w=' + str(woeid) + '&u=' + unit
        #print(url)
        root = self.returnroot(url)
        data = dict()
        #Pull any useful information out of the xml
        try:
            data['language'] = root[0][3].text
            data['location'] = root[0][6].attrib
            data['units'] = root[0][7].attrib
            data['day0_wind'] = root[0][8].attrib
            data['day0_atmosphere'] = root[0][9].attrib
            data['day0_astronomy'] = root[0][10].attrib

            data['location_lat'] = root[0][12][1].text
            data['location_long'] = root[0][12][2].text
            data['yahoo_link'] = root[0][12][3].text
            data['pubDate'] = root[0][12][4].text
            data['day0_condition'] = root[0][12][5].attrib
            #Get forecast data, today being day0
            for i in range(0, 4):
                data['day'+str(i)+'_forecast'] = root[0][12][i+7].attrib
        except IndexError:
            print("WOEID: " + str(woeid) + " Is not a valid WOEID")
        return data

    def readDict(self, file):
        h = []
        with open(file, 'r') as f:
            reader = csv.reader(f)
            for row in reader:
                if row != []:
                    h.append(row)
        return dict(h)

    def returnroot(self, url):
        try:
            xmltext = (urllib.request.urlretrieve(url))
        except:
            print('Internet connection or WOEID is not valid')
        root = (etree.parse(xmltext[0])).getroot()
        global c
        c = root
        return root

In test.py:

import yahoo_weather
yw = yahoo_weather.YWeather()
data = yw.weather_data(6, 'c')

When I run test.py I get this error:

Traceback (most recent call last):
  File "/home/david/documents/test/test.py", line 2, in <module>
    yw = yahoo_weather.YWeather()
AttributeError: 'module' object has no attribute 'YWeather'

The __init__.py file is empty.

Thank you for reading!

Was it helpful?

Solution

You have a yahoo_weather package, which contains a yahoo_weather module, which contains your class. Import the module:

from yahoo_weather import yahoo_weather

after which you can use yahoo_weather.YWeather().

Alternatively, add a line:

from yahoo_weather import YWeather

in the __init__.py file in the yahoo_weather/ directory, then use your posted code as is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top