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

如何在PyInstaller中正确导入Crypto模块?解决ModuleNotFoundError

解决PyInstaller打包时Crypto模块找不到的问题

以下是几个可行的解决步骤:

  • 确认安装正确的Crypto依赖包
    旧版pycrypto已停止维护,当前主流替代是pycryptodome,两者对外包名均为Crypto但PyInstaller的识别逻辑有差异。先清理旧依赖再安装正确版本:

    pip uninstall -y pycrypto
    pip install pycryptodome
    
  • 添加更具体的隐式导入参数
    仅指定Crypto作为隐式导入覆盖范围不足,需要明确包含代码中用到的子模块,修改打包命令:

    pyinstaller --noconfirm --onefile --console --hidden-import "Crypto" --hidden-import "Crypto.Cipher" --hidden-import "Crypto.Cipher.AES" "main.py"
    
  • 通过spec文件配置导入规则
    使用spec文件能更灵活地控制打包细节:

    1. 生成基础spec文件:
      pyi-makespec --onefile main.py
      
    2. 打开生成的main.spec,找到hiddenimports字段,修改为:
      hiddenimports=['Crypto', 'Crypto.Cipher', 'Crypto.Cipher.AES']
      
    3. 基于修改后的spec文件打包:
      pyinstaller --noconfirm main.spec
      
  • 确保自定义包的导入路径正确
    为避免自定义包ChromeSelya被PyInstaller遗漏,可在打包命令中指定项目根目录作为搜索路径:

    pyinstaller --noconfirm --onefile --console --paths ./ --hidden-import "Crypto.Cipher.AES" "main.py"
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 06:47:14