PyOWM: OpenWeatherMap API Wrapper

3.5.0 · active · verified Thu Apr 16

PyOWM is a Python wrapper library for the OpenWeatherMap web APIs, providing easy access to current weather conditions, forecasts, and historical weather data for various locations worldwide. It is currently at version 3.5.0 and actively maintained, with updates typically released to support new OpenWeatherMap API versions, features, and Python compatibility requirements.

Common errors

Warnings

Install

Imports

Quickstart

Initialize PyOWM with your OpenWeatherMap API key (preferably from an environment variable). Then, use the `weather_manager` to fetch current weather conditions for a city or get a daily forecast using the One Call API 3.0. A free API key supports basic calls and the first 1000 calls/day for One Call by Call API.

import os
from pyowm import OWM

API_KEY = os.environ.get('OWM_API_KEY', 'your-api-key-here')

if not API_KEY or API_KEY == 'your-api-key-here':
    print("WARNING: OWM_API_KEY environment variable not set or is placeholder.")
    print("Please get a key from openweathermap.org and set it.")
    exit()

owm = OWM(API_KEY)
mgr = owm.weather_manager()

try:
    # Search for current weather in a specific city
    observation = mgr.weather_at_place('London,GB')
    weather = observation.weather

    print(f"Current weather in London: {weather.status}, Temperature: {weather.temperature('celsius')['temp']}°C")

    # Get a One Call API daily forecast (requires One Call API 3.0 compatible key)
    # For free keys, use 'One Call by Call' subscription, first 1000 calls/day are free.
    one_call_forecaster = mgr.one_call(lat=51.5074, lon=-0.1278)
    daily_forecast = one_call_forecaster.forecast_daily
    if daily_forecast:
        today = daily_forecast[0]
        print(f"Today's forecast: {today.status}, Max Temp: {today.temperature('celsius')['max_temp']}°C")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →