pyinstaller利用spec檔案打包的使用模板
阿新 • • 發佈:2020-07-17
pyinstaller打包
使用pyqt5開發軟體,當專案越來越大,引用的資源越來越多時,那麼使用pyinstaller進行打包,如果不利用spec檔案,是很難滿足打包需求的。
spec檔案,其實你在使用pyinstaller main.py打包時 ,也是會自動生成的,叫main.spec。
不過,如果你想把自己的資原始檔一起打進包去,則需要對spec檔案進行一些編輯,然後使用pyinstaller main.spec即可打包完成。
pyinstaller 基礎常規打包方法,網上文章很多了,大家自己搜尋也是一大堆,就不一一列舉了。這裡推薦一篇,剛學習使用pyinstaller時對我幫助最大的文章,同時在此對這位大神作者表示感謝。
本文主要就是列舉下pyinstaller利用spec檔案進行打包的幾個使用模板,以供大家參考使用。至於內涵原理,本人時間有限,也沒深入研究,大家根據自己情況去探索吧。
【如下程式碼,完全複製,直接執行,即可使用】
【注1:模板中的相關路徑和檔名稱,當然需要根據自己的情況對應修改了】
【注2:如果你的spec檔案叫main.spec的話,打包命令便是pyinstaller main.spec】
【注3:當專案越來越大時,免安裝綠色資料夾 在軟體啟動速度上,比單個可執行檔案,要快!**】
模式一:使用spec檔案,打成【單個可執行檔案】
# -*- mode: python -*-
block_cipher = None
a = Analysis(['main.py'],
pathex=['D:\\PythonProject\\mysoft'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
#######!!!注意點1:載入自己的資原始檔#####################
def extra_datas(mydir):
def rec_glob(p, files):
import os
import glob
for d in glob.glob(p):
if os.path.isfile(d):
files.append(d)
rec_glob("%s/*" % d, files)
files = []
rec_glob("%s/*" % mydir, files)
extra_datas = []
for f in files:
extra_datas.append((f, f, 'DATA'))
return extra_datas
# append the 'Resources' dir
a.datas += extra_datas('Resources') ###這裡是自己的資原始檔夾
a.datas += extra_datas('Reports') ###這裡是自己的資原始檔夾
a.datas += extra_datas('Drivers') ###這裡是自己的資原始檔夾
################################################
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries, ###!!!注意點2
a.zipfiles, ###!!!注意點2
a.datas, ###!!!注意點2
[],
exclude_binaries=False, ###!!!注意點3:這裡是False
name='mysoft',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False,
icon='d:\\mysoft.ico')
模式二:使用spec檔案,打成【免安裝綠色資料夾】
# -*- mode: python -*-
block_cipher = None
a = Analysis(['main.py'],
pathex=['D:\\PythonProject\\mysoft'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
#######!!!注意點1:載入自己的資原始檔#####################
def extra_datas(mydir):
def rec_glob(p, files):
import os
import glob
for d in glob.glob(p):
if os.path.isfile(d):
files.append(d)
rec_glob("%s/*" % d, files)
files = []
rec_glob("%s/*" % mydir, files)
extra_datas = []
for f in files:
extra_datas.append((f, f, 'DATA'))
return extra_datas
# append the 'Resources' dir
a.datas += extra_datas('Resources') ###這裡是自己的資原始檔夾
a.datas += extra_datas('Reports') ###這裡是自己的資原始檔夾
a.datas += extra_datas('Drivers') ###這裡是自己的資原始檔夾
################################################
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True, ###!!!注意點3:這裡是True
name='mysoft',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False,
icon='d:\\mysoft.ico')
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='mysoft')
模式三:使用spec檔案,同時打出【單個可執行檔案】和【免安裝綠色資料夾】
# -*- mode: python -*-
block_cipher = None
a = Analysis(['main.py'],
pathex=['D:\\PythonProject\\mysoft'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
#######!!!注意點1:載入自己的資原始檔#####################
def extra_datas(mydir):
def rec_glob(p, files):
import os
import glob
for d in glob.glob(p):
if os.path.isfile(d):
files.append(d)
rec_glob("%s/*" % d, files)
files = []
rec_glob("%s/*" % mydir, files)
extra_datas = []
for f in files:
extra_datas.append((f, f, 'DATA'))
return extra_datas
# append the 'Resources' dir
a.datas += extra_datas('Resources') ###這裡是自己的資原始檔夾
a.datas += extra_datas('Reports') ###這裡是自己的資原始檔夾
a.datas += extra_datas('Drivers') ###這裡是自己的資原始檔夾
################################################
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe1 = EXE(pyz,
a.scripts,
a.binaries, ###!!!注意點2
a.zipfiles, ###!!!注意點2
a.datas, ###!!!注意點2
[],
exclude_binaries=False, ###!!!注意點3:這裡是False
name='mysoft',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False,
icon='d:\\mysoft.ico')
exe2 = EXE(pyz,
a.scripts,
[],
exclude_binaries=True, ###!!!注意點3:這裡是True
name='mysoft',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False,
icon='d:\\mysoft.ico')
coll = COLLECT(exe2,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='mysoft')
預覽
本文如有幫助,敬請留言鼓勵。
本文如有錯誤,敬請留言改進。