Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `pytz` deprecated now or in the future in Python?

pytz is used in the Django version: <=3.2 doc Selecting the current time zone as shown below:

import pytz # Here

from django.utils import timezone

class TimezoneMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        tzname = request.session.get('django_timezone')
        if tzname:
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate()
        return self.get_response(request)

But, zoneinfo is used instead of pytz in the Django version: 4.0<= doc Selecting the current time zone as shown below:

import zoneinfo # Here

from django.utils import timezone

class TimezoneMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        tzname = request.session.get('django_timezone')
        if tzname:
            timezone.activate(zoneinfo.ZoneInfo(tzname))
        else:
            timezone.deactivate()
        return self.get_response(request)

My questions:

  1. Is pytz deprecated now or in the future in Python?
  2. Why isn't pytz used in the Django version: 4.0<= doc Selecting the current time zone?
like image 449
Kai - Kazuya Ito Avatar asked Jan 28 '26 03:01

Kai - Kazuya Ito


1 Answers

To quote pytz's README:

This project is in maintenance mode. Projects using Python 3.9 or later are best served by using the timezone functionaly now included in core Python and packages that work with it such as tzdata.

Without using the literal word "deprecated", the package maintainer is proposing that you don't use pytz for new projects, so that's really deprecation for all intents and purposes.

So Django's strategy is consistent with that, and in fact Django goes a bit further by facilitating the use of zoneinfo in Python 3.8 (the oldest Python version still supported). From the Django 4.0 documentation you cited:

Time zone support uses zoneinfo, which is part of the Python standard library from Python 3.9. The backports.zoneinfo package is automatically installed alongside Django if you are using Python 3.8.

and

Changed in Django 4.0: zoneinfo was made the default timezone implementation. You may continue to use pytz during the 4.x release cycle via the USE_DEPRECATED_PYTZ setting.

like image 170
slothrop Avatar answered Jan 30 '26 17:01

slothrop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!