Have no timezone keys for ZoneInfo on Windows 11 installation

I’m trying to migrate from pytz to zoneinfo as pytz fades into the sunset. I am running python 3.11.0 and also installed a fresh 3.12.1 (both via Python Releases for Windows | Python.org) to see if it solved my problem. I am simply trying to run the simple example:

>>> from zoneinfo import ZoneInfo
>>> from datetime import datetime, timedelta
>>> dt = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles"))

Both python versions error with

File "C:\Users\karl\AppData\Local\Programs\Python\Python312\Lib\zoneinfo\_common.py", line 24, in load_tzdata
raise ZoneInfoNotFoundError(f"No time zone found with key {key}")
zoneinfo._common.ZoneInfoNotFoundError: 'No time zone found with key America/Los_Angeles'

Note that I can use zoneinfo without issue on linux with python 3.9 and on win 11 in a pyzo shell also using python 3.9 (anaconda flavor). Does something needed to be added for 3.11+ beyond the standard install? I googled around and saw something about TZPATH but I don’t see directories named with timezone keys.

This must be a simple configuration missing.

For my Anaconda installation, I have a folder C:\Users\karl\anaconda3\share\zoneinfo that contains files and directories which match the timezone keys I want to use like America/Los_Angeles, America/New_York, etc. I tried setting a TZPATH env variable to C:\Users\karl\anaconda3\share\zoneinfo but this didn’t solve the problem. But it does explain why my pyzo/anaconda 3.9 environment has a working zoneinfo.

From the zoneinfo docs:

By default, zoneinfo uses the system’s time zone data if available; if no system time zone data is available, the library will fall back to using the first-party tzdata package available on PyPI.

First-party package maintained by the CPython core developers to supply time zone data via PyPI.

Data sources

The zoneinfo module does not directly provide time zone data, and instead pulls time zone information from the system time zone database or the first-party PyPI package tzdata, if available. Some systems, including notably Windows systems, do not have an IANA database available, and so for projects targeting cross-platform compatibility that require time zone data, it is recommended to declare a dependency on tzdata. If neither system data nor tzdata are available, all calls to ZoneInfo will raise ZoneInfoNotFoundError.

So you probably want to install tzdata e.g. via pip or whatever dependency system you use.

2 Likes

Thank you so much. That was too easy :wink:. Sorry I missed that in the docs. Interesting that the windows installer doesn’t add a pip install tzdata if the intent is to make pytz fully obsolete. Obviously, my python 3.9 installs include tzdata.

1 Like