Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 236
Sergo055 Avatar asked Oct 30 '21 02:10

Sergo055


4 Answers

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()
like image 117
Alex Poca Avatar answered Oct 26 '22 08:10

Alex Poca


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.

like image 40
Pavel Sapezhka Avatar answered Oct 26 '22 09:10

Pavel Sapezhka


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.

like image 4
Guilherme Matheus Avatar answered Oct 26 '22 08:10

Guilherme Matheus


I just changed to

if __name__ == '__main__':
scheduler = BlockingScheduler(timezone=pytz.timezone('Asia/Ho_Chi_Minh'))
like image 2
grey Avatar answered Oct 26 '22 08:10

grey