loading...
Cover image for How to use the OpenWeatherMap API with Python
Next Tech

How to use the OpenWeatherMap API with Python

andrewsverdrup profile image Andrew Sverdrup Updated on ・4 min read

In this article we’ll explore the powerful OpenWeatherMap API, which allows you to access and use weather data in your programs. It can be used to display information such as the temperature, wind, and humidity, for the current weather and for the forecasted weather.

We’ll use the Python programming language and an awesome library called Python OpenWeatherMap (PyOWM) to make it easier to use the OpenWeatherMap API in Python.

This article is based on an interactive course I released on Next XYZ. The course has Python and the PyOWM library preinstalled and lets you use both directly from your browser. If you want to learn even more about PyOWM , you can check out the course and get started for free!

Setting Up Your Environment

To run the code snippets in this article you’ll need to have Python and the pyowm library installed (install instructions for pyowm can be found here).

Once you have this installed, you can open up a Python shell from your command line (usually using python or python3, depending on which version of Python you're using) and use it to run the commands below.

Getting your OpenWeatherMap API Key

Once you have your environment setup, the next step is to get your free API key from the OpenWeatherMap website.

After you’ve signed up on OpenWeatherMap’s website, you’ll see this at the top of the page:

Click on "API keys" and you’ll see the API key.

When you work with the the examples below, you’ll need to replace <api_key> with this API key.

Weather Conditions

Now that you have your API key, let’s check the weather! First we’ll start by checking if there are any clouds in the five-day forecast for Los Angeles. Here’s the code to do that:

import pyowm
owm = pyowm.OWM('<api_key>') # TODO: Replace <api_key> with your API key
la = owm.three_hours_forecast('Los Angeles, US')
print(la.will_have_clouds())

Let’s break this down. We start by importing the pyowm library, then we authenticate using an API key. The authenticated connection to the API is stored in the owm variable.

Next we use the three_hours_forecast() method to get the forecast for a specified location (in this case, Los Angeles) and store it in the la variable. Note that the three_hours_forecast() method returns a five-day forecast with weather data sampled every three hours.

Now that we have the weather object, we can call the will_have_clouds() method on it to check if there will be clouds! Within the pyowm library, this will check the weather at each three-hour interval and if any weather sample indicates there will be clouds, the method will return true, otherwise it will return false.

We can also use PyOWM to see if it will rain, be foggy, or check for other common weather conditions. OpenWeatherMap has data from all around the world as well. For example, let’s take a look at the weather in London:

import pyowm
owm = pyowm.OWM('<api_key>') # TODO: Replace <api_key> with your API key
london = owm.three_hours_forecast('London, GB')
print(london.will_have_rain())
print(london.will_have_fog())

Awesome! With just a few lines of code you can get and display some amazing information in your program.

Current Temperature

Now let’s take a look at how to get the current temperature:

import pyowm
owm = pyowm.OWM('<api_key>') # TODO: Replace <api_key> with your API key
sf = owm.weather_at_place('San Francisco, US')
weather = sf.get_weather()
print(weather.get_temperature('fahrenheit')['temp'])

As you might guess from the use of 'fahrenheit' in the above code, you can also request the data in Celsius by passing 'celsius' to the get_temperature() method.

Sunrise and Sunset Times

Another cool thing you can do with PyOWM is request and display the sunrise and sunset times. The get_sunrise_time() and get_sunset_time() methods can be called to get this info. There is a catch when using these methods though- they return the time in unix time by default (e.g., 1542800608). Passing the timeformat='iso' parameter converts the time to a more human-readable format (e.g., 2018–11–21 21:16:54+00). Also, the times are in the GMT timezone (in the Next XYZ course we cover how to convert the times to a specific timezone).

Here’s an example of getting the sunrise and sunset times:


import pyowm
owm = pyowm.OWM('<api_key>') # TODO: Replace <api_key> with your API key
boston = owm.weather_at_place('Boston, US')
weather = boston.get_weather()
print(weather.get_sunrise_time(timeformat='iso')) # Prints time in GMT timezone
print(weather.get_sunset_time(timeformat='iso')) # Prints time in GMT timezone

Wrapping Up

If you enjoyed this post and want to learn more about using Python OpenWeatherMap to access weather data, check out the course on Next XYZ, which goes more in-depth into how to use PyOWM. It also includes interactive tasks and quizzes which will help you master the material in the course. By the end of it you’ll have created a weather GUI just like the image below!

Have any questions or comments? Just leave a note below!

Thanks for reading, and happy coding!

Next Tech

Helping you build what's next in tech.

Discussion

pic
Editor guide