OpenWeatherMap Tutorial in Python using PyOWM

In this tutorial, we will learn OpenWeatherMap tutorial in Python using PyOWM. This tutorial includes the following:

  • How to get API key from OpenWeatherMap
  • Get weather data from OpenWeatherMap
  • Forecast weather
  • You can also check historical weather data

We will use the Python OpenWeatherMap API (PyOWM) to get the weather details.

Initial installations and getting API key from OpenWeatherMap

First, we start off by installing pyowm library. The easiest way to do this is to type this into your command line:

$- pip install pyowm

Then, you need to have an API key to access the weather data from

For this, all you need to do is sign up on the OpenWeatherMap Website. Refer to the picture below:
OpenWeatherMap Tutorial in Python using PyOWM

After signing up, click on “API keys” and copy your API key to your clipboard. Refer to the picture below:
getting API key from OpenWeatherMap
By default, the API key that you get will be free. But, you can choose to go for the paid key which has additional privileges. But for this tutorial, the free one will work just fine.

Now that we have installed pyowm and gotten our API key, we can start with our script!

Getting Weather data in Python from OpenWeatherMap

Firstly, import the pyowm library.

import pyowm

Then, we authenticate using an API key. The authenticated connection to the API is stored in the owm_obj object. You will have to provide the API key that you got after signing up at the  Open Weather Map website in place of <Enter_API_KEY>

api_key = "<Enter_API_KEY>"    #Enter your own API Key

To get the current weather for a particular city, you need to create an observation object using the omw object by providing the city name, id or coordinates. To get the observation object using the name of the city, use weather_at_place() method.

obs_obj = owm_obj.weather_at_place('Mumbai')

The observation object stores two important objects: A weather object and a location object.

Weather object:

To get all the current weather information, we will create the weather object.


We now get the values by making use of the different methods of pyowm. The library provides many methods to access weather data. It is only a matter of our requirement as to which one suits our needs the most. To have a look at all the methods provided by the pyowm library, visit: pyowm documentation

Let’s take a few examples:

  • Temperature:


{'temp': 298.8, 'temp_kf': None, 'temp_max': 300.5, 'temp_min': 294.3}

 To get the temperature in Celsius write, “weather.get_temperature(unit=‘celsius’) ” , instead.

  • Humidity:


  • Description:

Used to give a short or detailed description of the current weather in the city.

w.get_status()             #for brief description
w.get_detailed_status()    #for detailed description


Light Drizzle

Location Object:

Now, create the location object in the same way as before.

l = obs.get_location()

And try the methods given below:





Weather Forecasting in Python

In addition to all the above methods, this powerful library is also capable of forecasting the weather based on the weather data of the previous days.

This API provides weather forecasts. We need to create a new forecaster object for this purpose.

For 3 hour forecast:

forecast = owm_obj.three_hours_forecast('mumbai')

For daily forecast:

forecast = owm_obj.three_hours_forecast('mumbai')

Some of the forecaster methods are:



time = "2019-11-30 12:00+00"    



For learning about more methods and information about pyowm library, go to

To apply what you have learned in this tutorial, visit: Weather script using OpenWeatherMap in Python with GUI

Leave a Reply

Your email address will not be published. Required fields are marked *