Python 使用PyInstaller打包發佈

打包發佈

1.安裝插件PyInstaller

 pip install PyInstaller 

2.在目錄根目錄內新增main.spec文件(打包腳本)

 # -*- mode: python ; coding: utf-8 -*-
 
 block_cipher = None
 
 py_files = ['main.py',
             'XXX\\XX.py',
             'YYYY\\YYYYYY.py'
             ]
 add_files = [
     ('chromedriver.exe','.'),  # .表示將資源文件複製到打包文件的根目錄
     ('AppSettings.ini','.'),
     ('doc\\*.txt','doc'), 
     ('images\\my.png','images'),
     ]
 a = Analysis(py_files,         #所有腳本文件路徑
              pathex=['E:\\XX\\XX'], #項目根目錄
              binaries=[],
              datas=add_files ,  #打包資源文件(圖片、文檔、視頻等)
              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,
           [],
           exclude_binaries=True,
           name='test',   #打包後exe名稱
           debug=False,
           bootloader_ignore_signals=False,
           strip=False,
           upx=True,
           console=True )  #是否顯示命令窗口,默認true
 coll = COLLECT(exe,
                a.binaries,
                a.zipfiles,
                a.datas,
                strip=False,
                upx=True,
                upx_exclude=[],
                name='test')  #打包後文件夾名稱

3.執行打包命令

 pyinstaller -F main.spec 

4.完成後會在項目內出現build及dist文件夾,打包文件在dist文件夾內

PS:

也可以不寫spec,直接執行-F命令強制打包。但是需要打包的項目如果有資源文件及多個腳本文件依賴時,會出現打包丟失文件。所以建議按照標準打包。

直接打包exe命令(main爲入口py):

pyinstaller -F main.py

 

參考:https://www.cnblogs.com/the3times/p/12386007.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章