Python Weather App Project – Build a Weather App with Python

Introduction

The Weather App Project is one of the most practical and real-world Python projects for beginners and intermediate developers. This application allows users to check current weather conditions, temperature, humidity, wind speed, and weather forecasts for any city.

Building a Weather App helps developers learn how to work with:

  • APIs (Application Programming Interfaces)
  • JSON Data
  • HTTP Requests
  • User Input
  • Exception Handling
  • Python Modules

Project Overview

The Python Weather App performs the following tasks:

  1. Takes a city name as input.
  2. Sends a request to a weather API.
  3. Receives weather data.
  4. Extracts useful information.
  5. Displays weather details to the user.

Features of the Project

  • Search weather by city name
  • Display temperature
  • Display humidity
  • Display wind speed
  • Show weather conditions
  • Error handling
  • Real-time weather updates
  • User-friendly interface

What is a Weather API?

A Weather API allows applications to access weather information from weather service providers.

Popular Weather APIs:

  • OpenWeatherMap API
  • WeatherAPI
  • AccuWeather API
  • Tomorrow.io API

For this project, we’ll use the OpenWeatherMap API.

How the Weather App Works


Start
 ↓
Enter City Name
 ↓
Send API Request
 ↓
Receive JSON Data
 ↓
Extract Weather Information
 ↓
Display Weather Details
 ↓
End

Getting an API Key

Before using the API:

  1. Create an account on OpenWeatherMap.
  2. Generate an API Key.
  3. Copy the API key.

Example:


API_KEY = "your_api_key"

Understanding API URL

Example API URL:


https://api.openweathermap.org/data/2.5/weather?q=Delhi&appid=YOUR_API_KEY

Components:

Parameter Description
q City Name
appid API Key
Hard Weather Endpoint

1. Basic Weather App

Source Code


import requests
API_KEY = "your_api_key"
city = input(
    "Enter City Name: "
)
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
response = requests.get(url)
data = response.json()
print(data)

Sample Output

{
‘weather’: [
{‘main’: ‘Clouds’}
],
‘main’: {
‘temp’: 32.5
}
}

2. Extracting Weather Information

Instead of printing the entire JSON response, we can extract specific values.

Source Code


import requests
API_KEY = "your_api_key"
city = input(
    "Enter City Name: "
)
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
response = requests.get(url)
data = response.json()
temperature = data["main"]["temp"]
humidity = data["main"]["humidity"]
weather = data["weather"][0]["main"]
print("Temperature:", temperature)
print("Humidity:", humidity)
print("Weather:", weather)

Sample Output

Enter City Name: Delhi
Temperature: 34.2
Humidity: 60
Weather: Clouds

3. Display Wind Speed

The API also provides wind information.

Example:


wind_speed = data["wind"]["speed"]
print(
    "Wind Speed:",
    wind_speed,
    "m/s"
)

4. Complete Weather App

Source Code


import requests
API_KEY = "your_api_key"
city = input(
    "Enter City Name: "
)
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
response = requests.get(url)
data = response.json()
temperature = data["main"]["temp"]
humidity = data["main"]["humidity"]
weather = data["weather"][0]["description"]
wind_speed = data["wind"]["speed"]
print("\nWeather Report")
print(
    "Temperature:",
    temperature,
    "°C"
)
print(
    "Humidity:",
    humidity,
    "%"
)
print(
    "Weather:",
    weather
)
print(
    "Wind Speed:",
    wind_speed,
    "m/s"
)

Sample Output

Weather Report
Temperature: 31°C
Humidity: 65%
Weather: scattered clouds
Wind Speed: 3.4 m/s

5. Handling Invalid City Names

Users may enter incorrect city names.

Example:


if data["cod"] != 200:
    print(
        "City Not Found!"
    )
else:
    print(
        "Weather Data Found"
    )

6. Exception Handling

Network issues may cause errors.

Example:


try:
    response = requests.get(url)
except Exception as e:
    print(
        "Error:",
        e
    )

Weather App Using Functions

Functions improve readability and reusability.

Source Code


import requests
def get_weather(city):
    API_KEY = "your_api_key"
    url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
    response = requests.get(url)
    return response.json()
city = input(
    "Enter City: "
)
weather_data = get_weather(city)
print(weather_data)

Weather App with Multiple Cities

Example:


cities = [
    "Delhi",
    "Mumbai",
    "Chennai"
]
for city in cities:
    print(
        get_weather(city)
    )

GUI Weather App Using Tkinter

A graphical interface makes the application more user-friendly.

Basic GUI Example:


from tkinter import *
root = Tk()
root.title("Weather App")
root.geometry("400x300")
root.mainloop()

Real-Life Applications

Weather applications are used in:

  1. Agriculture
  2. Transportation
  3. Tourism
  4. Event Management
  5. Disaster Management

Common Errors and Solutions

1. Invalid API Key

Error:

401 Unauthorized

Solution:


Use a valid API key.

2. City Not Found

Error:

404 Not Found

Solution:


Check spelling of the city name.

3. Missing Requests Module

Error:

ModuleNotFoundError

Solution:


pip install requests

4. Internet Connection Issues

Error:

ConnectionError

Solution:


Verify internet connectivity.

Project Enhancement Ideas

After completing the basic project, try adding:

  1. 5-Day Weather Forecast
  2. Weather Icons
  3. GUI Interface Using Tkinter
  4. Weather by GPS Location
  5. Multiple City Comparison
  6. Temperature Unit Conversion
  7. Weather Alerts
  8. Dark Mode
  9. Voice Search
  10. Mobile App Version

These enhancements make the project more professional and portfolio-ready.

Conclusion

The Python Weather App is an excellent real-world project that teaches developers how to interact with APIs, process JSON data, handle user input, and build practical applications.

It introduces important software development concepts such as API integration, exception handling, and modular programming. By adding advanced features such as weather forecasts, graphical interfaces, GPS support, and weather alerts, you can transform this simple application into a professional-grade weather monitoring system.

Related Python Tutorials