Python is packaged as an exe executable

At present, the most common method of packaging exe is through Pyinstaller to achieve, this article will also use this general method.

Install Pyinstaller

Install Pyinstaller directly in CMD using PIP command

pip install pyinstaller
Copy the code

If the network speed is too slow, switch to a domestic source to speed it up

 

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

This article code is used to illustrate, used to batch modify the JPG folder under the picture, the code is

Import OS path = os.getcwd()+'/ JPG '# fileList = os.listdir(path) n = 0for I in fileList: Oldname = path + os.sep + fileList[n] # os.sep + fileList[n] # os.sep + fileList (n Rename (oldname, newname) print(oldname, '======>', newname) n += 1Copy the code

Put the script Changename. Py and the software icon tu.ico together

1, CMD switch to the directory of the code files

Pyinstaller -f -w -I tu.ico Changename. Py Pyinstaller -f -w -I tu.ico Changename.

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

An exe application called Changename has been generated,

Pyinstaller parameter description

Pyinstaller -F -w -i tu.ico changename.py
Copy the code

Explain where the Pyinstaller parameters, where

The -f parameter represents making a standalone executable program.

-w indicates that the command line is not opened when the program is started.

-i tu.ico sets your own icon pattern, as the default package image looks like the one below. This argument can also be written as –icon=tu.ico

To conclude a little bit:

Py Pyinstaller -f -w Changename. Py Pyinstaller -f -w Changename. Py Pyinstaller -f -w -I tu.ico Changename Package Specifies the package of exe ICONSCopy the code

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

Ico Image generation

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

Another is can generate its own, to share a website, converted to other format pictures ico format: app.xunjiepdf.com/img2icon/

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.

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!

My computer conda installation is not good, so I can’t take screenshots. The general command is:

Conda create -n test python==3.6 conda activate test conda deactivateCopy the code

Then, in the test virtual environment, execute

Pyinstaller -f -w -I Tu.ico Changename. Py Package Specifies how to package an exe iconCopy the code

You get a smaller EXE file