ZhuLinsen/daily_stock_analysis · error · ImportError

请安装 schedule 库: pip install schedule

Error message

请安装 schedule 库: pip install schedule

What it means

ImportError raised in StockScheduler.__init__ when the third-party 'schedule' package cannot be imported. The stdlib has no scheduler of this shape, so the dependency is required; the code logs the remediation hint and re-raises as an explicit ImportError with the pip command. It fails at scheduler construction, before any scheduling happens.

Source

Thrown at src/scheduler.py:111

        self,
        schedule_time: str = "18:00",
        schedule_time_provider: Optional[Callable[[], str]] = None,
        schedule_times: Optional[Sequence[str]] = None,
        schedule_times_provider: Optional[Callable[[], Union[Sequence[str], str]]] = None,
        register_signals: bool = True,
    ):
        """
        初始化调度器

        Args:
            schedule_time: 每日执行时间,格式 "HH:MM"
        """
        try:
            import schedule
            self.schedule = schedule
        except ImportError:
            logger.error("schedule 库未安装,请执行: pip install schedule")
            raise ImportError("请安装 schedule 库: pip install schedule")

        self.schedule_time = schedule_time
        self.schedule_times = (
            normalize_schedule_times(schedule_times, fallback_time=schedule_time)
            if schedule_times is not None
            else [(schedule_time or "").strip()]
        )
        self._schedule_time_provider = schedule_time_provider
        self._schedule_times_provider = schedule_times_provider
        self.shutdown_handler = GracefulShutdown(register_signals=register_signals)
        self._task_callback: Optional[Callable] = None
        self._daily_job: Optional[Any] = None
        self._daily_jobs: List[Any] = []
        self._background_tasks: List[Dict[str, Any]] = []
        self._running = False

    def set_daily_task(self, task: Callable, run_immediately: bool = True):
        """

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Install the package: pip install schedule (or pip install -r requirements.txt which pins it)
  2. Add 'schedule' to the deployed image's requirements and rebuild
  3. If you hit a resolver conflict, align versions so 'schedule' installs cleanly alongside the pinned stack
  4. If you never use --schedule mode, avoid constructing StockScheduler rather than removing the dependency ad hoc

Example fix

# before
$ python main.py --schedule
ImportError: 请安装 schedule 库

# after
$ pip install schedule
$ python main.py --schedule
Defensive patterns

Strategy: validation

Validate before calling

import importlib.util

def schedule_available() -> bool:
    return importlib.util.find_spec("schedule") is not None

Try / catch

try:
    scheduler = StockScheduler(schedule_time="09:30")
except ImportError:
    log.error("Schedule mode requires 'schedule'; install deps or drop --schedule")
    raise SystemExit(2)

Prevention

When it happens

Trigger: Instantiating the scheduler in an environment where 'schedule' is not installed — a fresh venv, a Docker image built from a partial requirements install, or a deployment where requirements.txt was trimmed.

Common situations: Running --schedule mode without installing dev/optional deps; CI cache or slim image missing the package; dependency resolver dropped it after a version conflict; running from source without pip install -r requirements.txt.

Related errors


AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15). Data as JSON: /api/errors/5a00efea19151e4d. Report an issue: GitHub.