PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider
Asked Answered
T

4

15

I am writing a simple function that sends messages based on a schedule using AsyncIOScheduler.

scheduler = AsyncIOScheduler()
scheduler.add_job(job, "cron", day_of_week="mon-fri", hour = "16")
scheduler.start()

It seems to work, but I always get the following message:

PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
PytzUsageWarning: The localize method is no longer necessary, as this time zone supports the fold attribute (PEP 495)

I am not familiar with time in python at all, so I am not really sure what this message is asking me to do. I visited the link provided in the message, but the explanation there is too complicated for me. As I understand, I have to migrate to using PEP495, but how exactly can I do just that?

Timbering answered 30/10, 2021 at 2:46 Comment(0)
F
22

To set a PIP495 compatible timezone in APScheduler, set a parameter when instantiating the scheduler:

scheduler = AsyncIOScheduler(timezone="Europe/Berlin")
scheduler.add_job(job, "cron", day_of_week="mon-fri", hour = "16")
scheduler.start()

With flask-APScheduler (version 1.12.2), add the timezone to the configuration class:

"""Basic Flask Example from flask-apscheduler examples, file jobs.py"""
from flask import Flask
from flask_apscheduler import APScheduler


class Config:
    """App configuration."""
    JOBS = [
        {
            "id": "job1",
            "func": "jobs:job1",
            "args": (1, 2),
            "trigger": "interval",
            "seconds": 10,
        }
    ]
    SCHEDULER_API_ENABLED = True
    SCHEDULER_TIMEZONE = "Europe/Berlin"  # <========== add here


def job1(var_one, var_two):
    print(str(var_one) + " " + str(var_two))


if __name__ == "__main__":
    app = Flask(__name__)
    app.config.from_object(Config())

    scheduler = APScheduler()
    scheduler.init_app(app)
    scheduler.start()

    app.run()
Flameout answered 1/11, 2021 at 11:54 Comment(7)
for me I needed to add timezone to the IntervalTrigger, not the scheduler (I'm using BackgroundScheduler) to silence the warningHatband
thanks a lot. using with pyrogram and works :)Grand
Sure, that worked fine, I'll delete the previous onesLycurgus
@Sergo055, if the answer solves your problem please accept it, so it is closed.Flameout
This won't solve the issue mentioned in the question, answer from @pavel-sapezhka solved the issue we had.Kalakalaazar
@Stefan, I tried the code and it solves it indeed, at least with the library versions I was using when I answered it.Flameout
@AlexPoca. Sorry, didnt check this page for a while. The answer did solve my problem. Thanks a lot!Timbering
P
6

I was getting the same warning message because of the zone.

What I did was:

import tzlocal

scheduler = AsyncIOScheduler(timezone=str(tzlocal.get_localzone()))

With that local timezone I'm not getting the warning message anymore and it's running correctly.

Pollock answered 6/1, 2022 at 19:21 Comment(0)
L
3

For now, you can't effectively suppress these warnings, because the problem comes from apscheduler itself.

In your particular case warning comes from here

if obj.zone == 'local':

The problem is pytz is considered deprecated in favor of zoneinfo module and its backports. So even if you set directly timezone argument as suggested before, you'll probably face the same warning from some other places until apsheduler would fix pytz usage for modern Python version.

But still, actually, there is something that you could do, but I'm not sure if such kind of fixes is acceptable for you.

PytzUsageWarning comes from pytz_deprecation_shim package which is the dependency of tzlocal. tzlocal is deeply integrated with pytz. As apscheduler has relatively relax dependency for tzlocal, you can install pretty old version of one, that hasn't such warning, but still acceptable for apsscheduler itself.

pip install tzlocal==2.1

But, please, pay attention it could break other project dependencies, so be careful.

Liftoff answered 3/11, 2021 at 14:36 Comment(1)
Thanks a lot! Actually, the warnings did get suppressed after setting a timezone parameter when initiating the scheduler.Timbering
H
3

I just changed to

if __name__ == '__main__':
scheduler = BlockingScheduler(timezone=pytz.timezone('Asia/Ho_Chi_Minh'))
Halitosis answered 6/2, 2022 at 18:11 Comment(1)
make sure to import 'pytz' ...otherwise, this worksDisplay

© 2022 - 2025 — McMap. All rights reserved.