Python package to generate exe executable file

The official account backstage replied "Books" to learn more about the contents of the new book of the account owner

Author: ZHU Xiao five

Source: Bump Data

Standard packaging

At present, the more common packaging exe method is Pyinstallerrealized through, and 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.

Then if we write a small script for data analysis/automation 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)

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 no repetitive notes will be made.

 
  1. pip install -i https://pypi.douban.com/simple/ pyinstaller #豆瓣源

  2. pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller #清华源

Pyinstaller packaging steps

Here we take the previous " Python Automated Office | Colleagues asked me to help make up 178 Word daily newspapers! 》This python code is used as a case to demonstrate. Put the script py_word.py, the table file to be processed workbook.xlsx, and the prepared software icon picture chengzi.icoin the F:\py_worddirectory of my computer (if you are interested, you can download it at the end of the article)

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.

A named py_wordexe application has been generated , and the icon is also the orange pattern we set. It seems that it has been half successful.

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

Execute after packaging the exe

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

Detailed explanation of Pyinstaller parameters

Speaking of the command just executed

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

Explain to you the parameters of Pyinstaller, where the -Fparameters represent making independent executable programs.

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

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

Finally, a little summary:

 
  1. Pyinstaller -F py_word.py 打包exe

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

  4.  
  5. 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

ico image generation

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

One is that you can 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, you can convert pictures in other formats into ico format: https://app.xunjiepdf.com/img2icon/

ico image format conversion

Compress and pack

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 posted on WeChat.

I have also tried many methods, such as: modifying the spec file to customize 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 too large to be packaged?

The 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 is because "There are many libraries built in Anaconda, and many unnecessary modules are packaged when packaging, and 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

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

  2.  
  3. conda activate 虚拟环境名字  #激活虚拟环境

  4.  
  5. conda deactivate  #退出虚拟环境

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

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

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

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

Install required libraries

Above we have created and activated the aotuvirtual environment named virtual environment, input conda listcan view the libraries that have been installed in the current virtual environment.

We opened the Python script to be packaged, compared the above figure, and found pandasthat docxthese two libraries still need to be installed additionally. Of course, the necessary pyinstallerlibraries for packaging are also indispensable .

Script to be packaged

The process of installing the library will not be repeated

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

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

  4.  
  5. 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):

 
  1. #创建虚拟环境

  2. conda create -n aotu python=3.6

  3.  
  4. #激活虚拟环境

  5. conda activate aotu

  6.  
  7. #Pyinstaller打包

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

Summarize some small pits

1. Speaking of it is still a bit metaphysical, I have performed exactly the same process above on both computers. It is displayed on one of them that xlrdthe library is missing , and it is successfully packaged after installation, and it is also 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, the library is not pip install docxrequired, but required

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. If the operation is correct, then pack it, which is safer.

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

conda remove -n aotu--all 

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

Download link

If you want to test Python packaging and you don’t have the appropriate files at hand, you can reply to " Package " in the backstage of the official account to get the following files:

ps:

The following problems may exist in the windows environment, that is, an error will be reported when running the packaging command, RecursionError: maximum recursion depth exceeded, as shown in the following figure:

The solution is as follows:

After pyinstaller, it will generate a xxx.spec file paired with the xxx.py file , as shown below

Open the xxx.spec file, import the sys package at the beginning of the line, and then set the limit of recursive calls, which can be as large as possible. After I set it here 1 million times, no error is reported, as shown in the figure below.

 

After the modification, then   pyinstall -F xxx.spec  (the file you just modified) will do, the --add-data parameter is no longer needed, and the spec file already exists.

 

Then solved 

Guess you like

Origin blog.csdn.net/weixin_42575020/article/details/113178570