您的位置:首页 > 编程语言 > Qt开发

PyQt5+QtDesigner编写摄像头界面程序(五)——用pyinstaller将py文件打包成exe

2019-01-24 10:49 906 查看

上一节,我们已经完成了摄像头界面程序的编写,点击运行程序后就可以看到正常工作的界面程序了,具备了实时显示摄像头采集到的图像,调整摄像头的亮度、增益等参数,改变图像的颜色、保存截图或录像等功能。然而,由于程序运行时会用到python、opencv、pyqt5等可能是我们自己电脑上独有的工具,一旦程序移植到别的电脑上就无法运行了。为了让我们编写的程序在别的电脑上也能正常运行,我们就需要将程序打包成exe程序。本节将介绍使用pyinstaller来打包我们的python程序。
第一节里我们已经介绍过pyinstaller的安装方法了。方法很简单,如果我们已经安装好python和pip工具了,只要在cmd命令窗口输入

pip install pyinstaller
即可自动完成安装。
安装完成后,同样在cmd窗口中,使用cd命令进入到我们编写的主程序文件所在的文件夹。这里,我文件的路径为D:\Python\PyQtTest,主程序的文件名为QiUi.py,因此在命令窗口中输入命令
pyinstaller -F -w QtUi.py
,如下图所示,打包程序开始自动运行。

其中pyinstaller的部分参数含义如下表所示,本例中
pyinstaller -F -w QtUi.py
的意思是将QtUi.py文件打包成一个单独的exe文件,且程序运行时不使用cmd窗口。

参数 含义
-F 只生成一个exe文件
-distpath 指定生成exe存放的目录
-workpath 指定编译过程中临时文件的存放目录
-D 创建一个目录,包含exe文件和依赖文件
-i 指定exe图标
-p 指定exe依赖的包、模块
-clean 清理编译时的临时文件
-c 使用控制台
-w 使用窗口

我在打包程序的过程中,产生了很多的warning,warning的内容如下所示,提示找不到相应的函数库。

101524 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of C:\users\chengjingxin\appdata\local\programs\python\python36\lib\site-packages\PyQt5\Qt\plugins\platforms\qwindows.dll
102471 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of C:\users\chengjingxin\appdata\local\programs\python\python36\lib\site-packages\PyQt5\Qt\plugins\platforms\qwindows.dll
103298 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of C:\users\chengjingxin\appdata\local\programs\python\python36\lib\site-packages\PyQt5\Qt\plugins\platforms\qwindows.dll
104276 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of C:\users\chengjingxin\appdata\local\programs\python\python36\lib\site-packages\PyQt5\Qt\plugins\platforms\qwindows.dll
105170 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of C:\users\chengjingxin\appdata\local\programs\python\python36\lib\site-packages\PyQt5\Qt\plugins\platforms\qwindows.dll
105986 WARNING: lib not found: api-ms-win-crt-utility-l1-1-0.dll dependency of C:\users\chengjingxin\appdata\local\programs\python\python36\lib\site-packages\PyQt5\Qt\plugins\platforms\qwindows.dll
106972 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of C:\users\chengjingxin\appdata\local\programs\python\python36\lib\site-packages\PyQt5\Qt\plugins\platforms\qwindows.dll

通过网上查找原因发现,这是由于pyinstaller的搜索路径没有包括这些函数库所在的路径。解决办法是手动添加这些库的路径,如下图所示,运行过一次上述pyinstaller命令后,在程序所在的文件夹中会生成一个QtUi.spec文件,使用记事本打开该文件,将路径

C:\Windows\SysWOW64\downlevel
C:\Windows\System32\downlevel
添加到pathex中,如下图所示。

保存后,在cmd窗口中在此运行
pyinstaller -F -w QtUi.py
命令,可以发现那些warning没有了,cmd命令显示打包成功。然后再程序所在的文件夹中会生成dist、build等新的文件夹,在dist文件夹里就是我们打包好的exe文件了。至此我们的摄像头控制界面程序就大功告成啦。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: