ImportError:无法导入名称'天气'

时间:2018-01-17 04:16:30

标签: python importerror

我刚从文档页面复制并粘贴了这段代码,我使用pip成功安装了包,但我一直遇到这个错误:

  File "c:\Users\Guppy\Desktop\RACHEAL\RACHEALs Tools\Applications\Weather\weather.py", line 1, in <module>
    from weather import Weather
ImportError: cannot import name 'Weather'

下面是代码:

from weather import Weather

weather = Weather()

# Lookup WOEID via http://weather.yahoo.com.

lookup = weather.lookup(560743)
condition = lookup.condition()
print(condition.text())

# Lookup via location name.

location = weather.lookup_by_location('dublin')
condition = location.condition()
print(condition.text())

# Get weather forecasts for the upcoming days.

forecasts = location.forecast()
for forecast in forecasts:
    print(forecast.text())
    print(forecast.date())
    print(forecast.high())
    print(forecast.low())

有没有人知道什么是错的或以任何方式解决这个问题?

3 个答案:

答案 0 :(得分:1)

使用python 2.x,我相信天气API不适用于Python 3,只需通过pip安装即可。

您可能需要重新安装它才能使用pip3

在Python3.x上运行

您可以尝试:pip3 install weather-api

答案 1 :(得分:1)

您的测试程序weather.py与您尝试使用的模块之间的名称冲突似乎相同; Python正在尝试从您的程序而不是模块加载Weather类。尝试重命名测试人员,并删除关联的weather.pyc和/或__pycache__目录(如果存在)。

答案 2 :(得分:0)

当你的运行时正在查看其中两个中的另一个时,Pip偶尔会将软件包安装到本地解释器或virtual_env。

找出pip安装模块的位置,然后检查sys.path以查看是否列出了该目录。如果没有,那就是你的问题。添加到路径或安装它将找到的地方。