Detailed tutorial of pyinstaller packaging exe application

1. Introduction to PyInstaller

PyInstaller is a cross-platform Python application packaging tool that supports the three major platforms of Windows/Linux/MacOS. It can package Python scripts and their Python interpreters into executable files, allowing end users to install Python without installing Python. Execute the application.
The execution files produced by PyInstaller are not cross-platform. If you need to package for different platforms, you must run PyInstaller on the corresponding platform to package.

2. Install anaconda and install pyinstaller on the anaconda prompt. The installation instructions are as follows:

pip install Pyinstaller

3. The documents to be prepared are as follows:

Our ZMZ_test folder is placed on the desktop of the c drive

Guangda School Emblem.ico is an icon for generating executable files

gui2.py is a python file that needs to be packaged

4. Use Pyinstaller

Open the anaconda prompt and switch to the project directory, as shown in the following figure:

 Be sure to switch to the project directory and execute the packaging command, otherwise the generated files will be in other directories, so that you cannot find the files you need.

Enter the command to package, the command entered is

pyinstaller -F -i 广大校徽.ico gui2.py --add-data "C:\Users\lenovo\Desktop\ZMZ_test\epoch200.pth.tar;."

After waiting for a while, if you see successfully, it means success. After the packaging is successful, the following files will be generated. The exe executable file is in the dist folder.

Double-click the exe file to see the gui interface we need. 

5. Introduction of the parameters involved

6. Encountering pits and filling pits

6.1, Packaging of pure Python code

If your software only has .py files, that is, Python code files, excluding resource files such as icons and pictures, then it is very simple to use pyinstaller to package, often only need the following line of command:

pyinstaller -F -i 广大校徽.ico gui2.py

After running this command, a corresponding gui2.spec file will be generated, which will be useful later.

Several options:
-F Package the entire software (including various dependent library files) into a single file; you can also use -D instead, -D is to generate a folder and put all dependencies into the folder
-i The generated exe file will have this icon;
the last gui2.py is the entry program of the translation software

But in fact, there is a pit in packaging the ZMZ_test project. The project generally uses some plug-ins, such as ZMZ_test-html, etc. These modules are not automatically imported during the packaging process of pyinstaller, so they need to be manually written in the spec file.

Generally speaking, the ZMZ_test project can be modified as follows:

- numpy, etc. can be added according to the actual situation, mainly pytest-related installation packages

- Please replace the C:/Python38/Lib/site-packages path with your own local python path
 

a = Analysis(
    ['main_for_xxx.py',],
    pathex=[],
    binaries=[],
    datas=[('C:/Python38/Lib/site-packages/pytest_html', './pytest_html'),
           ('C:/python38/Lib/site-packages/pytest_html-3.1.1.dist-info', './pytest_html-3.1.1.dist-info'),
           ('C:/Python38/Lib/site-packages/py', './py'),
           ('C:/Python38/Lib/site-packages/pytest', './pytest'),
           ('C:/Python38/Lib/site-packages/_pytest', './_pytest'),
           ('C:/Python38/Lib/site-packages/pytest_assume', './pytest_assume'),
           ('C:/python38/Lib/site-packages/pytest_assume-2.4.3.dist-info', './pytest_assume-2.4.3.dist-info'),
           ('C:/Python38/Lib/site-packages/pytest_metadata', './pytest_metadata'),
           ('C:/python38/Lib/site-packages/pytest_metadata-2.0.1.dist-info','./pytest_metadata-2.0.1.dist-info'),
           ('C:/Python38/Lib/site-packages/numpy', './numpy'),
           ('C:/python38/Lib/site-packages/numpy-1.22.3.dist-info', './numpy-1.22.3.dist-info'),],

Among them, the content added by datas is actually the same as that of the pyinstaller --add-data command, taking ('C:/Python38/Lib/site-packages/ZMZ_test_html', './ZMZ_test_html') as an example, the tuple The first parameter represents the ZMZ_test_html folder in the local python site-packages library, and './ZMZ_test_html' represents the path where pytest packs the folder and stores it. The same add-data command is as follows: --add-data=" source address; destination address"

pyinstaller main_for_xxx.py --add-data "C:/Python38/Lib/site-packages/pytest_html ;./pytest_html"

6.2. Packages containing other files

In fact, the principle is similar to adding resource files to datas mentioned earlier, but there are more things to pay attention to.

Since my gui2.py contains the file epoch200.pth.tar, it is necessary to add the --add-data command when entering the command. The command is:

--add-data "C:\Users\lenovo\Desktop\ZMZ_test\epoch200.pth.tar;."

 If this command is not added, when running the exe file, an error will be reported that the relevant file cannot be found, and the content of the error report is:

 Also, a few important notes:

1. The command uses pyinstaller and pyinstaller.exe and the results are the same

2. -w does not display the command window, -i path of the icon file This is to change the icon, but I found that only the icon in the taskbar and the command window can be changed, and the icon of the exe file cannot be changed. In addition, these parameters need to load pyinstaller and the middle of the path.

3. There are some codes that need to call some pictures and resource files, which will not be imported automatically, and you need to manually copy them in. Otherwise, the command window will report an error that the file cannot be found when the exe file is running.

4. It is also the worst point. It is when you use the wrong parameters to package or package is interrupted halfway, and so on when the operation is halfway gone. Will cause your original py file to become a 0KB empty file. The code inside will all disappear! ! ! So you need to have a good habit in the future, that is, copy a copy of the code and use this copy for packaging. And when the parameters are wrong, or the wrong type leads to failure, check whether the py file of the copy file still exists and then continue to repackage, otherwise the typed out is an empty file, and naturally it keeps crashing, because there is no content at all.

5. When writing code, you should have a good habit, what function to use to import what function, do not import the entire library, in the end you will find that your 100KB code is packaged to 500MB, all libraries, it is speechless

6. Use quit() when debugging pygame code, otherwise the program will crash at the end. But this function is not needed to run the py file directly. This was also mentioned in our previous article. But here, when I use -w to make a windowless program, I find an error at the end, and after the pause, I find that the quit() function cannot be found. It can be seen that this function is actually a very speechless function. On the one hand, the pygame official website says that this function needs to be added when the program ends. On the other hand, it does not even need to report an error when it is running. Although it does not affect the operation, popping up a window to say "fail to execute" will always make others think you are a lowb. Therefore, add it when debugging, and remove it when executing.

Guess you like

Origin blog.csdn.net/qq_42233059/article/details/128835564