我将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 "PyInstallerloaderpyimod03_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 "PyInstallerloaderpyimod03_importers.py", line 546, in exec_module
File "bip_utilswif__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 "PyInstallerloaderpyimod03_importers.py", line 546, in exec_module
File "bip_utilswifwif.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 "PyInstallerloaderpyimod03_importers.py", line 546, in exec_module
File "bip_utilsecc__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 "PyInstallerloaderpyimod03_importers.py", line 546, in exec_module
File "bip_utilseccelliptic_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 "PyInstallerloaderpyimod03_importers.py", line 546, in exec_module
File "bip_utilseccsecp256k1.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 "PyInstallerloaderpyimod03_importers.py", line 546, in exec_module
File "bip_utilseccsecp256k1_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 "PyInstallerloaderpyimod03_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 "PyInstallerloaderpyimod03_importers.py", line 546, in exec_module
File "coincurvecontext.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 "PyInstallerloaderpyimod03_importers.py", line 546, in exec_module
File "coincurveflags.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 "PyInstallerloaderpyimod03_importers.py", line 546, in exec_module
File "coincurve_libsecp256k1.py", line 239, in <module>
File "cffiapi.py", line 150, in dlopen
File "cffiapi.py", line 832, in _make_ffi_library
File "cffiapi.py", line 827, in _load_backend_lib
OSError: cannot load library 'C:CryptoWalletdistCryptoWalletcoincurvelibsecp256k1.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中。要做到这一点,在运行pyinstaller时需要add-data
。您可以使用pyinstaller规范文件来配置编译的设置。
下面是一个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