记一次Pyinstaller打包缺失.dll解决方式,ImportError: DLL load failed while importing blspy: 动态链接库(DLL)初始化例程失败。

首先我们需要修改.spec文件中的

debug=True,console=True # 开启debug  控制台输出打开模式
# -*- mode: python ; coding: utf-8 -*-
import sys
sys.setrecursionlimit(5000)
from pkg_resources import get_distribution

from PyInstaller.utils.hooks import collect_submodules

block_cipher = None
keyring_imports = collect_submodules("keyring.backends")
hidden_imports = []
hidden_imports.extend(keyring_imports)
hidden_imports.extend(["win32timezone", "win32cred", "pywintypes", "win32ctypes.pywin32"])

a = Analysis(['SecretKeyGenerate.py'],
             pathex=[],
             binaries=[],
             datas=[('data','data')],
             hiddenimports=hidden_imports,
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='SecretKeyGenerate',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=False )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='SecretKeyGenerate')

之后我们进行打包命令:

(secretKeyGenerate) PS D:\PycharmProjects\secretKeyGenerate> pyinstaller -F .\SecretKeyGenerate.spec

# 打包结果
14200 INFO: Removing dir D:\PycharmProjects\secretKeyGenerate\dist\SecretKeyGenerate
14284 INFO: Building COLLECT COLLECT-00.toc
14849 INFO: Building COLLECT COLLECT-00.toc completed successfully.

之后我们在控制台运行我们打包后的程序

(secretKeyGenerate) PS D:\PycharmProjects\secretKeyGenerate> .\dist\SecretKeyGenerate\SecretKeyGenerate.exe

# 完整日志
[19752] PyInstaller Bootloader 3.x
[19752] LOADER: executable is D:\PycharmProjects\secretKeyGenerate\dist\SecretKeyGenerate\SecretKeyGenerate.exe
[19752] LOADER: homepath is D:\PycharmProjects\secretKeyGenerate\dist\SecretKeyGenerate
[19752] LOADER: _MEIPASS2 is NULL
[19752] LOADER: archivename is D:\PycharmProjects\secretKeyGenerate\dist\SecretKeyGenerate\SecretKeyGenerate.exe
[19752] LOADER: No need to extract files to run; setting extractionpath to homepath
[19752] LOADER: SetDllDirectory(D:\PycharmProjects\secretKeyGenerate\dist\SecretKeyGenerate)
[19752] LOADER: Already in the child - running user's code.
[19752] LOADER: Python library: D:\PycharmProjects\secretKeyGenerate\dist\SecretKeyGenerate\python38.dll
[19752] LOADER: Loaded functions from Python library.
[19752] LOADER: Manipulating environment (sys.path, sys.prefix)
[19752] LOADER: sys.prefix is D:\PycharmProjects\secretKeyGenerate\dist\SecretKeyGenerate
[19752] LOADER: Pre-init sys.path is D:\PycharmProjects\secretKeyGenerate\dist\SecretKeyGenerate\base_library.zip;D:\PycharmProjects\secretKeyGenerate\dist\SecretKeyGenerate
[19752] LOADER: Setting runtime options
[19752] LOADER: Initializing python
[19752] LOADER: Overriding Python's sys.path
[19752] LOADER: Post-init sys.path is D:\PycharmProjects\secretKeyGenerate\dist\SecretKeyGenerate\base_library.zip;D:\PycharmProjects\secretKeyGenerate\dist\SecretKeyGenerate
[19752] LOADER: Setting sys.argv
[19752] LOADER: setting sys._MEIPASS
[19752] LOADER: importing modules from CArchive
[19752] LOADER: extracted struct
[19752] LOADER: callfunction returned...
[19752] LOADER: extracted pyimod01_os_path
[19752] LOADER: callfunction returned...
[19752] LOADER: extracted pyimod02_archive
[19752] LOADER: callfunction returned...
[19752] LOADER: extracted pyimod03_importers
[19752] LOADER: callfunction returned...
[19752] LOADER: Installing PYZ archive with Python modules.
[19752] LOADER: PYZ archive: PYZ-00.pyz
[19752] LOADER: Running pyiboot01_bootstrap.py
[19752] LOADER: Running pyi_rth_multiprocessing.py
[19752] LOADER: Running pyi_rth_pkgres.py
[19752] LOADER: Running pyi_rth_pyqt5.py
[19752] LOADER: Running SecretKeyGenerate.py
Traceback (most recent call last):
  File "SecretKeyGenerate.py", line 2, in <module>
    from keychain import Keychain, master_sk_to_farmer_sk, master_sk_to_pool_sk
  File "PyInstaller\loader\pyimod03_importers.py", line 531, in exec_module
  File "keychain.py", line 10, in <module>
    from blspy import AugSchemeMPL, G1Element, PrivateKey
ImportError: DLL load failed while importing blspy: 动态链接库(DLL)初始化例程失败。
[19752] Failed to execute script SecretKeyGenerate
[19752] LOADER: OK.
[19752] LOADER: Manually flushing stdout and stderr
[19752] LOADER: Cleaning up Python interpreter.

然鹅此时 我们的Python程序没打包运行却可以正常运行该程序!仅是打包之后运行报错!

1. 我将C:\Windows\System32目录下的所有的dll文件 通过以下Python脚本复制到我的缓存目录:

import pathlib
import shutil

# C:\\Windows\\System32
# C:\\Users\\zhangjie\\anaconda3\\envs\\secretKeyGenerate
# C:\\Users\\zhangjie\\anaconda3\\envs\\secretKeyGenerate\\Lib\\site-packages
rnf = pathlib.WindowsPath('C:\\Users\\zhangjie\\anaconda3\\envs\\secretKeyGenerate')
pth = []
for i in rnf.iterdir():
    # print(i)
    if str(i).endswith('.dll'):
        pth.append((str(i), '.'))
        shutil.copy(str(i), 'D:\\tmp\\dll')

print(pth)

全部依次丢到我的打包后的目录D:\PycharmProjects\secretKeyGenerate\dist\SecretKeyGenerate试过之后,发现将以下6个.dll文件放入到我打包成功后的D:\PycharmProjects\secretKeyGenerate\dist\SecretKeyGenerate目录下,我的程序可以正常运行!

 

完成了本次的缺失.dll文件的bug解决!

总结一下本次的经验:

1. 从C:\\Windows\\System32 目录下复制.dll 文件放到打包后的程序目录下面去试;

2. 从C:\\Users\\zhangjie\\anaconda3\\envs\\secretKeyGenerate 目录下复制.dll 文件放到打包后的程序目录下面去试;

3. 从C:\\Users\\zhangjie\\anaconda3\\envs\\secretKeyGenerate\\Lib\\site-packages 目录下复制.dll文件放到打包后的程序目录下面去试;

最后发现缺失的正是第三种情况下的dll文件。

遇到问题表慌,反正我昨天有点慌 =_=。想很多办法,弄了一晚上 8点半了饭都么吃,难受

Pyinstaller打包失败可以尝试重新安装低版本Pyinstaller包。

 

从您的描述来看,在尝试导入 TensorFlow 遇到了 `ImportError: DLL load failed` 错误。这个问题通常是由于动态链接库加载失败引起的,常见的原因包括环境配置不当、依赖项缺失或其他兼容性问题。以下是关于这一错误的具体分析及解决方案: --- ### 可能的原因 1. **CUDA 或 cuDNN 配置不正确** - 如果您安装的是 GPU 版本的 TensorFlow,则需要确保系统上已正确定位并安装了与之匹配版本的 CUDA 和 cuDNN。 2. **Python 环境或 TensorFlow 版本冲突** - 不同版本的 Python 和 TensorFlow 具有不同的支持范围。某些组合可能会导致无法正常工作的情况发生。 3. **操作系统架构问题** - 您的操作系统的位数应该与所使用的 TensorFlow 轮子文件相匹配(比如 Windows x86_64 对应于官方发布的预编译二进制)。 4. **缺少必要的 Visual C++ Redistributable** - 微软的一些更新或者特定功能集可能未安装完整;尤其是较老版 VC++ Runtime Libraries 容易造成类似的问题。 5. **路径变量污染** - 若 PATH 环境变量中有干扰性的条目指向错误的位置也可能引发此类异常行为。 --- ### 解决方案 #### 方法一:验证 CUDA/cuDNN 是否适配 首先确认是否真的需要用到 GPU 加速。如果不需要的话建议切换到 CPU-only 的 TensorFlow 发行版(`tensorflow-cpu`),这样就不必处理复杂的显卡驱动程序集成问题了。另外如果是确实要用gpu加速计算性能,请参考[官方文档](https://www.tensorflow.org/install/source_windows#gpu)检查对应的cuda和cuDNN版本需求,并按需下载安装合适的工具链。 #### 方法二:升级 pip 并重新安装 TensorFlow 有候简单的重装能够修复许多隐匿的小毛病: ```bash pip install --upgrade pip setuptools wheel pip uninstall tensorflow pip install tensorflow==指定版本号 ``` 注意替换上面命令中的“指定版本号”,以适应当前 python 版本以及硬件条件限制的最佳选择。 #### 方法三:安装正确的 Microsoft Visual Studio redistributables 前往 [Microsoft官网](https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads),找到适合自己操作系统的VCRedist发行包进行安裝即可解决大多数因缺乏必要运行刻而产生的dll丢失报错现象。 #### 方法四:清理旧残留物后再试一次先前失败的安装会留下一些垃圾资料影响新版本的表现,因此先彻底移除现有的tf相关软件再依照上述步骤一步步来是最好的办法之一。 #### 方法五:使用 Conda 来管理虚拟环境 Anaconda 提供了一种更易于维护的方式去隔离各个项目所需的独立生态系统,从而降低交叉感染的风险几率。利用 conda 创建全新env然后在其中单独激活所需要的包也是一种不错的选择: ```bash conda create -n my_env python=3.x activate my_env conda install tensorflow ``` ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值