PyOWM: OpenWeatherMap API Wrapper
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
-
AttributeError: 'OWM25' object has no attribute 'xxx'
cause You are attempting to use code written for PyOWM v2 with a PyOWM v3 library installation. The `OWM25` class and its methods (`get_weather_manager`, etc.) were removed in PyOWM v3.fixUpdate your code to use the PyOWM v3 API. Replace `from pyowm.owm25 import OWM25` with `from pyowm import OWM` and adapt object instantiation and method calls (e.g., `owm = OWM(API_KEY)` and `mgr = owm.weather_manager()`). -
UnauthorizedError: Invalid API Key provided.
cause The OpenWeatherMap API key provided is either incorrect, expired, or you are attempting to access an API endpoint not permitted by your subscription level or that is deprecated for your key type (e.g., using a new free key with legacy API 2.5 endpoints).fixDouble-check your API key for typos. If using a new free key, ensure you are calling the `one_call()` methods. Verify your OpenWeatherMap account for API key status and subscription details. Allow some time (up to a few hours) for new API keys to become active. -
ModuleNotFoundError: No module named 'geojson'
cause The `geojson` dependency, required by PyOWM 3.4.0 and later, is missing from your environment. This can happen if pip's dependency resolution fails or if installing from source without dependencies.fixInstall the `geojson` package: `pip install geojson`. If you previously installed PyOWM, run `pip install --upgrade pyowm` to ensure all dependencies are correctly pulled in. -
KeyError: 'temp' (or similar on weather.temperature('celsius')['temp'])cause This usually indicates that the weather data returned from the OpenWeatherMap API did not contain the expected 'temp' key within the temperature data. This can happen for certain locations or when the API response structure changes slightly.fixAdd error handling to check for the existence of keys before accessing them. For temperature, sometimes min/max/day/night temps might be available instead of a generic 'temp' depending on the API call (e.g., forecast vs current weather). Check the `weather.temperature()` output structure. For example, `print(weather.temperature('celsius'))` to see available keys.
Warnings
- breaking PyOWM v3.5.0 dropped support for Python 3.8. Python 3.8 reached end-of-life status. Attempting to use PyOWM 3.5.0+ with Python 3.8 will result in incompatibility.
- breaking PyOWM v3.4.0 transitioned to OpenWeatherMap's One Call API 3.0, deprecating One Call API 2.5. OpenWeatherMap officially closed access to API 2.5 in June 2024. Older API keys or code relying on API 2.5 endpoints might fail.
- breaking PyOWM 3.0.0 was a major release that introduced significant backwards-incompatible changes from PyOWM 2.x, including a complete refactor of the high-level API functions and object model. This version also explicitly dropped Python 2.x support.
- gotcha Newer free OpenWeatherMap API keys might return 'UnauthorizedError' or fail when attempting to access legacy API endpoints (e.g., `weather_at_coords` or `forecast_at_place` directly) that are not part of the One Call API 3.0, even if the key is valid.
Install
-
pip install pyowm
Imports
- OWM
from pyowm import OWM
- weather_manager
owm = OWM25(API_KEY) mgr = owm.get_weather_manager()
from pyowm.utils import config, timestamps owm = OWM(API_KEY) mgr = owm.weather_manager()
Quickstart
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}")