1、安裝:pyinstaller
pip install pyinstaller
2、使用如下命令編譯
pyinstaller -F -w GraphCut.py
3、會在項目下生成文件:NewCutUI.spec。之后我們需要在文件里添加導入的包。
原始生成文件:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['NewCutUI.py'],
pathex=['E:\\項目\\GraphCut\\graph_cut'],
binaries=[],
datas=[],
hiddenimports=[],
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,
a.binaries,
a.zipfiles,
a.datas,
[],
name='NewCutUI',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False )
修改后的文件:
# -*- mode: python ; coding: utf-8 -*-
from PyInstaller.utils.hooks import collect_submodules
block_cipher = None
hiddenimports_numpy = collect_submodules('numpy')
hidden_imports_PyQt4 = collect_submodules('PyQt4')
hiddenimports_graph_cut = collect_submodules('graph_cut')
hidden_imports_maxflow = collect_submodules('maxflow')
hidden_imports_argparse = collect_submodules('argparse')
hidden_imports_GraphMaker = collect_submodules('GraphMaker')
hidden_imports_cv2 = collect_submodules('cv2')
block_cipher = None
all_hidden_imports = hiddenimports_numpy + hidden_imports_PyQt4+hiddenimports_graph_cut + hidden_imports_maxflow + hidden_imports_argparse + hidden_imports_GraphMaker + hidden_imports_cv2
a = Analysis(['NewCutUI.py'],
pathex=['E:\\項目\\GraphCut\\graph_cut'],
binaries=[],
datas=[],
hiddenimports=all_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,
a.binaries,
a.zipfiles,
a.datas,
[],
name='NewCutUI',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False )
4、使用:pyinstaller.exe NewCutUI.spec 進行打包,其中你需要找到pyinstaller.exe文件位置,我的位置:D:\anaconda\Anaconda\envs\pytorch\Scripts\pyinstaller.exe
pyinstaller.exe E:\項目\GraphCut\graph_cut\NewCutUI.spec
最后生成的exe文件目錄:D:\anaconda\Anaconda\envs\pytorch\Scripts\dist\NewCutUI.exe
我在外面導包的時候經常遇到錯誤,所以直接在文件里面填了導包的內容。最后exe文件可能會很大,大概都是使用import這個方法導包,雖然我使用from * import *沒小多少。
