当我运行代码时,我收到错误“ModuleNotFoundError:没有名为“urllib3.packages.six.moves”的模块”,尽管我已经多次卸载并重新安装了 6、requests 和 urllib3。
代码:
import requests
import json
def get_weather(city, api_key):
base_url = "http://api.openweathermap.org/data/2.5/weather?"
complete_url = base_url + "q=" + city + "&appid=" + api_key + "&units=metric"
response = requests.get(complete_url)
data = response.json()
if data["cod"] != "404":
main = data["main"]
wind = data["wind"]
weather = data["weather"][0]
temperature = main["temp"]
humidity = main["humidity"]
weather_description = weather["description"]
wind_speed = wind["speed"]
print(f"City: {city}")
print(f"Temperature: {temperature}°C")
print(f"Humidity: {humidity}%")
print(f"Weather: {weather_description.capitalize()}")
print(f"Wind Speed: {wind_speed} m/s")
else:
print(f"City {city} not found. Please enter a valid city name.")
if __name__ == "__main__":
api_key = "5a249ac961fe0222f884ded14818c13e"
city = input("Enter city name: ")
get_weather(city, api_key)
我尝试运行该程序,出现一个文本输出,然后要求用户输入城市,然后输入它并显示天气信息。我已经尝试重新安装六个,requests和urllib3,删除了microsoft目录中的0kb版本的python并完成了“sfc /scannow”。
您的代码在 Windows 中也可以正常运行。您使用的是虚拟环境吗?如果没有,请在命令提示符处尝试“pip list”以验证 urllib3 是否在列表中。如果您使用的是虚拟环境,请激活它并在命令提示符下执行“pip list”并检查列表中的 urllib3。有可能您运行代码时使用的环境(虚拟或系统)仍然没有安装 urllib3。