Tutorial Guide: Building a Localized Weather App Using Python and OpenWeatherMap
API
1. Introduction
In this tutorial, we’ll walk through creating a localized weather application using Python. We'll
fetch real-time weather data from the OpenWeatherMap API and display it in a user-friendly
format.
2. Prerequisites
● Basic understanding of Python
● Python 3 is installed on your system
● Internet connection
● OpenWeatherMap API key (free account at https://openweathermap.org/)
3. Project Setup
3.1. Install Required Libraries
pip install requests
3.2. File Structure
weather_app/
├── weather.py
4. Fetching Data from OpenWeatherMap API
weather.py
import requests
def get_weather(city_name, api_key):
url =
f"http://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={api_key}&units=metri
c"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
print_weather(data)
else:
print("City not found or API error.")
def print_weather(data):
city = data['name']
temp = data['main']['temp']
description = data['weather'][0]['description']
humidity = data['main']['humidity']
wind_speed = data['wind']['speed']
print(f"\nWeather in {city}:")
print(f"Temperature: {temp}°C")
print(f"Condition: {description.capitalize()}")
print(f"Humidity: {humidity}%")
print(f"Wind Speed: {wind_speed} m/s")
if __name__ == '__main__':
city = input("Enter your city: ")
api_key = "YOUR_API_KEY_HERE" # Replace with your API key
get_weather(city, api_key)
5. Running the App
● Open your terminal
● Navigate to the weather_app directory
● Run the script:
python weather.py
● Input your city name when prompted
6. Enhancements
● Add a graphical user interface (GUI) using Tkinter or PyQt
● Save recent city searches to a file
● Display forecast data (use One Call API)
● Localize language and units using API parameters
7. Troubleshooting Tips
● Ensure your API key is active and correctly entered
● Make sure you have an internet connection
● Check for typos in city names
8. Conclusion
By completing this tutorial, you’ve learned how to interact with a RESTful API using Python,
parse JSON data, and create a simple yet functional weather application. This is a great
foundation for building more complex Python apps that interact with external APIs.