Retrieving live weather forecasts in Python with PyOWM

Over the weekend I was wondering if there were any useful APIs out there for retrieving information about the weather for a given location. I was particularly interested in current weather but this post also applies to forecasting. I had a Google and found the OpenWeatherMap service which impressed me for several reasons:

  • It is open - anyone can add a weather station to it providing they fulfill certain criteria
  • It provides a lot of information related to the weather!
  • And most importantly, it is designed with developers in mind.
It has a very friendly API as it is designed for use in mobile applications, with simple calls to retrieve information for current information, forecasting, and also historical data. Information is passed back as either JSON, XML or HTML. While I could have used the json format quite simply with Python, I had a quick search to see if there was any existing wrappers to save me some time, and sure enough I came across PyOWM. After taking 30 seconds to create a free OpenWeatherMap developer account, I had an API key and was ready to use it. The docs show a simple tutorial, although it is easy enough to figure out just by playing around with it. Firstly you need to get a handle to owm by passing your API key in as follows:

owm = pyowm.OWM(API_KEY_GOES_HERE)

Then to get the current weather at a given location you need an observation instance which is retrieved from:

observation = owm.weather_at_place("Paris,FR")

An observation contains information about the weather and the location itself, which can be retrieved by observation.get_weather() and observation.get_location() respectively. A weather object contains all the data you could ever need with simple get calls to retrieve specific information, such as:

weather.get_clouds()
weather.get_temperature()

If you’re looking for an easy to use weather API in Python I’d strongly recommend PyOWM. Oh and there’s a simple exe installer available for Windows (as I always have problems with pip on Windows).

Related

comments powered by Disqus