0

Recently installed PyDev for Eclipse Oxygen 3 on WIndows 10. As a new programmer I am writing basic programs as I work through a "learn to code" book. The example I am working on uses a module called "snaps", which is in "pygame". The program below works fine in the Python Shell IDE.

import snaps

temp = snaps.get_weather_temp(latitude=47.61, longitude=-122.33)

print('The temperature in Seattle is:', temp)

Output: The temperature in Seattle is: 60

But when I run this in PyDev I get:

Traceback (most recent call last):
File "C:\Users\mando\eclipse- 
workspace\BeginToCodeWithPython\Chapter4\ActualTemp.py", line 9, in <module>
temp = snaps.get_weather_temp(latitude=47.61, longitude=-122.33)
AttributeError: module 'snaps' has no attribute 'get_weather_temp'

I've confirmed the Python Path is set correctly in windows and the PyDev Interpreter is set correctly as well. I've added every lib, script folder etc. I can under project and perspective preferences. I've even added the pygame folder to my workspace. But I still am getting the errors.

I've confirmed that "get_weather_temp" is in the snaps module via the help() function, and it works in the IDE, just not in PyDev.

Any assistance would be appreciated.

1 Answers1

0

I think this may help you. If I'm not mistaken PyDev needs to be told where to look for the "modules"/"libraries" where installing it the IDE uses the default and pydev would use its own environment. (below snippet is from comments) if you have any questions just ask - its ready to go you just need to put your own api key in. (it defaults to kelvin so you'd have to convert to f or c, i didn't take the time,if you want to do that i found an example here)

also this is what the json response looks like if you are curious. the output would look like: the current temp of Seattle is 284.02

import requests
import json
# settings
key = 'put your api key here' #you can get a free api from https://home.openweathermap.org/api_keys 
#takes less then 10 seconds to make an account, no verification.
base = 'http://api.openweathermap.org/data/2.5/weather?q='
id = '&appid='
city = 'Seattle,US' # found here; https://openweathermap.org/ you could add exceptions here for failed responses or a method for the user to input their own request etc)
url = base + city + id + key #following their api structure, probably much better ways to do this lol

response = requests.get(url) #send the request to the api and brings it back
data = response.json() #saves the response as json (seemed easier they had multiple options)
json_str = json.dumps(data) #turn the response into a string
resp_dict = json.loads(json_str) #turn string into dict (i learned today to turn it into a dict it has to be a string,i couldn't figure out to print specifics from a str so i converted to a dict)
temp = resp_dict['main']['temp'] #grabs the temp from the dict
loc = resp_dict['name'] #grabs the location defined above (seattle)
print('the current temp of {} is {}'.format(loc,temp)) #(prints the current location and temp)

decided to try out the conversion - it was much easier to use the pyowm lib. the output would look like "The current temp in Seattle is 51.57" I made the variables stupid long so you could follow along.

import json
import pyowm


owm = pyowm.OWM('your key here')
location = 'Seattle' #i only made this a seperate variable so i could call it later/change it
observation = owm.weather_at_place(location) #tells the api where the location is
weather = observation.get_weather() #gets the weather of location above
temperature = weather.get_temperature('fahrenheit')
temperature_to_string = json.dumps(temperature)
temperature_string_to_dictionary = json.loads(temperature_to_string)
temperature = temperature_string_to_dictionary['temp'] 
print('The current temp in {} is {}'.format(location, temperature))
Justin
  • 1
  • 2
  • Thank you. I reviewed that thread. I had done most of what it suggested, but it's still throwing the error. I can get PyDev to import and identify pygame and print it's version. But it still won't find that attribute in the snaps module. It will find other attributes in snaps, just not get_weather_temp. – HerdingDogRescuer Apr 11 '18 at 13:25
  • Hey uh, what version of python do you use? it seems 3.x isn't supported by snaps - could be your [issue](https://snap.stanford.edu/snappy/index.html) – Justin Apr 11 '18 at 13:49
  • I am using Python 3.6.4. The snaps module I am using is part of pygame. It's not that big file from Stanford. Snaps works just fine in the IDE, just not in Eclipse. I am finding that Anaconda can't find pygame. I am ready to throw in the towel on this. It's been hours and hours and hours. – HerdingDogRescuer Apr 11 '18 at 15:11
  • Ah okay, well i made a quick snippet for you, hopefully you can learn from it, i learned a few things doing it lol :) (edited my main post) – Justin Apr 11 '18 at 16:00