How to package python script into exe with more than 10 MB?

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only. They do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it yourself

Python free learning materials and group communication answers Click to join


Standard packaging

At present, the more common packaging exe method is implemented through Pyinstaller, this article will also use this conventional method. If you are familiar with this area, you can slide directly to the second half of this article.

Why package?

As we all know, Python scripts cannot be run on machines without Python installed.

So if we wrote a small script for data analysis/automated office, we want to share it with my colleagues, but her computer does not have Python installed.

At this time, if you package the script into an exe file and send it to her on WeChat, even if the Python interpreter is not installed on her computer, the exe program can run on it. Isn't it beautiful?

 

(Of course, if you want to build a relationship with Miss Sister by helping to install Python, just treat it as I didn't say it)

(Of course, if you want to build a relationship with Miss Sister by helping to install Python, just treat it as I didn't say it)

Install Pyinstaller

First of all, we have to install Pyinstaller first, and use the pip command directly in cmd

pip install pyinstaller 

If the network speed is too slow, you can switch to domestic sources to speed up. The other parts of this article will directly use domestic sources and will not repeat the comments.

 

pip install -i https://pypi.douban.com/simple/ pyinstaller #豆瓣源
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller #清华源

Pyinstaller packaging steps

Put the script py_word.py, the to-be-processed form file workbook.xlsx, and the prepared software icon picture chengzi.ico in the F:\py_word directory of my computer

 

 

1. CMD switches to the directory where we just put the file

 


2. Execute the command Pyinstaller -F -w -i chengzi.ico py_word.py, the execution process is very long, and there is no moving picture.
After execution, you will find that the current directory has a few more folders, open the folder named dist among them.

 

An exe application named py_word has been generated, and the icon is also the orange pattern we set, which seems to be half the success.

How about running it, try to generate the word daily report normally?

 

Execute after packaging exe

Execute after packaging exe

 

It runs successfully, but the file is a bit big (339M)

 

Pyinstaller parameter details

Speaking of the command just executed

Pyinstaller -F -w -i chengzi.ico py_word.py

Let me explain the parameters of Pyinstaller. The -F parameter stands for making an independent executable program.

-w means that the command line will not be opened when the program starts. If you don't add the -w parameter, there will be a black hole console window. For example, in the script just now I added a line of print('Hello World!'), then don't put the -w parameter, otherwise an error will be reported when running, after all, Hello World! needs to be printed on the command line. In addition, the -w parameter is very useful in the GUI interface.

The last -i chengzi.ico refers to setting your own icon pattern, because the default packaged picture is like the following picture. This parameter can also be written as --icon=chengzi.ico

 

 

Finally, a little summary:

Pyinstaller -F py_word.py 打包exe

Pyinstaller -F -w py_word.py 不带控制台的打包

Pyinstaller -F -w -i chengzi.ico py_word.py 打包指定exe图标打包

The above three are more commonly used parameters, other parameters are detailed in the table below

 

Pyinstaller parameter list

Pyinstaller parameter list

ico image generation

I like to put my own icons on the software I make, but where are so many ico pictures?

One is to find a dedicated ico picture website, but they are very small and the picture library is also very small.

The other is that you can generate it yourself, here is a website for you to share pictures in other formats into ico format: https://app.xunjiepdf.com/img2icon/

ico image format conversion

ico image format conversion

 

Compressed

Okay, friends

At the most exciting moment, the exe just generated is too big, and it takes a lot of effort for a software program of more than 300M to be transmitted via WeChat.

I have also tried many methods, such as: modifying the spec file custom packaging, pipenv virtual environment, using open source upx compression, etc., but often the process is more troublesome, or the success rate is not high (the compression is unsuccessful, all depends on the face).

And what I want to share is the simplest and most successful method I have been using-conda to create a virtual environment.

 

Why is Python packaging big?

Before compressing and packaging, let's briefly talk about why Python is packaged too large?

Python packaged exe is not only large in size but also extremely slow. Interpreted languages ​​are mostly like this, but Python is particularly prominent. To solve the problem of large and slow problems, only compiled languages ​​such as C, C++, and even VB are much better. The smallest size is assembly. [1]

In addition, Zhihu said that it was because "There are many libraries built in Anaconda, and many unnecessary modules are packaged when packaging. They must be packaged with pure Python."

So we can simulate a new environment in which only the toolkits necessary for our packaging are installed.

The most suitable one is the virtual environment!

Virtual environment

There are many ways to create virtual environments in Python, and I am a loyal user of Anaconda. If you are like me, it is simple. (You can also use Virtualenv and Pipenv to set up a virtual environment, and make good use of search, the methods are similar.)

Remember a few commands first, it's very simple

conda create -n 虚拟环境名字 python==3.6  #创建虚拟环境

conda activate 虚拟环境名字  #激活虚拟环境

conda deactivate  #退出虚拟环境

Run "Anaconda Prompt" from the start menu, and enter the instructions for creating a virtual environment on the interface that appears. A virtual environment named aotu and based on python version 3.6 was successfully created.

 

 

Need to reply (y/n) during the creation process, Yes, then activate the virtual environment

 


The virtual environment installed by conda will generate the virtual environment directory under the env directory under the anaconda installation directory.

 

Of course, we can also use the command conda info --envs in the window just now to view all the virtual environments in the conda environment

Install required libraries

We have created and activated the virtual environment named aotu above. Enter conda list to view the libraries installed in the current virtual environment.

 

 

We opened the Python script to be packaged, compared the above picture, found that pandas, docx these two libraries need to be installed additionally. Of course, the essential pyinstaller library for packaging is also indispensable.

 

Script to be packaged

Script to be packaged


The process of installing the library will not be repeated

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pandas

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple python-docx

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller

Look after installation

 

 

It has been successfully installed, and there are some that are installed with these libraries, so ignore them.

Pyinstaller packaging steps

I won’t repeat it here, just try another apple icon

Pyinstaller -F -w -i apple.ico py_word.py

 

Generate

 

 

Successfully compressed to 29.8M, if you don’t import pandas, it should be more than 10M

Run it, no problem

 

 

Finally, briefly summarize the whole process of virtual environment + packaging (only three steps):

#创建虚拟环境
conda create -n aotu python=3.6

#激活虚拟环境
conda activate aotu

#Pyinstaller打包
Pyinstaller -F -w -i apple.ico py_word.py

Sum up some small pits

1. Speaking of it is still a bit metaphysical, I have performed the exact same process above on both computers. One of them shows that the library is missing xlrd, and it is packaged successfully after installation, and it is the same size. Friends can get the file at the end of the article, and try it too.

2. When installing the library, pay attention to some library names, such as docx, do not pip install docx, but need

pip install python-docx

There are also some libraries that may not be available due to different versions. Just bump into the wall.
3. In order to prevent some libraries from not being installed when packaging, you can execute the Python script in the virtual environment first. Pack it if it runs correctly, which is safer.

 

 

4. Finally, if you want to delete the virtual environment, execute the following command

conda remove -n aotu--all 

5. If packaged as exe, try to choose python3.6+32-bit version, because win64-bit system is backward compatible with 32-bit programs, but if you don’t consider 32-bit system, it doesn’t matter. You can directly package the python64-bit version directly, but only Can run on win64 system.

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/112986712