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

PyInstaller打包Python3.7应用时jwcrypto导入jwt失败求助

解决PyInstaller打包后无法导入jwcrypto.jwt的问题

问题分析

打包后的可执行文件出现ImportError: cannot import name 'jwt' from 'jwcrypto',核心原因是PyInstaller未正确收集jwcrypto.jwt、jwcrypto.jws这类子模块,仅指定jwcrypto和jwcrypto.jwk的隐藏导入不足以覆盖全部依赖。

解决方案

1. 补充完整的隐藏导入参数

修改PyInstaller命令,将jwcrypto.jwt和jwcrypto.jws加入--hidden-import列表,完整命令如下:

pyinstaller --clean --noconfirm --onedir --log-level=ERROR "./menu.py" \
--add-data "./blockchain:blockchain/" \
--add-data "./db:db/" \
--add-data "./secrets:secrets/" \
--add-data "./smartcontracts:smartcontracts/" \
--add-data "./utils:utils/" \
--hidden-import uuid \
--hidden-import "web3" \
--hidden-import "sqlite3" \
--hidden-import "jwcrypto" \
--hidden-import "jwcrypto.jwk" \
--hidden-import "jwcrypto.jwt" \
--hidden-import "jwcrypto.jws" \
--hidden-import "pydantic"

2. 验证本地环境的导入有效性

打包前先在Python3.7环境中执行以下代码,确认基础导入无问题:

from jwcrypto import jwt, jwk, jws
print("导入成功")

如果此步骤报错,说明本地jwcrypto安装存在兼容性问题,建议重新安装适配Python3.7的版本(例如pip install jwcrypto==1.4.2)。

3. 使用.spec文件手动配置依赖(可选)

若命令行参数仍无法解决,可生成.spec文件手动编辑:

  • 生成spec文件:
    pyinstaller --clean --noconfirm --onedir --log-level=ERROR "./menu.py" \
    --add-data "./blockchain:blockchain/" \
    --add-data "./db:db/" \
    --add-data "./secrets:secrets/" \
    --add-data "./smartcontracts:smartcontracts/" \
    --add-data "./utils:utils/" \
    --specpath ./specs
    
  • 打开生成的menu.spec文件,在hiddenimports列表中添加缺失模块:
    hiddenimports=['uuid', 'web3', 'sqlite3', 'jwcrypto', 'jwcrypto.jwk', 'jwcrypto.jwt', 'jwcrypto.jws', 'pydantic'],
    
  • 用spec文件重新打包:
    pyinstaller --clean --noconfirm ./specs/menu.spec
    

4. 彻底清理缓存后重试

每次打包前手动删除旧缓存文件,避免干扰:

rm -rf build dist *.spec specs/

内容的提问来源于stack exchange,提问作者J.A

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 06:41:13