在exe中编译后如何使用pyinstaller修复错误
How to fix error with pyinstaller after compilation in exe
我把一个Python脚本编译成exe文件,但是打开exe后,控制台打开报错,然后控制台立马关闭。也许这是因为我正在使用下载的库(web3、bs4、hdwallet、bip_utils)
Traceback (most recent call last):
File "CryptoWallet.py", line 11, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "bip_utils\__init__.py", line 21, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "bip_utils\wif\__init__.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "bip_utils\wif\wif.py", line 27, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "bip_utils\ecc\__init__.py", line 2, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "bip_utils\ecc\elliptic_curve_getter.py", line 31, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "bip_utils\ecc\secp256k1.py", line 39, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "bip_utils\ecc\secp256k1_keys_coincurve.py", line 25, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "coincurve\__init__.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "coincurve\context.py", line 4, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "coincurve\flags.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "coincurve\_libsecp256k1.py", line 239, in <module>
File "cffi\api.py", line 150, in dlopen
File "cffi\api.py", line 832, in _make_ffi_library
File "cffi\api.py", line 827, in _load_backend_lib
OSError: cannot load library 'C:\CryptoWallet\dist\CryptoWallet\coincurve\libsecp256k1.dll': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'C:\\CryptoWallet\dist\CryptoWallet\coincurve\libsecp256k1.dll'
[21180] Failed to execute script 'CryptoWallet' due to unhandled exception!
看来您需要将项目中使用的文件复制到exe中。为此,您需要 add-data
当 运行 宁 pyinstaller。您可以使用 pyinstaller spec 文件来配置编译设置。
这里是一个spec文件的例子(您需要修改它以供您使用):
a = Analysis(['C:\path\to\your\main\py file.py'],
pathex=['C:\path\to\search\for\imports'],
binaries=[],
datas=[('C:\Users\username\AppData\Local\Programs\Python\Python38\Lib\site-packages\package\xxxx\yyyyy\*', '.\package\xxxx\yyyyy'),
('C:\Users\username\AppData\Local\Programs\Python\Python38\Lib\site-packages\package2\zzz\*', '.\package2\zzz'),
],
hiddenimports=["a_package"], #A list of module names (relative or absolute) that should be part of the bundled app
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=None,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=None)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='nameOfExe',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False,
icon='icon.ico') # specify icon
您可以从 Pyinstaller 文档中查看其他参数以及它们的作用。
此文件可以运行使用:
python -m PyInstaller -F main.spec --clean
我把一个Python脚本编译成exe文件,但是打开exe后,控制台打开报错,然后控制台立马关闭。也许这是因为我正在使用下载的库(web3、bs4、hdwallet、bip_utils)
Traceback (most recent call last):
File "CryptoWallet.py", line 11, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "bip_utils\__init__.py", line 21, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "bip_utils\wif\__init__.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "bip_utils\wif\wif.py", line 27, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "bip_utils\ecc\__init__.py", line 2, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "bip_utils\ecc\elliptic_curve_getter.py", line 31, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "bip_utils\ecc\secp256k1.py", line 39, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "bip_utils\ecc\secp256k1_keys_coincurve.py", line 25, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "coincurve\__init__.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "coincurve\context.py", line 4, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "coincurve\flags.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "coincurve\_libsecp256k1.py", line 239, in <module>
File "cffi\api.py", line 150, in dlopen
File "cffi\api.py", line 832, in _make_ffi_library
File "cffi\api.py", line 827, in _load_backend_lib
OSError: cannot load library 'C:\CryptoWallet\dist\CryptoWallet\coincurve\libsecp256k1.dll': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'C:\\CryptoWallet\dist\CryptoWallet\coincurve\libsecp256k1.dll'
[21180] Failed to execute script 'CryptoWallet' due to unhandled exception!
看来您需要将项目中使用的文件复制到exe中。为此,您需要 add-data
当 运行 宁 pyinstaller。您可以使用 pyinstaller spec 文件来配置编译设置。
这里是一个spec文件的例子(您需要修改它以供您使用):
a = Analysis(['C:\path\to\your\main\py file.py'],
pathex=['C:\path\to\search\for\imports'],
binaries=[],
datas=[('C:\Users\username\AppData\Local\Programs\Python\Python38\Lib\site-packages\package\xxxx\yyyyy\*', '.\package\xxxx\yyyyy'),
('C:\Users\username\AppData\Local\Programs\Python\Python38\Lib\site-packages\package2\zzz\*', '.\package2\zzz'),
],
hiddenimports=["a_package"], #A list of module names (relative or absolute) that should be part of the bundled app
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=None,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=None)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='nameOfExe',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False,
icon='icon.ico') # specify icon
您可以从 Pyinstaller 文档中查看其他参数以及它们的作用。
此文件可以运行使用:
python -m PyInstaller -F main.spec --clean