We have checked different weather APIs to find the best weather API for Python. The most important features to consider were:
- the quality of weather data
- a wrapper library that will make implementation in Python easier
- a weather archive based on actual measurements available
- the level of support provided
The final verdict was easy to make. From those high-quality weather data providers we have selected, only Meteosource provides a Python wrapper library called pymeteosource that makes it very easy to use the weather API in your Python applications. We will walk through the process of using the pymeteosource library to retrieve weather data from the Meteosource API.
Signing up for a weather API
The first step in using the Meteosource weather API is to sign up for a Meteosource account and obtain an API key. You can do this by visiting the meteosource weather API website and following the sign-up process. There is also a free developer plan available for testing. Once you have your API key, you're ready to start using the weather API.
Installing the pymeteosource library
Before you can use the pymeteosource library you will need to install it first. You can do this by running the following command in your terminal or command prompt:
pip install pymeteosource
To initialize the Meteosource object, you need your API key and the name of your subscription plan (tier). A basic example of initialization is shown below:
from datetime import datetime, timedelta
from pymeteosource.api import Meteosource
from pymeteosource.types import tiers
# Change this to your actual API key
YOUR_API_KEY = 'abcdefghijklmnopqrstuvwxyz0123456789ABCD'
# Change this to your actual tier
YOUR_TIER = tiers.FLEXI
# Initialize the main Meteosource object
meteosource = Meteosource(YOUR_API_KEY, YOUR_TIER)
Retrieving weather data in Python with pymeteosource
After following the above steps, you have your script ready to get your weather data. The Meteosource class provides several methods for retrieving weather data, depending on your specific use case.
To get the weather data for a given place, use get_point_forecast() method of the Meteosource object. You have to specify either the coordinates of the place (latitude + longitude) or the place_id.
from pymeteosource.types import sections, langs, units
# Get the forecast for a given point
forecast = meteosource.get_point_forecast(
lat=37.777, # Latitude of the point
lon=-122.416, # Longitude of the point
place_id=None, # You can specify place_id instead of lat+lon
sections=[sections.CURRENT, sections.HOURLY], # Defaults to '("current", "hourly")'
tz='US/Pacific', # Defaults to 'UTC', regardless of the point location
lang=langs.ENGLISH, # Defaults to 'en'
units=units.US # Defaults to 'auto'
In this example, we're using the get_point_forecast() method to retrieve weather data for San Francisco, California. To access a variable you need, you can use the dot operator (.), or the index operator ():
You can even easily export the data to Pandas. The nested sections (for example wind.angle) are flattened, and the column names are created by concatenating them with _. So for example, instead of wind.angle, you get a column named wind_angle.
df = forecast.hourly.to_pandas()
And that’s it! If you need more information and examples, you will find them in the pymeteosource library documentation.