What is the zoneinfo.ZoneInfoNotFoundError in Python?

Overview

ZoneInfoNotFoundError is a subclass of keyError1. It is raised by the ZoneInfo class constructor if no matching file is found on the search path.

Possible reasons for errors:

  • System doesn’t supply the time zone data.
  • An invalid key.

Note: The ZoneInfo module is only available in Python version 3.9 or higher.

Example

In the example below:

  • Import the ZoneInfo class.
  • Deliberately pass an incorrect format for the key America/New_York to raise ZoneInfoNotFoundError.
from zoneinfo import ZoneInfo
zone = ZoneInfo("America/NewYork")
print(zone)

How to resolve the error

Check key spelling:

  • It’s possible that you incorrectly spelled the key or provided an incorrect format, as shown in the example above.
from zoneinfo import ZoneInfo
zone = ZoneInfo("America/New_York")
print(zone)

Check available time zones in your system:

  • It’s possible that a specific time zone isn’t supported by your system.
  • The ZoneInfo module provides the zoneinfo.available_timezones() function to get the set of available time zones in your system.
import zoneinfo
for zone in zoneinfo.available_timezones():
print(zone)