6

I am trying to run a python async app with an asyncioscheduler scheduled job but the APScheduler fails during build because of this error:

'Only timezones from the pytz library are supported' error

I do include pytz in my app and i am passing the timezone. What is causing the error?

I am calling the asyncioscheduler in a class where i create job manager:

from apscheduler.schedulers.asyncio import AsyncIOScheduler


class ScheduleManager:
    def __init__(self) -> None:
    self.scheduler = AsyncIOScheduler()
    
    def start(self):
    self.scheduler.start()

    def stop(self):
    self.scheduler.shutdown()
    
    def add_seconds_interval_job(self, callback, interval : int):
    self.scheduler.add_job(callback, 'interval', seconds = interval)
    
    def add_minutes_interval_job(self, callback, interval : int):
    self.scheduler.add_job(callback, 'interval', minutes = interval)

    def add_hours_interval_job(self, callback, interval : int):
    self.scheduler.add_job(callback, 'interval', hours = interval)

    def add_days_interval_job(self, callback, interval : int):
    self.scheduler.add_job(callback, 'interval', days = interval)

then i call this manager from my application like :

from jobs import ScheduleManager, ConfigJob

class AppInitializer:

    def __init__(self) -> None:
    self.schedule_manager = ScheduleManager()
    self.config__job = ConfigJob()

    async def initialize(self, app, loop):
    self.schedule_manager.add_seconds_interval_job(self.config_job.run, 5)
    self.schedule_manager.start()
KZiovas
  • 3,491
  • 3
  • 26
  • 47
  • Questions seeking debugging help (**"why isn't this code working?"**) should include the desired behavior, *a specific problem or error* and *the shortest code necessary* to reproduce it *as formatted text* (not images) **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [mre]. – MattDMo Aug 18 '21 at 12:46
  • there is nothing special about my code i am just using the asynciosceduler() in a asynchronous but I ll add my code – KZiovas Aug 18 '21 at 13:07
  • We need a short, self-contained, runnable example that produces the same output you're getting. – MattDMo Aug 18 '21 at 13:10
  • That's fine, we just need something that can reproduce what you're seeing. – MattDMo Aug 18 '21 at 13:13

3 Answers3

5

The tzlocal library switched from pytz to zoneinfo timezones in 3.0 and APScheduler 3.x is not compatible with those. Due to this, APScheduler 3.7.0 has tzlocal pinned to v2.x. If you're getting tzlocal 3.0 installed through APScheduler, you're using an old version. Please upgrade.

Alex Grönholm
  • 5,563
  • 29
  • 32
  • If you're sure that you are only using timezones from pytz or tzlocal, please open a new question here with a minimal reproducing script. – Alex Grönholm Feb 23 '22 at 09:37
3

I stumbled over this as well. And now there's a new tzlocal version (4.1) out that isn't actually compatible with apscheduler 3.x which isn't handled in the pinning: apscheduler 3.8.1 requires tzlocal!=3.*,>=2.0 This breaks things if you have tzlocal==4.1 installed. I pinned tzlocal now manually in my requirements.yml:

tzlocal<3.0 or more specific tzlocal==2.1

Update: The apscheduler docs state that this issue is fixed with 3.8.1 and with 3.9.0 they don't enforce pytz time zones anymore. After some testing with different versions I nevertheless still get the same errors with Python 3.10.2, apscheduler 3.8.1/3.9.0 and tzlocal 4.0/4.1. I am forced to use tzlocal<3.0.

dom
  • 111
  • 5
  • How does tzlocal 4.1 break things? There is nothing in their changelog to indicate that it should be breaking things. – Alex Grönholm Feb 23 '22 at 09:40
  • You are actually right! According to the documentation it's fixed with version 3.8.1: https://apscheduler.readthedocs.io/en/stable/versionhistory.html I am using Python 3.10.2 and apscheduler 3.8.1. If I use tzlocal 4.1 I get these errors: `TypeError: Only timezones from the pytz library are supported` If I use tzlocal 2.1 this works as expected (as it uses pytz time zones). I just tested with apscheduler 3.9.0 because they stated in the above links that they no longer enforce pytz time zones in that version.But still doesn't work for me, same error. – dom Feb 24 '22 at 10:24
  • Please create a ticket in the APScheduler issue tracker with a reproducible sample. – Alex Grönholm Feb 25 '22 at 11:30
  • [Done, check here](https://github.com/agronholm/apscheduler/issues/599) – dom Feb 27 '22 at 14:58
0

Ok so it required a dependency tzlocal==2.1 so it could get local timezone, i assume for some reason the version that the module has does not work on my system

KZiovas
  • 3,491
  • 3
  • 26
  • 47