pytz
World timezone definitions, modern and historical
This package has a good security score with no known vulnerabilities.
Community Reviews
Battle-tested but with non-intuitive APIs; consider zoneinfo for new projects
The API feels dated compared to modern Python standards. You need to remember to use `tz.localize(dt)` instead of `dt.replace(tzinfo=tz)`, and `tz.normalize()` after arithmetic operations. These aren't intuitive, and the error messages won't help you understand what went wrong. Type hints are minimal, so IDEs provide little guidance.
For Python 3.9+, the standard library's `zoneinfo` module offers a much more ergonomic API with proper datetime constructor support. Pytz remains necessary for older Python versions or projects already using it, but migration guides are sparse and the transition can be tricky.
Best for: Legacy Python projects (pre-3.9) or maintaining existing codebases that already depend on pytz.
Avoid if: You're starting a new Python 3.9+ project where the standard library's zoneinfo provides better ergonomics.
Battle-tested but with subtle gotchas; superseded by zoneinfo in Python 3.9+
Performance-wise, pytz loads timezone data lazily which helps memory usage, but the startup cost is negligible anyway. No connection pooling concerns since it's pure data manipulation. Error handling is minimal—invalid timezone names raise KeyError, not a custom exception, making defensive coding awkward. No built-in retry logic (not applicable here) but also no hooks for observability—you're on your own for tracking timezone conversion metrics.
The real issue is that Python 3.9+ has `zoneinfo` in the standard library, which uses the datetime-native tzinfo interface correctly. If you're on modern Python, there's little reason to add pytz as a dependency. It's only essential for Python 3.8 and earlier, or when you need the IANA database bundled rather than relying on system tzdata.
Best for: Legacy Python 3.8 and earlier projects requiring comprehensive timezone support with bundled IANA data.
Avoid if: You're on Python 3.9+ where zoneinfo provides the same functionality with a better API in the standard library.
Powerful but with non-intuitive gotchas that trip up newcomers
The documentation exists but doesn't emphasize these critical pitfalls enough. I've seen countless Stack Overflow questions from developers who thought `datetime(2023, 3, 15, tzinfo=pytz.timezone('US/Eastern'))` would work correctly. The error messages don't help either - pytz will happily accept incorrect usage and produce subtly wrong results rather than failing fast.
That said, when you know the patterns, it's reliable and comprehensive. Python 3.9+ added zoneinfo to the standard library which has a more intuitive API, and I'd recommend that for new projects. pytz is still necessary for Python < 3.9 or when you need the absolute latest timezone database updates.
Best for: Legacy Python codebases (< 3.9) or projects requiring bleeding-edge timezone database updates beyond what zoneinfo provides.
Avoid if: You're starting a new project on Python 3.9+ where the standard library's zoneinfo module provides a more intuitive API.
Sign in to write a review
Sign In