Get Current Weather of Any City in Python | Weather App in Python

We made a weather app in vanilla JavaScript. Today we build a weather app in python. In this script, you will get the current weather information of any city.

We use openweathermap to get weather data. openweathermap is a service that provides weather data, including current weather data, forecasts, and historical data to the developers of web services and mobile applications.

Just sign-up yourself in openweathermap and create an account then go to this page: https://home.openweathermap.org/api_keys. Then generate a key

Now code.

Code

import requests

# paste your api key here
api_key = "PASTE_YOUR_API_KEY"

# getting city name from user
city = input("Enter city name: ")

"""
we appending the city variable and api_key variable to complete the url. for example city name is Mumbai  then url looks like 
https://api.openweathermap.org/data/2.5/weather?q=Mumbai&units=metric&APPID=4256b3de394a56a86ee35e43af6f5c2e
"""
data = requests.get(
    f"https://api.openweathermap.org/data/2.5/weather?q={city}&units=metric&APPID={api_key}"
)

# uncomment the following line and run it so you can get the data in json format
# print(data.json())

# getting the data
print(f"Location: {data.json().get('name')}, {data.json().get('sys').get('country')}")
print(f"Temperature: {data.json().get('main')['temp']}°C")
print(f"Weather: {data.json().get('weather')[0].get('main')}")
print(
    f"Min/Max Temperature: {data.json().get('main')['temp_min']}°C/{data.json().get('main')['temp_max']}°C"
)
print(f"Humidity: {data.json().get('main')['humidity']}%")
print(f"Wind: {data.json().get('wind')['speed']} km/h")

#follow @code_snail on instagram

Output

weather app in python, Find current weather of any city using OpenWeathermap API in Python

Hope you like it this simple weather app in python. Share it with your friends.

Other mini projects,