apscheduler: PyInstaller fails to package APScheduler various `pkg_resources.DistributionNotFound`

Describe the bug When using PyInstaller to compile an executable python application containing apscheduler the application fails to add all of apschedulers dependencies…

It seems that the apscheduler code is making some “on the run imports” and therefore PyInstaller does not find the packages it needs to integrate. Similar to 131.

Pyinstaller executable package can be configured using a “hook files” but I can’t determine which imports are required…

Running the executable raises various pkg_resources.DistributionNotFound errors. What is strange is that running the same executable multiple times raises different exceptions.

2021-10-12 11:02:17,694 - apscheduler.scheduler - INFO - Scheduler started
Exception in thread Thread-3:
Traceback (most recent call last):
  File "apscheduler/schedulers/base.py", line 901, in _create_plugin_instance
KeyError: 'interval'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "threading.py", line 973, in _bootstrap_inner
  File "sentry_sdk/integrations/threading.py", line 69, in run
  File "sentry_sdk/_compat.py", line 54, in reraise
  File "sentry_sdk/integrations/threading.py", line 67, in run
  File "adapters/primary/scheduler/schedule_runner.py", line 128, in run
  File "adapters/primary/scheduler/scheduler.py", line 51, in add_job
  File "apscheduler/schedulers/base.py", line 425, in add_job
  File "apscheduler/schedulers/base.py", line 926, in _create_trigger
  File "apscheduler/schedulers/base.py", line 904, in _create_plugin_instance
  File "pkg_resources/__init__.py", line 2449, in load
  File "pkg_resources/__init__.py", line 2472, in require
  File "pkg_resources/__init__.py", line 772, in resolve
pkg_resources.DistributionNotFound: The 'setuptools>=0.7' distribution was not found and is required by the application
Traceback (most recent call last):
  File "apscheduler/schedulers/base.py", line 901, in _create_plugin_instance
KeyError: 'interval'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "threading.py", line 973, in _bootstrap_inner
  File "sentry_sdk/integrations/threading.py", line 69, in run
  File "sentry_sdk/_compat.py", line 54, in reraise
  File "sentry_sdk/integrations/threading.py", line 67, in run
  File "adapters/primary/scheduler/schedule_runner.py", line 128, in run
  File "adapters/primary/scheduler/scheduler.py", line 51, in add_job
  File "apscheduler/schedulers/base.py", line 425, in add_job
  File "apscheduler/schedulers/base.py", line 926, in _create_trigger
  File "apscheduler/schedulers/base.py", line 904, in _create_plugin_instance
  File "pkg_resources/__init__.py", line 2449, in load
  File "pkg_resources/__init__.py", line 2472, in require
  File "pkg_resources/__init__.py", line 772, in resolve
pkg_resources.DistributionNotFound: The 'pytz' distribution was not found and is required by the application
2021-10-12 10:55:04,156 - apscheduler.scheduler - INFO - Scheduler started
Exception in thread Thread-3:
Traceback (most recent call last):
  File "apscheduler/schedulers/base.py", line 901, in _create_plugin_instance
KeyError: 'interval'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "threading.py", line 973, in _bootstrap_inner
  File "sentry_sdk/integrations/threading.py", line 69, in run
  File "sentry_sdk/_compat.py", line 54, in reraise
  File "sentry_sdk/integrations/threading.py", line 67, in run
  File "adapters/primary/scheduler/schedule_runner.py", line 128, in run
  File "adapters/primary/scheduler/scheduler.py", line 51, in add_job
  File "apscheduler/schedulers/base.py", line 425, in add_job
  File "apscheduler/schedulers/base.py", line 926, in _create_trigger
  File "apscheduler/schedulers/base.py", line 904, in _create_plugin_instance
  File "pkg_resources/__init__.py", line 2449, in load
  File "pkg_resources/__init__.py", line 2472, in require
  File "pkg_resources/__init__.py", line 772, in resolve
pkg_resources.DistributionNotFound: The 'six>=1.4.0' distribution was not found and is required by the application

To Reproduce Working on this now… https://github.com/iamliamc/pyinstaller_apscheduler_test

Expected behavior Python standalone application works fine. Expect compiled version to run similarly.

Additional context Possibly related tickets: 131 554

As 131 describes I have tried switching over to explicit trigger declarations like:

        self.scheduler.add_job(
            self.restore_defaults.__call__,
            id="RestoreDefaults",
            trigger=IntervalTrigger(
                start_date=self.scheduler.calc_start_for_interval_minutes(self.start_offset) + dt.timedelta(minutes=60),
                seconds=600,
            )
        )

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (8 by maintainers)

Most upvoted comments

Closing as the problem was resolved via the above PR.

Did you add the recursive=True option?

Since copy_metadata returns a subset of the collect_all:

>>> data1 = copy_metadata('apscheduler')
>>> data1
[('/home/liam/spectral/test/test_pyinstaller/venv/lib/python3.9/site-packages/APScheduler-3.8.0.dist-info', 'APScheduler-3.8.0.dist-info')]
>>> datas, binaries, hiddenimports = collect_all('apscheduler')
427475 WARNING: Unable to find package for requirement six from package apscheduler.
427475 INFO: Packages required by apscheduler:
['tzlocal', 'pytz', 'setuptools']
>>> datas[0] == data1[0]
True

I am trying this ap-scheduler-hook.py:

from PyInstaller.utils.hooks import collect_all

datas, binaries, hiddenimports = collect_all('apscheduler')