Python packaged EXE python encounter a variety of pit exit the program method

Ado, anyway, I'm not successful, but I record what pit encountered!

1: Install the relevant library is too slow

Solution: offline installation

In a lot of tutorial, you want to install several libraries, but some libraries use pip install on-line has been stuck (see the progress bar when the card will go die), this problem can be solved using an offline installation, attached below solve process!

Installation error (in fact, is too slow, I am forced to stop)

$ pip install pywin32
Collecting pywin32
  Downloading https://files.pythonhosted.org/packages/a3/8a/eada1e7990202cd27e58eca2a278c344fef190759bbdc8f8f0eb6abeca9c/pywin32-224-cp37-cp37m-win_amd64.whl (9.0MB)
ERROR: Operation cancelled by user

  

Solutions are as follows:

First, find the download link this library, here is this:

https://files.pythonhosted.org/packages/a3/8a/eada1e7990202cd27e58eca2a278c344fef190759bbdc8f8f0eb6abeca9c/pywin32-224-cp37-cp37m-win_amd64.whl

 

Then use other HTTP download tool, I use the IDM, you can also use your browser!

Then open storage directory,

Here then open the command line,

pip install pywin32-224-cp37-cp37m-win_amd64.whl

  

Because I have installed, was not shot, I do not bother uninstalled, anyway, is connected to the back of the library on the line (after entering the first letter of the file name, press the TAB key to completion)

Then here the first pit is solved, this method is suitable for installation in all libraries (official or slow when you need to install third-party libraries)! !

第二个坑,调试没问题,运行就报错(代码错误)

在这里我确定使用py文件运行是没问题的,但是打包之后却提示我没有定义exit变量/函数(exit是系统变量)

对于这个问题我暂时的解决办法有两个,要么舍弃这个退出功能,否则无法打包,要么定义这个函数!

我的退出原代码为,这样子是打包不了的,至少我打包的时候是这样

bt_exit = tk.Button(win, text="更新软件", font=("宋体", 15), command=exit).place(x=500, y=250) 

  

可以参考这个方法---- python 退出程序方法

怎么去定义我就不细说了,能写出程序的人看了这个方法都会懂的

那么第二个坑就解决了(虽然我发现定义了之后点击退出却没有反应,这个可以点X关闭,所以我就先不管了)

第三个坑:运行提示Failed to execute script main

首先,执行构建命令:

pyinstaller -F 主程序.py

  

然后查看文件列表

将配置文件(背景文件)放在一起!

最后运行程序!----------------------居然不报错了!

​其实呢,在我之前的构建中是报错的,错误提示是

Failed to execute script 

  

在这里我也顺便说一下理论能解决这个错误的方法

首先确保所需文件都在你写的相对目录里,例如我这个程序调用的背景写的是

impath = 'timg.jpg'
img = Image.open(impath)

  

所以这个timg.jpg是跟主程序在同一级目录的,这时候你就需要把这些调用的文件放在所在的目录,然后在构建文件编辑一下,方法如下:

找到这个.spec文件,使用文本编辑(在这里我用vim工具),下面第二张图(VIM);

windows用户建议使用Notepad++,下面第三张图(Notepad++)

反正就是在.spec里面修改一下参数,首先找到

a = Analysis

  

然后找到参数datas,在后面加上你需要调用的文件名(不在同级目录的要写路径加文件名),例如我这里要调用的是timg.jpg文件,而这个文件和程序在同一目录,那么参数就是
datas=['timg.jpg'],

  

假如我的代码写的是程序目录下的bg目录下的timg.jpg文件,那么参数就要写成
datas=['bg\timg.jpg'],

  

为避免出现各种错误,如果是小的程序,建议全部调用文件放在程序根目录!那么到这里也就成功一半了(总不能把程序发给别人的时候还要带上配置文件吧)!

接下来继续压缩!

在打包程序的时候是不会打包图片文件的,但是我们可以把图片文件转为py文件,所以解决这个背景问题的关键点就是这个了,下面附上一些别人的教程!

Pyinstaller 使用+打包图片方法

下面是我的打包记录。

首先安装相关库

pip install img
pip install base64

  

然后新建一个py文件,内容如下
import base64
bg = open("timg.jpg", "rb")
base_data = base64.b64decode(bg.read())
bg.close()
write_data = "img = '%s'" % base_data
f = open("bg.py", "w+")
f.write(write_data)
f.close()

  

上面的代码需要注意的就是变量,变量解释如下
"timg.jpg" #需要转换的图片文件
"bg.py" #要生成的图片PY文件

  

运行之后得到下面的数据----很长很长的,如果你得到的是很短的,那就考虑一下是不是图片原文件出问题了,还有一个更快的方法,在线转换

http://imgbase64.duoshitong.com/

然后得到这个开头的编码
data:image/jpeg;

  

转换之后就需要调用,调用方法如下

首先需要导入base64库以及OS库

所以需要添加的代码有两部分,第一部分是库导入

import base64
import os

  

第二部分则是创建临时文件的代码
strs = '''...............//Z\n\n\n\n\n \n\n\n'''
imgdata = base64.b64decode(strs)
file = open('timg.jpg', 'wb')
file.write(imgdata)
file.close()

  

上面的strs中,填写的方法为在下面这段代码中插入从网页获取到的图片转base4的加密信息
os Import, Base64 
strs = '' 'coded pictures up here, other do not move \ the n-\ the n-\ the n-\ the n-\ the n-\ the n-\ the n-\ the n-' '' 
imgdata = base64.b64decode (strs) 
File = Open ( '1 .jpg ',' WB ') 
a file.write (imgdata) 
File.close ()

  

The above code, 1.jpg represents a new generation of image text, this should be changed according to your actual needs

Well, here disguised to solve the problem of the image files can not be packaged, then reseal the program to see

 

Guess you like

Origin www.cnblogs.com/liuyi778/p/11462967.html