pyinstaller打包闪退处理办法

本文最后更新于:7 个月前

1 故事引言

我们如果写了一个 Python程序,比如 hello.py ,然后发给女神,让女神看看效果,可是,女神双击了你的文件,结果啥也没
有,你是不是觉得很尴尬?从专业的角度来说,就是女神的电脑缺少了运行环境,而我们的Python 如何才能让女神不用安装什么
其它东西,直接双击就能运行呢?这就需要我们本次的主角了

2 安装 Pyinstaller
1
2
pip install -i https://pypi.doubanio.com/simple/  pyinstaller

20220628230546

3 使用

*如下:

20220628230247

1
2
3
单文件打包:
pyinstaller -F 9_函数.py

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
E:\Cgrain\AdvancedTraining\21_1_初识Py>pyinstaller -F 9_函数.py 
79 INFO: PyInstaller: 5.1
80 INFO: Python: 3.9.8
104 INFO: Platform: Windows-10-10.0.19042
... 省列...
'F:\\Python\\lib\\site-packages\\PyInstaller\\hooks'...
6404 INFO: Loading module hook 'hook-encodings.py' from 'F:\\Python\\lib\\site-packages\\PyInstaller\\hooks'...
6726 INFO: Loading module hook 'hook-heapq.py' from 'F:\\Python\\lib\\site-packages\\PyInstaller\\hooks'.
... 省列...
6935 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'F:\\Python\\lib\\site-packages\\PyInstaller\\hooks'...
6937 INFO: Loading module hook 'hook-xml.py' from 'F:\\Python\\lib\\site-packages\\PyInstaller\\hooks'...
7056 INFO: Loading module hook 'hook-_tkinter.py' from 'F:\\Python\\lib\\site-packages\\PyInstaller\\hooks'...
7314 INFO: checking Tree
7314 INFO: Building Tree because Tree-00.toc is non existent
7314 INFO: Building Tree Tree-00.toc

... 省列...
7624 INFO: Looking for ctypes DLLs
7656 INFO: Analyzing run-time hooks ...
7661 INFO: Including run-time hook 'F:\\Python\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_subprocess.py'
7674 INFO: Looking for dynamic libraries
8286 INFO: Looking for eggs
8287 INFO: Using Python library F:\Python\python39.dll

10493 INFO: Writing RT_ICON 6 resource with 4264 bytes
10495 INFO: Writing RT_ICON 7 resource with 1128 bytes
10500 INFO: Copying 0 resources to EXE
10502 INFO: Embedding manifest in EXE
10504 INFO: Updating manifest in E:\Cgrain\AdvancedTraining\21_1_初识Py\dist\9_函数.exe.notanexecutable
10506 INFO: Updating resource type 24 name 1 language 0
10512 INFO: Appending PKG archive to EXE
12460 INFO: Building EXE from EXE-00.toc completed successfully.

生成了两个文件夹

20220628231252

打开dist 文件

双击,然后就会发现一闪而过

我们需要打开cmd 然后再cmd 运行,如下
20220628231507

ps 如果我们需要双击的时候打开,你只需要在python 源文件最后面加一个 input(“”) 这样他就会一直打开了

1
2
3
多文件打包:
pyinstaller - 文件

** 注意,多文件打包必须要有一个主文件(启动文件)**

下面我们来进行多文件打包
20220628231745

可以明显的看出,我们的主文件是app.py

我们进行 打包

1
pyinstaller -D app.py

Log

1
2
3
4
5
6
7
8
9
10
11
12
E:\Cgrain\AdvancedTraining\Request1688>pyinstaller -D app.py                                          
127 INFO: PyInstaller: 5.1
127 INFO: Python: 3.9.8
157 INFO: Platform: Windows-10-10.0.19042

...
10920 INFO: Building EXE from EXE-00.toc completed successfully.
10925 INFO: checking COLLECT
10926 INFO: Building COLLECT because COLLECT-00.toc is non existent
10927 INFO: Building COLLECT COLLECT-00.toc
11281 INFO: Building COLLECT COLLECT-00.toc completed successfully.

20220628232039

可以看出,也生成了两个文件夹,我们打开dist文件夹

一直进去,最后发现
20220628232128

双击发现,一闪而过,关于使用pyinstaller 打包完成之后的问题,没有什么好的办法能够制止他一闪而过,你在项目input 了他也无效,经过测试,发现了一个很好的处理方法,不在用单身二十多年的手速来进行快速截图定位错误了

处理办法,直接在项目cmd 运行,然后输入app.exe,如下
20220628232332

这样子不管怎么报错,他都不会自动关闭了,定位问题也变得简单了
好了,今天的文章就写在这里了,如果有什么不会的可以在下面留言,谢谢大家


pyinstaller打包闪退处理办法
http://example.com/2022/06/28/pyinstaller打包闪退处理办法/
作者
Ray
发布于
2022年6月28日星期二晚上10点39分
更新于
2022年6月28日星期二晚上11点25分
许可协议