Replace pytz with Python's zoneinfo for time zones
Python 3.9+ includes zoneinfo in the standard library. Learn how to migrate from pytz, attach time zones directly to datetimes, and check for missing time-zone data.
09 Sept 2025, 06:40 UTC

The problem: pytz in a modern Python service
Your deployment just failed on a minimal container image because pytz is not installed, or your application recorded a nonexistent local time during a daylight-saving-time spring forward. If your Python code stamps events with time-zone information, you have probably seen pytz.timezone().localize() and normalize() calls scattered through datetime handling. Python 3.9 and later include zoneinfo in the standard library, so many teams can remove the external dependency entirely. The catch is that zoneinfo does not work like pytz: you attach a time zone directly to a datetime instead of localizing a naive one, and ambiguous or missing local times still require explicit application logic.
Attach time zones directly with zoneinfo
zoneinfo.ZoneInfo reads the IANA time zone database and acts as a tzinfo object, which is the standard Python interface for attaching time-zone information to a datetime. You pass it to the datetime constructor or assign it with replace(tzinfo=...). To convert between zones, use astimezone().
from datetime import datetime
from zoneinfo import ZoneInfo
event_time = datetime(2024, 3, 10, 14, 30)
new_york = ZoneInfo("America/New_York")
aware_time = event_time.replace(tzinfo=new_york)
utc_time = aware_time.astimezone(ZoneInfo("UTC"))
Unlike pytz, there is no separate localize() step. The resulting aware_time carries its offset and can be compared or serialized with isoformat().
Migrating from pytz: what changes
| pytz pattern | zoneinfo replacement |
|---|---|
pytz.timezone("America/New_York").localize(naive_dt) | naive_dt.replace(tzinfo=ZoneInfo("America/New_York")) |
tz.normalize(aware_dt) | No direct equivalent; astimezone() handles conversion |
Ambiguous times resolved by is_dst flag | Check the fold attribute on the resulting datetime |
The fold attribute distinguishes between two possible offsets when a clock falls back. If your application schedules recurring local times, you must decide how to handle nonexistent times, such as 2:30 AM on a spring-forward morning, because zoneinfo will not silently adjust them.
Worked example: verify your runtime and time-zone data
Run these commands in a terminal where your application interpreter is available. No special permissions are required for the version check, but installing packages may require write access to your environment or a virtual environment.
- Confirm the interpreter is Python 3.9 or newer:
python --version
Expected check: the output should report Python 3.9.x or later. If it reports 3.8 or earlier, zoneinfo is not available.
- Confirm
zoneinfocan load a common zone:
python -c "from zoneinfo import ZoneInfo; ZoneInfo('America/New_York')"
Expected check: the command exits with no error. If it raises ZoneInfoNotFoundError, the host is missing the IANA time-zone database.
- Install the
tzdatabackport from PyPI if the system database is absent:
python -m pip install tzdata
Risk: this installs a package into your active environment. In production, prefer building tzdata into your container image or using a system package that provides time-zone data rather than running pip at runtime.
Limits and pre-flight checks
zoneinfois unavailable in Python 3.8 and earlier. Confirm your target runtime before removingpytz.- Systems without system
tzdataneed the PyPItzdatapackage, whichzoneinfodetects automatically when installed. - Ambiguous and nonexistent local times still need application logic. There is no automatic
is_dstflag as in olderpytzcode.
What to do next
Audit your codebase for pytz.timezone, localize, and normalize calls. Replace direct localize usage with replace(tzinfo=ZoneInfo(...)), then run your test suite against dates around DST transitions. Keep pytz in your requirements file until the migration is complete. If you need to revert, restore the previous imports and redeploy the prior image. Once tests pass, remove pytz from dependencies and confirm your container image still includes time-zone data.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.