Use resource loader for zoneinfo files

Bug #1834363 reported by Peter Baumgartner
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
pytz
New
Undecided
Unassigned

Bug Description

I'd like to use pytz in a project and package it with PyOxidizer for distribution. The use of __file__ to find the zoneinfo files is problematic. If they could be read with a resource loader instead, it would prevent this issue.

More info on the issue and resolution is here: https://pyoxidizer.readthedocs.io/en/latest/packaging_pitfalls.html

Revision history for this message
Stuart Bishop (stub) wrote :

Bug #383171 contains relevant history here. Currently the code attempts to locate the zoneinfo files using __file__, falling back to pkg_resources if it fails. I'd need a patch that works for Python2.4+ (or drop obsolete Python support, which I hadn't planned to do as pytz is maintenance only).

See open_resource in pytz/__init__.py for the relevant code.

Revision history for this message
Peter Baumgartner (ipmb) wrote :

Thanks for following up!

The issue I'm running into is that __file__ results in a NameError so it never gets to that loader.

What do you think about something like this which prefers 3.7's importlib.resources (or its backport) then falls back on the existing methods?

It also requires dropping __init__.py's in the zoneinfo directories and a little tweaking to setup.py to identify the new packages.

Revision history for this message
Peter Baumgartner (ipmb) wrote :
Revision history for this message
Stuart Bishop (stub) wrote :

Unfortunately I don't we can't drop __init__.py's in the zoneinfo directories, because most pytz installations using OS supplied packages get adjusted to use the system zoneinfo files. And at some point there will be a supported mechanism for pip installations to make use of the system zoneinfo database, overriding the pytz supplied one.

It would probably just work if we check globals().get('__file__'), falling back to pkg_resources if it isn't set the same way we fall back if the file relative to __file__ isn't found.

Revision history for this message
Peter Baumgartner (ipmb) wrote :

This change would still allow you to load via filesystem (via the PYTZ_TZDATADIR env var). Only if the filesystem path is not defined will it load using importlib.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers