Client reference

class python_weather.client.Client(*, unit: _Unit = <Unit [C, Kmph]>, locale: Locale = Locale.ENGLISH, session: ClientSession | None = None, max_retries: int = 3)

Interact with the API’s endpoints.

Examples:

# Explicit cleanup
client = python_weather.Client(unit=python_weather.IMPERIAL)

# ...

await client.close()

# Implicit cleanup
async with python_weather.Client(unit=python_weather.IMPERIAL) as client:
  # ...
Parameters:
  • unit (_Unit) – Whether to use the metric or imperial/customary system (IMPERIAL). Defaults to METRIC.

  • locale (Locale) – Whether to use a different locale/language as the description for the returned forecast. Defaults to Locale.ENGLISH.

  • session (ClientSession | None) – Whether to use an existing ClientSession for requesting or not. Defaults to None (creates a new one instead).

  • max_retries (int | None) – Maximum amount of retries upon request failure before raising a RequestError. Use -1 to disable (NOT recommended). Defaults to 3 retries.

Raises:

Errorunit is not METRIC or IMPERIAL or locale is not a part of the Locale enum.

async close() None

Closes the client.

Example:

await client.close()
async get(location: str, *, unit: _Unit | None = None, locale: Locale | None = None) Forecast

Fetches a weather forecast for a specific location.

Example:

weather = await client.get('New York')
Parameters:
  • location (str) – The requested location.

  • unit (_Unit | None) – Overrides the unit used.

  • locale (Locale | None) – Overrides the locale used.

Raises:
  • TypeError – The specified location is not a string.

  • ValueError – The specified location is empty.

  • Error – The client is already closed.

  • RequestError – The client received a non-favorable response from the API.

Returns:

The requested weather forecast.

Return type:

Forecast

property locale: Locale

The localization used.

property unit: _Unit

The measuring unit used.

class python_weather.enums.Locale

A supported locale.

AFRIKAANS = 'af'
AMHARIC = 'am'
ARABIC = 'ar'
ARMENIAN = 'hy'
AZERBAIJANI = 'az'
BANGLA = 'bn'
BASQUE = 'eu'
BELARUSIAN = 'be'
BOSNIAN = 'bs'
BULGARIAN = 'bg'
CATALAN = 'ca'
CHINESE_SIMPLIFIED = 'zh'
CHINESE_SIMPLIFIED_CHINA = 'zh-cn'
CHINESE_TRADITIONAL_TAIWAN = 'zh-tw'
CROATIAN = 'hr'
CZECH = 'cs'
DANISH = 'da'
DUTCH = 'nl'
ENGLISH = 'en'
ESPERANTO = 'eo'
ESTONIAN = 'et'
FINNISH = 'fi'
FRENCH = 'fr'
FRISIAN = 'fy'
GALICIAN = 'gl'
GEORGIAN = 'ka'
GERMAN = 'de'
GREEK = 'el'
HINDI = 'hi'
HIRI_MOTU = 'ho'
HUNGARIAN = 'hu'
ICELANDIC = 'is'
INDONESIAN = 'id'
INTERLINGUA = 'ia'
IRISH = 'ga'
ITALIAN = 'it'
JAPANESE = 'ja'
JAVANESE = 'jv'
KAZAKH = 'kk'
KISWAHILI = 'sw'
KOREAN = 'ko'
KYRGYZ = 'ky'
LATVIAN = 'lv'
LITHUANIAN = 'lt'
MACEDONIAN = 'mk'
MALAGASY = 'mg'
MALAYALAM = 'ml'
MARATHI = 'mr'
NORWEGIAN_BOKMAL = 'nb'
NORWEGIAN_NYNORSK = 'nn'
OCCITAN = 'oc'
PERSIAN = 'fa'
POLISH = 'pl'
PORTUGUESE = 'pt'
PORTUGUESE_BRAZIL = 'pt-br'
ROMANIAN = 'ro'
RUSSIAN = 'ru'
SERBIAN = 'sr'
SERBIAN_LATIN = 'sr-lat'
SLOVAK = 'sk'
SLOVENIAN = 'sl'
SPANISH = 'es'
SWEDISH = 'sv'
TAMIL = 'ta'
TELUGU = 'te'
THAI = 'th'
TURKISH = 'tr'
UKRAINIAN = 'uk'
UZBEK = 'uz'
VIETNAMESE = 'vi'
WELSH = 'cy'
ZULU = 'zu'
class python_weather.errors.Error

The base error class. Extends Exception.

class python_weather.errors.RequestError

Thrown upon HTTP request failure. Extends Error.

reason: str | None

The reason for this status code.

status: int | None

The status code.

class python_weather.client.Client(*, unit: _Unit = <Unit [C, Kmph]>, locale: Locale = Locale.ENGLISH, session: ClientSession | None = None, max_retries: int = 3)

Interact with the API’s endpoints.

Examples:

# Explicit cleanup
client = python_weather.Client(unit=python_weather.IMPERIAL)

# ...

await client.close()

# Implicit cleanup
async with python_weather.Client(unit=python_weather.IMPERIAL) as client:
  # ...
Parameters:
  • unit (_Unit) – Whether to use the metric or imperial/customary system (IMPERIAL). Defaults to METRIC.

  • locale (Locale) – Whether to use a different locale/language as the description for the returned forecast. Defaults to Locale.ENGLISH.

  • session (ClientSession | None) – Whether to use an existing ClientSession for requesting or not. Defaults to None (creates a new one instead).

  • max_retries (int | None) – Maximum amount of retries upon request failure before raising a RequestError. Use -1 to disable (NOT recommended). Defaults to 3 retries.

Raises:

Errorunit is not METRIC or IMPERIAL or locale is not a part of the Locale enum.

async close() None

Closes the client.

Example:

await client.close()
async get(location: str, *, unit: _Unit | None = None, locale: Locale | None = None) Forecast

Fetches a weather forecast for a specific location.

Example:

weather = await client.get('New York')
Parameters:
  • location (str) – The requested location.

  • unit (_Unit | None) – Overrides the unit used.

  • locale (Locale | None) – Overrides the locale used.

Raises:
  • TypeError – The specified location is not a string.

  • ValueError – The specified location is empty.

  • Error – The client is already closed.

  • RequestError – The client received a non-favorable response from the API.

Returns:

The requested weather forecast.

Return type:

Forecast

property locale: Locale

The localization used.

property unit: _Unit

The measuring unit used.

python_weather.constants.METRIC = <Unit [C, Kmph]>

A supported measurement unit.

python_weather.constants.IMPERIAL = <Unit [F, Miles]>

A supported measurement unit.