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

使用PyInstaller打包含oracledb的Python程序后运行报错DPY-3016(未使用Fernet)

使用PyInstaller打包含oracledb的Python程序后运行报错DPY-3016(未使用Fernet)

这种情况我之前也踩过坑——明明本地环境里cryptography已经装好了,打包后却报出缺失依赖的错误,核心原因是PyInstaller的静态分析没自动识别到oracledb Thin模式运行时需要的cryptography子模块,或者打包过程中依赖被压缩/遗漏了。给你几个针对性的解决步骤:

1. 完善.spec文件的hiddenimports配置

oracledb Thin模式除了cryptography.hazmat.primitives.kdf.pbkdf2,还依赖cryptography的其他核心子模块,另外encodings.idna也是Thin模式连接Oracle时可能用到的编码模块。修改你的main2.spec文件里的hiddenimports字段:

hiddenimports=[
    'cryptography',
    'cryptography.hazmat.primitives.kdf.pbkdf2',
    'cryptography.hazmat.primitives.asymmetric.padding',
    'cryptography.hazmat.primitives.hashes',
    'uuid',
    'encodings.idna'
],

2. 在主程序主动导入依赖,强制PyInstaller扫描

在你的main.py最顶部添加以下导入语句,让PyInstaller在打包时能明确扫描到这些依赖,避免被静态分析忽略:

# 强制导入cryptography相关模块,确保PyInstaller打包时包含
import cryptography
from cryptography.hazmat.primitives.kdf import pbkdf2
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
import encodings.idna

3. 排查UPX压缩导致的依赖损坏

UPX压缩虽然能减小包体积,但有时候会破坏cryptography这类带底层实现的依赖文件。你可以先临时关闭UPX测试:

修改.spec文件中exe部分的upx=True为upx=False,重新打包后运行CREDTI.exe,如果错误消失,说明是UPX的问题。此时可以保留UPX,但把cryptography相关的文件排除在压缩外:

upx=True,
upx_exclude=['cryptography', 'oracledb'],

4. 对比正常项目的配置差异

你提到另一个同版本的项目没有问题,建议对比两个项目的:

  • .spec文件的hiddenimports、datas、UPX配置
  • 主程序的导入语句(是否有主动导入更多cryptography子模块)
  • 项目目录结构(是否有额外的PyInstaller hook文件)

找到差异点就能快速定位问题所在。

最后,修改完配置后,记得删除之前的build和dist目录,再重新执行pyinstaller main2.spec打包,避免缓存影响。

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 10:19:33