pyqt打包_pyinstaller

官網(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打包

簡單打包
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庫
  • 為可執(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個(gè) Awesome - XXX 系列...
    aimaile閱讀 26,842評(píng)論 6 427
  • 原文鏈接 我經(jīng)常用python寫些腳本什么的,有時(shí)候腳本寫完以后,每次運(yùn)行都得在IDE打開在運(yùn)行,很麻煩,所以經(jīng)常...
    g0閱讀 6,251評(píng)論 0 2
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,680評(píng)論 19 139
  • epub格式下載 感謝@Cluas 鏈接: https://pan.baidu.com/s/1kVGavLd 密碼...
    夜夜月閱讀 58,387評(píng)論 107 380
  • 原創(chuàng)作者:茶扉茉莉 你在西湖畔 在遙遠(yuǎn)的,我未曾去過的地方 卻總透過心窗,安靜的窺探 那時(shí)候的喜歡 純凈得像藍(lán)天一...
    茉莉的小茶館閱讀 426評(píng)論 12 16

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