首先我们需要修改.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包。