PyQt5用pyinstaller打包成桌面GUI
环境:OS 10.13,python3.7,PyQt5
之前使用的是py2app,一个是打包之后文件太大,有230M,另外在运行时会出现这种错误:
LSOpenURLsWithRole() failed for the application /Volumes/Disk1/编程练习/GUI测试/dist/NameDesign.app with error -10810.
应该是xcode的签名问题。暂时未解决。
关键语句:生成配置文件
import os
import os.path
def code2exe(pyfile):
cmd = '/LiManing/Library/Python/3.7/bin/py2applet --make-setup {pyfile}'.format(pyfile=pyfile)
os.system(cmd)
if __name__ == "__main__":
code2exe('QtTest.py')
执行后,会生成setup.py,即配置文件:
from setuptools import setup
APP = ['QtTest.py']
APP_NAME = 'test'
DATA_FILES = []
OPTIONS = {
'argv_emulation' : True,
'iconfile':'icon.icns', #应用图标
}
setup(
app=APP,
name=APP_NAME,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
再进行打包,之前的cmd部分,替换成:
cmd = 'python3 setup.py py2app' #python3.7 setup.py py2app -A 加了-A表示利用本机库,不打包依赖库
实际测试中,加了-A能够正常运行,而且包超级小,只有23k,写自用工具还是不错的。
如果其他机器也用,推荐pyinstaller,打包简单,体积小,运行正常。
因为环境是python3.7,所以在终端 pip3 install pyinstaller。注意pyinstaller用全路径,之后在python中调用终端:
import os
import os.path
def code2exe():
cmd1 = '/LiManing/Library/Python/3.7/bin/pyinstaller --windowed --onefile --icon=icon.icns --clean --noconfirm QtTest.py' #生成配置文件
cmd2 = '/LiManing/Library/Python/3.7/bin/pyinstaller --clean --noconfirm --windowed --onefile QtTest.spec'
#对配置文件进行打包
os.system(cmd1)
os.system(cmd2)
if __name__ == "__main__":
code2exe()
生成的配置文件如下:
# -*- mode: python -*-
block_cipher = None
a = Analysis(['QtTest.py'],
pathex=['/Volumes/Disk1/编程练习/GUI测试'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='QtTest',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=False , icon='icon.icns')
app = BUNDLE(exe,
name='QtTest.app',
icon='icon.icns',
bundle_identifier=None)
生成的dist包中,执行app:
之后可以用磁盘工具--文件--新建映像--来自文件夹的映像,将dist文件夹打包为dmg格式。
双击dmg,即可运行:
参考资料:https://blog.csdn.net/hhhhhhxh/article/details/79811004
https://blog.csdn.net/panna_linda/article/details/78644345