You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyInstaller打包含APScheduler程序报错找不到pytz-deprecation-shim怎么办

问题解决方案

核心错误原因:pytz-deprecation-shim 包的Python导入名称为带下划线的 pytz_deprecation_shim,如果你之前创建的钩子文件使用了横杠命名为 hook-pytz-deprecation-shim.py,PyInstaller 无法识别该钩子,会导致元数据收集失效。

按照以下步骤操作即可解决:

  1. 在你的自定义钩子目录下,新建 hook-pytz_deprecation_shim.py 文件,填入以下内容:
from PyInstaller.utils.hooks import copy_metadata, collect_data_files, collect_submodules

# 收集所有隐式子模块
hiddenimports = collect_submodules('pytz_deprecation_shim')
# 收集包元数据和资源文件
datas = copy_metadata('pytz-deprecation-shim')
datas += collect_data_files('pytz_deprecation_shim')
  1. 更新你已有的 hook-tzlocal.py 文件,补全依赖收集逻辑:
from PyInstaller.utils.hooks import copy_metadata, collect_data_files, collect_submodules

hiddenimports = collect_submodules('tzlocal')
# 显式声明依赖避免漏打包
hiddenimports += ['pytz_deprecation_shim']
datas = copy_metadata('tzlocal')
datas += collect_data_files('tzlocal')
  1. 打包时保留 --additional-hooks-dir 参数指定钩子目录,可额外加显式隐式导入声明做双重保险,示例打包命令:
pyinstaller -F 你的主程序文件.py --additional-hooks-dir=./你的钩子目录路径 --hidden-import=pytz_deprecation_shim

如果执行后仍有报错,可做如下排查:

  • 先删除打包生成的 build、dist 目录和 .spec 后缀的缓存文件,再重新执行打包命令
  • 卸载所有相关依赖后重新安装,避免虚拟环境包冲突:
    pip uninstall -y pytz pytz-deprecation-shim tzlocal six APScheduler
    pip install pytz==2021.3 pytz-deprecation-shim==0.1.0.post0 tzlocal==4.1 six==1.16.0 APScheduler==3.8.1
    

内容的提问来源于stack exchange,提问作者Gordian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 05:06:05