兩種工具將.py文件打包成exe:
- PyInstaller
- 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)
- 適用版本
python2 & python3 - 安裝方法
安裝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
- 使用方法
# 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
- 參數(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ù)(一般用不到) |
二. cx_Freeze (Windows)
- 適用版本
python2 & python3 - 安裝方法
# 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
- 使用方法
# CMD 環(huán)境中
cxfreeze "C:Test\test.py" --target-dir="C:\Test" --target-name="test.exe" --include-modules="test" --icon="test.ico"
- 參數(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) |