Build a Weather App Using Python and OpenWeather API (Step-by-Step Guide)

Want to build your first real-world Python project? A Weather App is a perfect beginner-friendly project that helps you learn about APIs, JSON, and creating a functional Python program.

In this blog, you'll learn how to create a simple command-line Weather App using Python and the OpenWeatherMap API — from setup to final output!


🔧 Tools You’ll Need:

  • Python installed on your computer

  • Internet connection

  • A free API key from OpenWeatherMap


🌐 Step 1: Get OpenWeatherMap API Key

  1. Go to https://openweathermap.org/api

  2. Sign up for a free account

  3. Navigate to the API keys section in your profile

  4. Copy your API key (you’ll need it for the script)


📁 Step 2: Create a Python File

Create a file named weather_app.py


🧱 Step 3: Write the Python Code

python
import requests def get_weather(city): API_KEY = "YOUR_API_KEY" # Replace with your actual key BASE_URL = "http://api.openweathermap.org/data/2.5/weather" params = { 'q': city, 'appid': API_KEY, 'units': 'metric' } response = requests.get(BASE_URL, params=params) data = response.json() if data.get("cod") != 200: print("City not found.") return weather = data['weather'][0]['description'] temperature = data['main']['temp'] humidity = data['main']['humidity'] wind_speed = data['wind']['speed'] print(f"📍 City: {city}") print(f"🌤️ Weather: {weather}") print(f"🌡️ Temperature: {temperature}°C") print(f"💧 Humidity: {humidity}%") print(f"🌬️ Wind Speed: {wind_speed} m/s") # Run the app city_name = input("Enter city name: ") get_weather(city_name)

🧪 Step 4: Test the App

  • Open your terminal or command prompt

  • Run:

bash
python weather_app.py
  • Enter a city name (e.g., Delhi, London)

  • See the current weather data appear in your console!


⚙️ How It Works

  • We use the requests library to send a GET request to OpenWeather's API.

  • The response is in JSON format. We extract specific data like temperature, weather condition, humidity, etc.

  • The app then prints that information in a user-friendly way.


🚀 Bonus: Add More Features

  • Convert it into a GUI using Tkinter or PyQt

  • Add weather forecast for 5 days

  • Log data to a file

  • Show icons for weather conditions


🔒 Don't Forget

✅ Replace "YOUR_API_KEY" with your actual OpenWeather API key
✅ Handle API errors and invalid city inputs properly


✅ Final Thoughts

This Weather App is a great introduction to APIs, Python requests, and JSON. It’s lightweight, beginner-friendly, and helps you build a project you can be proud of.


💬 Want a GUI
version with Tkinter? Or want to deploy it on the web using Flask? Comment below and I’ll help you build it!

Post a Comment

Post a Comment (0)

Previous Post Next Post