【Pyqt打包】Windows下使用Pyinstaller打包pyqt程序遇到的问题记录

1、打包报错

ValueError: API 'QString' has already been set to version 1

解决办法:在文件pyi_rth_qt4plugins.py添加版本设置代码即可。 PyQt4 supports two different APIs: version 1 (default on Python 2) and version 2 (default on Python 3). It is possible to change API at runtime through the sip.setapi call; a good example is that someone wants to write API v2 code on Python 2 to be future-proof and ready for the migration to Python 3.
Alas, changing API versions does not currently work with PyInstaller, and leads to error messages such as:
ValueError: API 'QString' has already been set to version 1
Workaround for Development version
In the development version of PyInstaller a good workaround is to create a runtime hook file that is executed before the runtime hook support/rthooks/pyi_rth_qt4plugins.py in PyInstaller, and add the API changing code at the top of your custom runtime hook. Let's name the file rthook_pyqt4.py.
Then add there code:
import sipsip.setapi(u'QDate', 2) sip.setapi(u'QDateTime', 2) sip.setapi(u'QString', 2) sip.setapi(u'QTextStream', 2) sip.setapi(u'QTime', 2) sip.setapi(u'QUrl', 2) sip.setapi(u'QVariant', 2) Then rebuild your application with the --runtime-hook option pyinstaller.py --runtime-hook rthook_pyqt4.py main_script.pyor add runtime_hooks argument to the Analysis object in your .spec file a = Analysis(['main_script.py'], pathex=[], hiddenimports=[], hookspath=None, runtime_hooks=['rthook_pyqt4.py']) and it should work with PyInstaller.

2、部分PC运行图片无法显示
【【Pyqt打包】Windows下使用Pyinstaller打包pyqt程序遇到的问题记录】将所有Py文件均加入spec打包脚本中,这样会导致打包后文件大小增加一倍左右
如果依然无法显示,依次尝试以下方案,基本都能搞定
A、Pyinstaller不会将windows运行库自动打包,可手动安装一次运行库
B、不打包为单个exe
3、打包常用命令
A、使用-key加密
B、使用upx压缩
C、使用虚拟环境
pyinstaller **.py pyinstaller **.spec pyinstaller --key 0123456789abcdef -F -w -i ../../images/**.ico **.py pyi-makespec --key 0123456789abcdef -F -w -i ../../images/**.ico **.py

    推荐阅读