英文:
Is `pytz` deprecated now or in the future in Python?
问题
pytz 用于 Django 版本: <=3.2 文档 中的 选择当前时区,如下所示:
import pytz # 这里
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)
但是,在 Django 版本: 4.0<= 文档 的 选择当前时区 中,使用了 zoneinfo 而不是 pytz,如下所示:
import zoneinfo # 这里
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)
我的问题:
pytz在 Python 中现在或将来是否被弃用?- 为什么在 Django 版本: 4.0<= 文档 中没有使用
pytz来选择当前时区?
英文:
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:
- Is
pytzdeprecated now or in the future in Python? - Why isn't
pytzused in the Django version: 4.0<= doc Selecting the current time zone?
答案1
得分: 3
根据 pytz 的 README:
该项目处于维护模式。使用 Python 3.9 或更高版本的项目最好使用现在包含在核心 Python 中以及与之配套工作的包(如 tzdata)的时区功能。
包维护者在不直接使用“已弃用”这个字眼的情况下,建议您不要在新项目中使用 pytz,因此从所有意图和目的来看,这实际上就是已弃用。
因此,Django 的策略与此一致,事实上,Django 通过在 Python 3.8 中提供对 zoneinfo 的使用进一步支持(这是仍然受支持的最旧版本的 Python) 。来自你引用的 Django 4.0 文档:
时区支持使用
zoneinfo,它从 Python 3.9 起是 Python 标准库的一部分。如果您使用 Python 3.8,则会自动安装backports.zoneinfo包。
和
Django 4.0 中的变化:将
zoneinfo设置为默认的时区实现。您在 4.x 发布周期内可以继续通过USE_DEPRECATED_PYTZ设置使用pytz。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论