PyD中的属性错误

2024-10-03 00:30:14 发布

您现在位置:Python中文网/ 问答频道 /正文

最近在Windows10上为EclipseOxygen3安装了PyDev。作为一个新的程序员,我在写一本“学习代码”的书时正在编写基本程序。我正在处理的示例使用了一个名为“snaps”的模块,它位于“pygame”中。下面的程序在pythonshellide中运行良好。在

import snaps

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

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

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

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

^{pr2}$

我已经确认Python路径在windows中设置正确,PyDev解释器也设置正确。我已经添加了每个库,脚本文件夹等,我可以在项目和透视首选项下。我甚至把pygame文件夹添加到了我的工作区。但我还是有错误。在

我已经通过help()函数确认了“get_weather_temp”在snaps模块中,它在IDE中工作,只是在PyDev中不起作用。在

如有任何帮助,将不胜感激。在


Tags: 模块代码程序文件夹示例getpygametemp
1条回答
网友
1楼 · 发布于 2024-10-03 00:30:14

我想this可能对你有帮助。如果我没搞错的话,PyDev需要被告知在哪里可以找到安装它的“模块”/“库”,IDE使用默认值,PyDev将使用自己的环境。(下面的代码片段来自评论)如果你有任何问题,只要问-它就可以开始了,你只需要把你自己的api密钥。 (它默认为kelvin,所以您必须转换为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库要容易得多。 输出结果看起来像“西雅图当前的温度是51.57”,我把变量设置得很愚蠢,这样你就可以跟着做了。在

^{pr2}$

相关问题 更多 >