pyqt

pyqt打包_pyinstaller

2017-04-27  本文已影响711人  jmukirin

官网

https://github.com/pyinstaller/pyinstaller
http://www.pyinstaller.org/

安装

稳定版本(反而有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 的内容

# -*- coding: utf-8 -*-
"""第一个程序"""
from PyQt5 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
first_window = QtWidgets.QWidget()
first_window.resize(400, 300)
first_window.setWindowTitle("我的第一个程序")
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

会多出3个目录pycache, build, dist和一个文件hello.spec

其中的 dist目录下, 有两份输出

dist/hello/          unix的可执行目录(mac下也能运行)
dist/hello.app    macOS的程序包(其实也是一个目录)

spec文件

spec文件的作用是什么呢?PyInstaller通过执行spec文件中的内容来生成app,有点像makefile。正常使用中我们是不需要管spec文件的,但是下面几种情况需要修改spec文件:

也可以通过下面命令生成spec文件
pyi-makespec hello.py

通过下面命令使用spec文件
sudo pyinstaller hello.spec

当你使用了spec文件, 那么命令行里的参数只剩下下面的几个还有效, 其它的都被spec文件里的参数覆盖了:

参数 备注
--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个class: Analysis, PYZ, EXE和COLLECT.

要看更详细的文档在
https://pyinstaller.readthedocs.io/en/stable/usage.html#general-options
https://pyinstaller.readthedocs.io/en/stable/spec-files.html

windows打包

windows下要加--path参数来找到pyqt的dll

pyinstaller --path C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\Lib\site-packages\PyQt5\Qt\bin -y -w hello.py
上一篇下一篇

猜你喜欢

热点阅读