PyQT5 GUI 打包器 fbs fbs打包是有软件版本需求的:
Python 3.6
PyQT 5.9.2
PyInstaller 3.4
pywin32 (Windows only)
如果使用matplotlib的话,版本只能是 3.2.2:
0. 安装必须的库文件(Windows 10 only)
64Bit: Microsoft Visual C++ 2010 SP1 Redistributable Package (x64)https://www.microsoft.com/en-us/download/details.aspx?id=13523
64Bit: Visual C++ Redistributable for Visual Studio 2015https://www.microsoft.com/en-us/download/details.aspx?id=48145
Visual C++ Redistributable for Visual Studio 2012https://www.microsoft.com/en-us/download/details.aspx?id=30679
1. 创建虚拟环境 注意:必须使用virtualenv , Python自带的venv打包会出错
1 python -m virtualenv venv
2. 进入虚拟环境,安装依赖包 1 2 venv\Scripts\activate.bat venv> pip install pyqt5==5.9.2 fbs qtawesome
3. 使用fbs运行:
4. 使用fbs打包成免安装文件:
打包后的文件在 target 目录中
5.Windows下的注意事项 注意:如果提示缺少dll错误,需要下载 api-ms-win-crt-multibyte-l1-1-0.dll 放到 PATH目录下重新打包 在Windows下可能会遇到:No module named ‘win32com’ 这时还需要额外安装: pywin32 冻结版本号:
1 pip freeze > requirements.txt
批量安装:
1 pip install -r requirements.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 (env) C:\Users\kxin\Documents\code\python\easy-quiz-suite>pip list Package Version --------------- --------- altgraph 0.17 certifi 2020.6.20 cycler 0.10.0 fbs 0.9.0 future 0.18.2 kiwisolver 1.2.0 macholib 1.14 matplotlib 3.2.2 numpy 1.19.2 pefile 2019.4.18 Pillow 7.2.0 pip 20.2.2 PyInstaller 3.4 pyparsing 2.4.7 PyQt5 5.9.2 python-dateutil 2.8.1 pywin32 228 pywin32-ctypes 0.2.0 QtAwesome 0.7.3 QtPy 1.9.0 setuptools 49.6.0 sip 4.19.8 six 1.15.0 wheel 0.35.1
6. MacOS下的注意事项 Mac下不需要按照 pywin32。
在Mac下如果打包带有 matpotlib的依赖的话,可能会遇到:Failed to execute script pyi_rth__tkinter 错误。 解决方法是修改文件 vi env/lib/python3.6/site-packages/PyInstaller/hooks/hook-_tkinter.py,加入 and ‘Python’ not in path_to_tcl 判断条件
1 2 3 4 5 6 7 PyInstaller/hooks/hook-_tkinter.py @@ -180,7 +180,7 @@ def _find_tcl_tk(hook_api): 180 181 # _tkinter depends on Tcl/Tk compiled as frameworks. 182 path_to_tcl = bins[0][1] - if 'Library/Frameworks' in path_to_tcl: 183 + if 'Library/Frameworks' in path_to_tcl and 'Python' not in path_to_tcl:
Mac下包版本如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 (env ) [kxin@38f9d358ed4c][~/Documents/code/python/easy-quiz-suite]% pip list Package Version --------------- --------- altgraph 0.17 cycler 0.10.0 fbs 0.9.0 future 0.18.2 kiwisolver 1.2.0 macholib 1.14 matplotlib 3.2.2 numpy 1.19.2 pefile 2019.4.18 pip 20.2.2 PyInstaller 3.4 pyparsing 2.4.7 PyQt5 5.9.2 python-dateutil 2.8.1 QtAwesome 0.7.3 QtPy 1.9.0 setuptools 49.6.0 sip 4.19.8 six 1.15.0 wheel 0.35.1
附录: Windows下使用Python自带的 venv 打包 安装必须的库文件:
64Bit: Microsoft Visual C++ 2010 SP1 Redistributable Package (x64)https://www.microsoft.com/en-us/download/details.aspx?id=13523
64Bit: Visual C++ Redistributable for Visual Studio 2015https://www.microsoft.com/en-us/download/details.aspx?id=48145
Visual C++ Redistributable for Visual Studio 2012https://www.microsoft.com/en-us/download/details.aspx?id=30679
创建虚拟环境 1 2 3 4 5 6 7 8 9 10 11 python -m venv env env \Scripts\activate(env ) C:\Users\kxin\PycharmProjects\eqs>pip list Package Version ---------- ------- pip 18.1 setuptools 40.6.2 You are using pip version 18.1, however version 20.2.3 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command . (env ) C:\Users\kxin\PycharmProjects\eqs>
安装依赖包: 1 pip install pyqt5==5.9.2 fbs qtawesome matplotlib==3.2.2 pywin32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 (env ) C:\Users\kxin\PycharmProjects\eqs>pip list Package Version --------------- --------- altgraph 0.17 cycler 0.10.0 fbs 0.9.0 future 0.18.2 kiwisolver 1.2.0 macholib 1.14 matplotlib 3.2.2 numpy 1.19.2 pefile 2019.4.18 pip 18.1 PyInstaller 3.4 pyparsing 2.4.7 PyQt5 5.9.2 python-dateutil 2.8.1 pywin32 228 pywin32-ctypes 0.2.0 QtAwesome 1.0.1 QtPy 1.9.0 setuptools 40.6.2 sip 4.19.8 six 1.15.0
创建fbs工程: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 (env ) C:\Users\kxin\PycharmProjects\eqs>fbs startproject App name [MyApp] : EasyQuizSuite Author [Kxin] : Kxin Mac bundle identifier (eg. com.kxin.easyquizsuite, optional): Created the src/ directory. If you have PyQt5 installed, you can now do : fbs run (env ) C:\Users\kxin\PycharmProjects\eqs>fbs run (env ) C:\Users\kxin\PycharmProjects\eqs>
工程结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 (env ) C:\Users\kxin\PycharmProjects\eqs>tree /F src Folder PATH listing Volume serial number is E2AC-4C65 C:\USERS\KXIN\PYCHARMPROJECTS\EQS\SRC ├───build │ └───settings │ base.json │ linux.json │ mac.json │ └───main ├───icons ... │ ├───python │ └───eqs │ main.pro │ main.py │ ui_utils.py │ zh_CN.ts │ __init__.py │ └───resources └───base ├───fonts │ wqy-microhei.ttc │ └───locale zh_CN.qm (env ) C:\Users\kxin\PycharmProjects\eqs>
在系统中搜索api-ms-win-crt-multibyte-l1-1-0.dll
, 将包含该文件的文件夹 C:\Windows\SysWOW64\downlevel
加入到path中,然后开始打包,并试运行:
1 2 3 4 5 6 7 (env ) C:\Users\kxin\PycharmProjects\eqs>set path=C:\Windows\SysWOW64\downlevel;%path% (env ) C:\Users\kxin\PycharmProjects\eqs>fbs freeze Done. You can now run `target\EasyQuizSuite\EasyQuizSuite.exe`. If that doesn't work, see https://build-system.fman.io/troubleshooting. (env) C:\Users\kxin\PycharmProjects\eqs>target\EasyQuizSuite\EasyQuizSuite.exe