官網(wǎng)
https://github.com/pyinstaller/pyinstaller
http://www.pyinstaller.org/
安裝
穩(wěn)定版本(反而有bug)
? pip3 install pyinstaller
? pyinstaller --version
3.2.1
dev版本(本文寫作是用它)
? git clone https://github.com/pyinstaller/pyinstaller.git
? cd pyinstaller
? python3 setup.py install
? pyinstaller --version
3.3.dev0+964547c
hello.py 的內(nèi)容
# -*- coding: utf-8 -*-
"""第一個(gè)程序"""
from PyQt5 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
first_window = QtWidgets.QWidget()
first_window.resize(400, 300)
first_window.setWindowTitle("我的第一個(gè)程序")
first_window.show()
sys.exit(app.exec_())
mac打包
簡(jiǎn)單打包
https://pyinstaller.readthedocs.io/en/stable/usage.html#general-options
? sudo pyinstaller -w -y hello.py
完成后的目錄
├── __pycache__
├── build
│ └── hello
├── dist
│ ├── hello
│ └── hello.app
│ └── Contents
│ ├── Frameworks
│ ├── Info.plist
│ ├── MacOS
├── hello.py
└── hello.spec
會(huì)多出3個(gè)目錄pycache, build, dist和一個(gè)文件hello.spec
其中的 dist目錄下, 有兩份輸出
dist/hello/ unix的可執(zhí)行目錄(mac下也能運(yùn)行)
dist/hello.app macOS的程序包(其實(shí)也是一個(gè)目錄)
spec文件
spec文件的作用是什么呢?PyInstaller通過執(zhí)行spec文件中的內(nèi)容來生成app隅忿,有點(diǎn)像makefile。正常使用中我們是不需要管spec文件的盹沈,但是下面幾種情況需要修改spec文件:
- 需要打包資源文件
- 需要include一些PyInstaller不知道的run-time庫(kù)
- 為可執(zhí)行文件添加run-time 選項(xiàng)
- 多程序打包
也可以通過下面命令生成spec文件
pyi-makespec hello.py
通過下面命令使用spec文件
sudo pyinstaller hello.spec
當(dāng)你使用了spec文件, 那么命令行里的參數(shù)只剩下下面的幾個(gè)還有效, 其它的都被spec文件里的參數(shù)覆蓋了:
參數(shù) | 備注 |
---|---|
--upx-dir UPX_DIR | Path to UPX utility (default: search the execution path) |
--distpath DIR | Where to put the bundled app (default: ./dist) |
--workpath WORKPATH | Where to put all the temporary work files, .log, .pyz and etc. (default: ./build) |
-y, --noconfirm | Replace output directory (default: SPECPATH/dist/SPECNAME) without asking for confirmation |
-a, --ascii | Do not include unicode encoding support (default: included if available) |
spec文件的配置
# -*- mode: python -*-
block_cipher = None
a = Analysis(['main.py'],
pathex=['/Users/kirin/workspaces/gitlab/pqbox'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='main',
debug=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='main')
spec文件中主要包含4個(gè)class: Analysis, PYZ, EXE和COLLECT.
- Analysis以py文件為輸入计维,它會(huì)分析py文件的依賴模塊袜香,并生成相應(yīng)的信息
- PYZ是一個(gè).pyz的壓縮包,包含程序運(yùn)行需要的所有依賴
- EXE根據(jù)上面兩項(xiàng)生成
- COLLECT生成其他部分的輸出文件夾鲫惶,COLLECT也可以沒有
要看更詳細(xì)的文檔在
https://pyinstaller.readthedocs.io/en/stable/usage.html#general-options
https://pyinstaller.readthedocs.io/en/stable/spec-files.html
windows打包
windows下要加--path參數(shù)來找到pyqt的dll
pyinstaller --path C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\Lib\site-packages\PyQt5\Qt\bin -y -w hello.py