pyinstaller利用spec文件打包的使用模板


pyinstaller打包

使用pyqt5開發軟件,當項目越來越大,引用的資源越來越多時,那么使用pyinstaller進行打包,如果不利用spec文件,是很難滿足打包需求的。

spec文件,其實你在使用 pyinstaller main.py打包時 ,也是會自動生成的,叫main.spec。

不過,如果你想把自己的資源文件一起打進包去,則需要對spec文件進行一些編輯,然后使用 pyinstaller main.spec即可打包完成。

pyinstaller 基礎常規打包方法,網上文章很多了,大家自己搜索也是一大堆,就不一一列舉了。這里推薦一篇,剛學習使用pyinstaller時對我幫助最大的文章,同時在此對這位大神作者表示感謝。

本文主要就是列舉下pyinstaller利用spec文件進行打包的幾個使用模板,以供大家參考使用。至於內涵原理,本人時間有限,也沒深入研究,大家根據自己情況去探索吧。

chachezulin

【如下代碼,完全復制,直接運行,即可使用】
【注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') 預覽

本文如有幫助,敬請留言鼓勵。
本文如有錯誤,敬請留言改進。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM