Skip to content

Remove the deprecated pytz package #33980

@UsamaSadiq

Description

@UsamaSadiq

Description

  • pytz got deprecated in Django 4.0 && has been completely removed in Django 5.0.
  • Django 4.2 had provided USE_DEPRECATED_PYTZ flag for pytz support which has now been completely removed in Django 5.0 as well.
  • Django now uses zoneinfo by default and datetime module use this under the hood now instead of pytz

Difference between datetime.timezone and zoneinfo

datetime.timezone and the zoneinfo package are both related to handling time zones in Python, but they serve slightly different purposes and have different use cases.

datetime.timezone

  • This is part of the standard library in Python.
  • It provides a simple way to represent a fixed offset from UTC (Coordinated Universal Time).
  • It doesn't have information about daylight saving time (DST) or historical changes in time zones.
  • It's suitable for scenarios where you only need to work with a constant offset, and historical changes in time zones are not important.

zoneinfo

  • The zoneinfo package is introduced in Python 3.9 as part of PEP 615.
  • It provides a more comprehensive and accurate way to handle time zones by including historical changes, daylight saving time transitions, and more.
  • It uses the IANA Time Zone Database, which is regularly updated to reflect changes in time zones around the world.
  • This package is suitable for applications that require precise handling of time zones, especially when dealing with historical dates.

pytz usages to replace

  • import pytz → 268 files in openedx/edx-platform.
  • pytz.UTC → 97 files in openedx/edx-platform.

Potential Solution

Approach 01 (OUTDATED)

  • Replace pytz.UTC with backports.zoneinfo.UTC
  • Migrate to Python>3.9
  • Replace backports.zoneinfo with zoneinfo

Approach 02

  • Upgrade from Python 3.8 to Python 3.9
  • Replace pytz.UTC with zoneinfo.UTC

Post Django 5.2 update

Edx-platform is already on Django 5.2 and all compatibility issues have already been resolved, even though the platform still relies pytz. Continued work to remove pytz will help remove an unnecessary dependency, following the recommendation in the pytz README:

Projects using Python 3.9 or later should be using the support now included as part of the standard library, and third party packages work with it such as tzdata. pytz offers no advantages beyond backwards compatibility with code written for earlier versions of Python.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions