PyDev中的属性错误

时间:2018-04-11 11:53:29

标签: python pydev

最近在WIndows 10上安装了Eclipse Oxygen 3的PyDev。作为一名新程序员,我正在编写基本程序,因为我正在学习编写代码"书。我正在处理的示例使用一个名为" snaps"的模块,它位于" pygame"中。下面的程序在Python Shell IDE中运行良好。

import snaps

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

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

输出:西雅图的温度为:60

但是当我在PyDev中运行时,我得到:

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'

我已经确认在Windows中正确设置了Python Path,并且PyDev Interpreter也设置正确。我已经添加了每个lib,脚本文件夹等。我可以根据项目和透视首选项。我甚至将pygame文件夹添加到我的工作区。但我仍然得到错误。

我已经确认&#34; get_weather_temp&#34;是通过help()函数在snaps模块中,它在IDE中工作,而不是在PyDev中。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我认为this可能会对您有所帮助。如果我没弄错,PyDev需要被告知在哪里寻找安装它的“模块”/“库”,IDE使用默认值,而pydev将使用自己的环境。 (下面的代码片段来自评论)如果你有任何问题只是问 - 它准备好了你只需要把你自己的api键。 (它默认为开尔文所以你必须转换为f或c,我没有花时间,如果你想这样做我找到了一个例子here

如果你好奇的话,this也就是json的反应。 输出看起来像:西雅图目前的温度是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)

决定尝试转换 - 使用pyowm lib要容易得多。 输出看起来像“西雅图目前的温度是51.57”我把变量变得很愚蠢,所以你可以跟进。

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))