How to Build a Simple Weather App in Python with OpenWeatherMap API

Reza Kalantar
4 min readMar 6, 2023

--

This tutorial will guide you through the process of building a simple weather app using Python and OpenWeatherMap API.

Images generated by MidJourney

Step 1: Sign up for OpenWeatherMap API

To get started, you will need to sign up for OpenWeatherMap API. This will allow you to access weather data that you can use in your Python script. Simply head over to https://home.openweathermap.org/users/sign_up and create a free account. Once you have created your account, navigate to your dashboard and generate an API key. You will need this key in order to access the weather data.

Step 2: Install the Requests module

The Requests module will be used to make HTTP requests to OpenWeatherMap API, so you will need to install it before proceeding. We will use the Requests module to make HTTP requests to OpenWeatherMap API. You can install the Requests module by running the following command in your terminal:

pip install requests

Step 3: Import the Requests module and API key

Now that we have installed the Requests module and generated our API key, we can start coding. Open a new Python file in your favorite code editor and import the Requests module:

import requests

Next, create a variable to store your API key:

api_key = 'YOUR_API_KEY'

Replace YOUR_API_KEY with your actual API key.

Step 4: Build the weather app

Now that we have imported the Requests module and stored our API key, we can start building our weather app. The app will prompt the user for a city name and use the Requests module to access weather data for that city.

Here’s the code:

import requests

api_key = 'YOUR_API_KEY'

city = input('Enter city name: ')

url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}'

response = requests.get(url)

if response.status_code == 200:
data = response.json()
temp = data['main']['temp']
desc = data['weather'][0]['description']
print(f'Temperature: {temp} K')
print(f'Description: {desc}')
else:
print('Error fetching weather data')

Let’s take a closer look at the code and understand how it works:

  • Firstly, the user is prompted to enter a city name using the input() function. The entered value is then stored in the ‘city’ variable.
  • Next, a URL is constructed using f-strings to access weather data for the entered city. The URL consists of the OpenWeatherMap API endpoint, the ‘city’ variable, and the API key.
  • The Requests module is used to send an HTTP GET request to the constructed URL, and the response is stored in the ‘response’ variable.
  • The status code of the response is checked to ensure that the request was successful (status code 200).
  • If the request was successful, the response data is converted into a Python dictionary using the json() method.
  • The temperature and weather description are extracted from the dictionary and printed to the console.
  • If the request was not successful, an error message is printed to the console.

By following these steps, you can build a simple weather app that retrieves weather data for a given city using Python and OpenWeatherMap API.

Step 5: Save and test the app

Save the Python file as “weather_app.py”. Now, with our weather app in place, we can test the application. Run the following command in the terminal.

python weather_app.py

Enter a city name when prompted and hit Enter. The app should retrieve weather data for the given city and print the temperature and description to the console.

If you have just signed up to get your API key and get an error, grab a cup of coffee, it may make take a little bit of time for it to be activated.

Here is the console output:

Retrieving weather forecast in London

Do not panic, the temperatures are in Kelvin (K). To convert to Celcius (C), simply subtract 273.15 from the output. In this example, the current temperature in London, United Kingdom is 280.72 - 273.15 = 7.57 C with broken clouds. I looked out of the window and it seems fairly accurate!

Congratulations, you have built a simple weather app using Python and OpenWeatherMap API! Now, you can be creative with the user interface and ship your application to the world.

Thank you for reading! If you find my blogs interesting or would like to get in touch, reach out on here, Github or LinkedIn.

--

--

Reza Kalantar

Medical AI Researcher by Profession • Scientist/Engineer by Trade • Investor by Instinct • Explorer by Nature • Procrastinator by Choice