pganssle / zoneinfo

Reference implementation for the proposed standard library module zoneinfo
Other
51 stars 20 forks source link

No time zone found with key UTC #144

Closed jaraco closed 5 months ago

jaraco commented 5 months ago

After installing backports.zoneinfo on Windows, the package fails to construct even a UTC info:

 # py -3.8 -m pip-run backports.zoneinfo -- -c "import backports.zoneinfo; backports.zoneinfo.ZoneInfo('UTC')"
Traceback (most recent call last):
  File "C:\Users\jaraco\AppData\Local\Temp\pip-run-ui6qgpys\backports\zoneinfo\_common.py", line 15, in load_tzdata
    return importlib_resources.open_binary(package_name, resource_name)
  File "C:\Python38\lib\importlib\resources.py", line 88, in open_binary
    package = _get_package(package)
  File "C:\Python38\lib\importlib\resources.py", line 47, in _get_package
    module = import_module(package)
  File "C:\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'tzdata'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\jaraco\AppData\Local\Temp\pip-run-ui6qgpys\backports\zoneinfo\_common.py", line 27, in load_tzdata
    raise ZoneInfoNotFoundError(f"No time zone found with key {key}")
backports.zoneinfo._common.ZoneInfoNotFoundError: 'No time zone found with key UTC'

It appears it's not possible to construct a UTC instance. It looks like the root issue was reported in #140, that the tzdata is a required dependency but is declared optional.

How is a project to depend on zoneinfo (or backports.zoneinfo) if the imported package won't be usable?

I ran into this issue on https://github.com/jaraco/tempora/issues/29, where I was asked to port tempora from pytz to zoneinfo, but I find the Windows tests failing in https://github.com/jaraco/tempora/issues/30.

What's the right way to deploy an application that supports Windows and requires zoneinfo and Python 3.8?

pganssle commented 5 months ago

How is a project to depend on zoneinfo (or backports.zoneinfo) if the imported package won't be usable?

I ran into this issue on jaraco/tempora#29, where I was asked to port tempora from pytz to zoneinfo, but I find the Windows tests failing in jaraco/tempora#30.

What's the right way to deploy an application that supports Windows and requires zoneinfo and Python 3.8?

zoneinfo provides a mechanism for working with time zone data, tzdata provides time zone data, but your system may also provide time zone data, or you may work entirely with your own system that generates TZif files.

Unfortunately, I know of no way to tightly scope your requirements to mean, "My application requires time zone data, and I only want you to depend on tzdata if the system doesn't provide it." You could simply depend on the tzdata package unconditionally, or you could declare a conditional dependency that only triggers on windows, or you could add an optional tzdata dependency and pass the buck to your end users.

In your case, as far as I can tell you don't need pytz or zoneinfo (other than in tests, since you will presumably want to test that your users can pass datetimes not in UTC), since the only time zone you seem to use is UTC, and there's a dedicated UTC object in the standard library (datetime.timezone.utc, which in more recent versions of Python has an alias at datetime.UTC). You are better off using that than zoneinfo.ZoneInfo("UTC"), though either one is better than pytz.