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:
- Takes a city name as input.
- Sends a request to a weather API.
- Receives weather data.
- Extracts useful information.
- 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:
- Create an account on OpenWeatherMap.
- Generate an API Key.
- 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
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
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:
- Agriculture
- Transportation
- Tourism
- Event Management
- Disaster Management
Common Errors and Solutions
1. Invalid API Key
Error:
Solution:
Use a valid API key.
2. City Not Found
Error:
Solution:
Check spelling of the city name.
3. Missing Requests Module
Error:
Solution:
pip install requests
4. Internet Connection Issues
Error:
Solution:
Verify internet connectivity.
Project Enhancement Ideas
After completing the basic project, try adding:
- 5-Day Weather Forecast
- Weather Icons
- GUI Interface Using Tkinter
- Weather by GPS Location
- Multiple City Comparison
- Temperature Unit Conversion
- Weather Alerts
- Dark Mode
- Voice Search
- 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.