- What is the difference between naive and aware datetime objects in Python?
- A naive datetime has no timezone information attached (the tzinfo attribute is None). An aware datetime has a tzinfo object that provides UTC offset and DST information. Naive datetimes are ambiguous — you cannot know whether datetime(2026, 6, 15, 14, 30) represents UTC, New York, or Tokyo without additional context. Always use aware datetimes in applications that handle multiple timezones.
- Should I use pytz or the zoneinfo module?
- Use the standard library zoneinfo module (Python 3.9+) for new projects. It provides IANA timezone support without a third-party dependency. For Python 3.8 and earlier, use the backports.zoneinfo package. pytz is a legacy option that is still widely used but has known footguns (like requiring pytz.localize() instead of replace(tzinfo=...)) that zoneinfo avoids.
- How do I convert a UTC datetime to a specific timezone in Python?
- With zoneinfo: create a UTC-aware datetime using datetime.now(timezone.utc) or datetime(..., tzinfo=timezone.utc), then call .astimezone(ZoneInfo("America/New_York")) to convert. With pytz: use pytz.utc.localize(naive_utc_dt).astimezone(pytz.timezone("America/New_York")). The zoneinfo approach is simpler and handles edge cases (like DST transitions) more correctly.