The following article is from bump data, by Zhu Xiaowu

Hi, I’m an early riser.

Readers have been asking questions in the background about packing Python scripts into exe.

Today I’ll give you a comprehensive summary of how Python is packaged as an EXE, and how to make it small enough.

Standard packing

At present, the most common method of packaging exe is through Pyinstaller to achieve, this article will also use this general method. If you are already familiar with this section, you can skip to the second half of this article.

Why pack?

As we all know, Python scripts cannot run on machines that do not have Python installed.

Let’s say we write a data analysis/office automation script and want to share it with our colleague’s sister, but she doesn’t have Python installed on her computer.

At this time, if you package the script into an EXE file and send it to her via wechat, the EXE program can run on it even if she does not have a Python interpreter installed on her computer. Isn’t it beautiful?

(Of course, if you want to build a bond with your sister by installing Python, forget it.)

Install Pyinstaller

First we need to install the Pyinstaller and use the PIP command directly from CMD

pip install pyinstaller 
Copy the code

If the network speed is too slow, you can switch to the domestic source to speed up. The rest of this article uses the domestic source directly.

PIP install -i https://pypi.douban.com/simple/ # pyinstaller douban source PIP install -i https://pypi.tuna.tsinghua.edu.cn/simple Pyinstaller #Copy the code

Pyinstaller package steps

Here before we get the Python office automation | colleagues want me to help write up 178 Word daily!” This Python code is used as an example. Py, workbook.xlsx, and chengzi.ico in the F:\py_word directory on my computer (you can download it at the end if you are interested).

1, CMD switch to the directory where we just put the files

2, Run Pyinstaller -f -w -I chengzi.ico py_word.py, the execution process is very long, no record GIF.

After executing, you will notice that there are several additional folders in the current directory. Open the folder named dist.

An exe application named Py_word has been generated, and the icon is the orange we set up, so it looks like we are halfway there.

How about running it to see if it can generate word daily?

Package the EXE and run it

Run successfully, but the file is a bit too big (339M)

Pyinstaller parameter description

Back to the command we just executed

Pyinstaller -F -w -i chengzi.ico py_word.py
Copy the code

The -f parameter represents creating a standalone executable program.

-w indicates that the command line is not opened when the program is started. If the -w parameter is not added, a black console window appears. For example, in the script I just added a line print(‘Hello World! ‘), then do not put the -w argument, otherwise the runtime error, after all, Hello World! It needs to be printed on the command line. In addition, the -w parameter is very useful in GUI interface.

Finally, I chengzi.ico means to set your own icon pattern, as the default packaging image looks like the one below. This argument can also be written as –icon=chengzi.ico

To conclude a little bit:

Pyinstaller -f -w py_word.py Pyinstaller -f -w PY_word.py Pyinstaller -f -w -I chengzi.ico py_word.py Pyinstaller -f -w py_word.py Pyinstaller -f -w -I chengzi.ico py_word.py Package Specifies the package of exe ICONSCopy the code

The above three parameters are commonly used. See the following table for other parameters

Pyinstaller parameters

Ico Image generation

Self-made software like to put their own icon, but how come so many ICO pictures?

One is to find a dedicated ICO picture website, but are very small, the photo library is also very small.

Another can be generated, here to share a website, converted to other format pictures ico format: app.xunjiepdf.com/img2icon/

Ico image format conversion

Compressed package

All right, guys

Came to the most exciting moment, just generated exe is too big, more than 300 M software programs want to use wechat to pass it all laborious.

I’ve tried a variety of methods, including custom packaging of spec files, virtual pipenv environments, and using open source UPX compression, but it’s often either a hassle or a poor success rate.

And WHAT I want to share is that I have been using the simplest and most successful method — Conda to create a virtual environment.

Why is Python packaging big?

Before we compress, why is Python too big to pack?

Python packages exe, which is large and extremely slow. Interpreted languages are mostly like this, but Python in particular. To solve the big and slow, can only use compiled languages, such as C, C++, even VB is much better, the smallest volume is assembly. [1]

In addition, another Zhihu said that because “Anaconda has a lot of built-in libraries, packaging a lot of unnecessary modules into the package, should be pure Python packaging.”

So we can simulate a new environment where we install only the kits we need for this packaging.

That is the most suitable – virtual environment!

A virtual environment

There are many ways to create virtual environments in Python, and I’m a big Anaconda user, so if you’re like me, it’s easy. (You can also use Virtualenv, Pipenv to set up the virtual environment, use the search, the method is similar)

Let’s just memorize a few commands. It’s easy

Conda create -n Virtual environment name python==3.6 # Create virtual environment conda activate Virtual environment name # activate virtual environment conda deactivate # Exit virtual environmentCopy the code

Run the Anaconda Prompt from the Start menu and enter the command to create the virtual environment on the screen that appears. A virtual environment named AOTU based on Python version 3.6 was successfully created.

Reply y/ N to Yes to activate the virtual environment

The virtual environment installed by Conda will generate the virtual environment directory in the env directory of the Anaconda installation directory.

Of course, we can also use the command conda info –envs in the previous window to view all virtual environments under conda

Install the required libraries

We have created and enabled a virtual environment called AOTU. Enter conda list to view the libraries already installed in the current virtual environment.

When we open the Python script we want to package, we see that there are two libraries that need to be installed. Of course, you can’t do without the Essential PyInstaller library for packaging.

Script to be packed

The process of installing the library is not described here

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
Copy the code

Look again after installation

Has been installed successfully, and there are some that are not installed because these libraries come with it.

Pyinstaller package steps

I’m not going to repeat this, but I’m just going to change the apple icon

Pyinstaller -F -w -i apple.ico py_word.py
Copy the code

generate

It is compressed to 29.8M. If you do not import pandas, it should be more than 10 M

Let’s run it. No problem

Finally, a brief summary of the whole process of virtual environment + packaging (only three steps) :

Conda activate aotu #Pyinstaller install Pyinstaller -f -w -i apple.ico py_word.pyCopy the code

Sum up some potholes

1, it is still a bit metaphysical, I have performed the same process on both computers. The XLRD library was shown missing on one of them, and was successfully packaged after installation, also of the same size. You can get the file at the end of this article, try it too.

PIP install docx: PIP install docx: PIP install docx

pip install python-docx
Copy the code

There are also some libraries that can’t be used because of different versions, so it’s good to hit a brick wall.

3. In case some libraries are not installed when packaging, execute Python script in virtual environment first. If it works correctly, it’s safe to pack it.

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

conda remove -n aotu--all 
Copy the code

Python3.6 +32 – bit version as far as possible, because the win64-bit system is backward compatible with 32 – bit programs, but if you do not consider 32 – bit systems, it is ok to directly package python64 – bit version, but only on the win64-bit system. [2]

Download link

If you want to test the Python package and don’t have the right file, you can reply “Package” in the early Python background to get the following files:

The resources

[1]

Vladimir: www.zhihu.com/question/28…

[2]

Stop asking me how to package Python into EXE! : mp.weixin.qq.com/s/zilDeFunW…