Python 文件打包成exe

兩種工具將.py文件打包成exe:

  1. PyInstaller
  2. cx_Freeze
Tool Windows Linux MacOS Python2 Python3 One file mode
PyInstaller Yes Yes Yes Yes Yes Yes
cx_Freeze Yes Yes Yes Yes Yes No

一. PyInstaller (Windows)

  1. 適用版本
    python2 & python3
  2. 安裝方法

安裝pywin32:
下載安裝PyInstaller運(yùn)行前需要安裝windows拓展---pywin32
pywin32下載地址: http://sourceforge.net/projects/pywin32/files/pywin32/

安裝PyInstaller:

pip install PyInstaller

驗(yàn)證安裝成功與否:

# python 環(huán)境下導(dǎo)入PyInstaller成功,則安裝成功
import PyInstaller

# 或者CMD環(huán)境中
pyinstaller --version
  1. 使用方法
# CMD環(huán)境中同目錄下
# 生成的test.exe文件在當(dāng)前dist文件夾中
pyinstaller test.py

# 生成單文件模式
pyinstaller -F test.py

# 改變生成test.exe的圖標(biāo)
# test.ico是與test.py同目錄下的圖標(biāo)文件名
pyinstaller -F --icon=test.ico test.py
  1. 參數(shù)介紹
    PyInstaller的語(yǔ)法:pyinstaller [options] script [script...] | specfile
參數(shù) 含義
-F 指定打包后只生成一個(gè)exe格式的文件
-D –onedir 創(chuàng)建一個(gè)目錄,包含exe文件,但會(huì)依賴很多文件(默認(rèn)選項(xiàng))
-c –console, –nowindowed 使用控制臺(tái),無(wú)界面(默認(rèn))
-w –windowed, –noconsole 使用窗口,無(wú)控制臺(tái)
-i 改變生成程序的icon圖標(biāo)
-p 添加搜索路徑,讓其找到對(duì)應(yīng)的庫(kù)(一般用不到)
  1. 參考
    https://blog.csdn.net/zengxiantao1994/article/details/76578421
    https://www.pyinstaller.org/

二. cx_Freeze (Windows)

  1. 適用版本
    python2 & python3
  2. 安裝方法
# step 1:
pip install cx_Freeze

# step 2: CMD環(huán)境中 cd 到python\scripts目錄下 run以下腳本
python cxfreeze-postinstall

驗(yàn)證安裝成功與否:

# CMD 環(huán)境中 cd 到python\scripts目錄下
# 如果出現(xiàn)help等信息則安裝成功
cxfreeze -h

# python 環(huán)境下導(dǎo)入cx_Freeze成功,則安裝成功
import cx_Freeze

# CMD 環(huán)境中 cd 到python\scripts目錄下
# 如果出現(xiàn)版本信息則安裝成功 
cxfreeze --version
  1. 使用方法
# CMD 環(huán)境中
cxfreeze "C:Test\test.py" --target-dir="C:\Test" --target-name="test.exe" --include-modules="test" --icon="test.ico"
  1. 參數(shù)介紹
    cx_Freeze的語(yǔ)法:cxfreeze [options] script [script...] | specfile
參數(shù) 含義
"C:Test\test.py" 需要打包的主文件
--target-dir 打包后的程序路徑
--target-name 打包后的程序名
--include-modules 包含的模塊或庫(kù)
--icon 改變生成程序的icon圖標(biāo)
  1. 參考
    http://cx-freeze.readthedocs.io/en/latest/index.html
    http://www.itdecent.cn/p/e47d9be6fa96
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容