Summary
Improve the error message in the zoneinfo module when timezone data cannot be found due to a missing tzdata package. The current error message doesn't explain why the error occurred or how to resolve it.
Current Behavior
When ZoneInfo cannot find timezone data (typically because the tzdata package is not installed and the system doesn't provide timezone data), users see:
>>> from zoneinfo import ZoneInfo
>>> z = ZoneInfo('America/New_York')
Traceback (most recent call last):
...
zoneinfo._common.ZoneInfoNotFoundError: 'No time zone found with key America/New_York'
Problems with current message:
- Doesn't explain why the timezone wasn't found
- Doesn't tell users how to fix the issue
- No guidance on where to find more information
Proposed Enhancement
Enhance the error message when ImportError occurs (indicating missing tzdata package) to provide:
- Clear explanation of what went wrong
- Actionable steps to resolve the issue
- Link to documentation for more details
Proposed error message:
>>> from zoneinfo import ZoneInfo
>>> z = ZoneInfo('America/New_York')
Traceback (most recent call last):
...
zoneinfo._common.ZoneInfoNotFoundError: No time zone found with key America/New_York.
This error may occur if timezone data is not available. To resolve this:
• Install the tzdata package: python -m pip install tzdata
• Ensure your operating system has timezone data installed
• Verify the timezone key is correct (e.g., 'America/New_York')
For more information, see:
https://docs.python.org/3/library/zoneinfo.html
Implementation Details
The implementation involves:
-
Separate ImportError handling in _common.py:
- Currently all exceptions are caught together
- Split
ImportError to provide specific guidance for missing tzdata
-
Override __str__ in ZoneInfoNotFoundError:
- Fix formatting issue where multi-line messages display with literal
\n
- This occurs because
ZoneInfoNotFoundError inherits from KeyError, which wraps messages in repr() by default
Benefits
- Better user experience: Users immediately understand what went wrong
- Reduced support burden: Clear instructions mean fewer questions
- Consistency: Aligns with Python's philosophy of helpful error messages
- Minimal risk: Only affects error message text, not behavior
Testing
- All existing tests pass (254 tests)
- Manually verified improved error message formatting
- No breaking changes to API or behavior
Code Example
# In Lib/zoneinfo/_common.py
except ImportError:
msg = (
f"No time zone found with key {key}.\n\n"
"This error may occur if timezone data is not available. "
"To resolve this:\n"
" • Install the tzdata package: python -m pip install tzdata\n"
" • Ensure your operating system has timezone data installed\n"
" • Verify the timezone key is correct (e.g., 'America/New_York')\n\n"
"For more information, see:\n"
"https://docs.python.org/3/library/zoneinfo.html"
)
raise ZoneInfoNotFoundError(msg)
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
Summary
Improve the error message in the
zoneinfomodule when timezone data cannot be found due to a missingtzdatapackage. The current error message doesn't explain why the error occurred or how to resolve it.Current Behavior
When
ZoneInfocannot find timezone data (typically because thetzdatapackage is not installed and the system doesn't provide timezone data), users see:Problems with current message:
Proposed Enhancement
Enhance the error message when
ImportErroroccurs (indicating missingtzdatapackage) to provide:Proposed error message:
Implementation Details
The implementation involves:
Separate
ImportErrorhandling in _common.py:ImportErrorto provide specific guidance for missing tzdataOverride
__str__inZoneInfoNotFoundError:\nZoneInfoNotFoundErrorinherits fromKeyError, which wraps messages inrepr()by defaultBenefits
Testing
Code Example
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs