Install pyinstaller to package EXE

Install pyinstaller 

In the lower left corner of pycharm, open the package manager

 Enter the package to search in the upper left corner

Click Install in the upper right corner 

After installation, set Python Interpreter in File - Settings - Projecxt:XXX

Set system environment variables

An error was reported when packaging: the pyinstaller command could not be found

pyinstaller: The item 'pyinstaller' is not recognized as the name of a cmdlet, function, script file, or executable:

This command will only be recognized in the current path after the system environment variable needs to be set.

Put the mouse on pyinstaller, the path where the package is stored will appear,

Storage package path: C:\Users\Administrator\AppData\Roaming\Python\Python310\site-packages

Scripts路径:C:\Users\Administrator\AppData\Roaming\Python\Python310\Scripts

Find the file Scripts at the same level as the storage package, and the added system variable environment is the same level of Scripts! ! ! ! !

Finding the storage path of the package is only for the convenience of finding the Scripts file. What needs to be added to the environment variable is the Scripts path. (this is just my path, not the same as yours)
 

My Computer - Properties - Advanced System Settings

 

 

 

 After changing the system variables, remember to restart the ide

settings icon

Only ico files can be set as icons. PNG and jpg formats are not acceptable. Here I will provide a free website for converting png and jpg formats to ico format. You can set the icon size according to your own needs. Click the link below to convert the URL

https://png2icojs.com/zh/

https://app.xunjiepdf.com/img2icon/

After typing, click Enter and wait for completion

console command format

1. Enter the Terminal console to edit

2. Transfer to the corresponding path (you can ignore it depending on the situation)

ps: If your file itself is located under the project, you can ignore this step

3. Enter the code in the console

enter:pyinstaller -F -w -i 图标路径(.ico) 运行文件名

例:

Pyinstaller -F py_word.py 打包exe
 
Pyinstaller -F -w py_word.py 不带控制台的打包
 
Pyinstaller -F -w -i chengzi.ico py_word.py 打包指定exe图标打包

-w means that the command line will not be opened when the program starts. If the -w parameter is not added, a black hole console window will come out. For example, I added a line of print('Hello World!') to the script just now, so 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. Also, the -w parameter is very useful when working with GUI interfaces.

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

1. Display of successful packaging

After the creation is successful, the following files will be generated ( the first two folders can be deleted directly ):

The exe in the dist folder is the executable file we need, you can pack it into a compressed package and send it to your friends for a try!

2. Find the path of the exe file

3. complete

Missing reference problem

I found out that most of the running exe files were not executed, and corresponding to my own actual situation, I found that almost 80% of the failures were caused by not citing the corresponding files.

The first method: find the dll of the module in your own virtual environment, manually copy and paste it to the packaged directory:

If there are files in the project that use relative paths, you need to copy those files and put them in the dist directory.

 

ps: References to other files are also similar solutions

If there are resources such as pictures in your program, you need to add them to the dist file according to the relative path in the program, and also add the images folder (picture resources) in the same directory as the .exe

If the original .py program uses pictures and other materials, and the added pictures are relative paths (absolute paths may not be used in the same folder), after packaging the files, you need to copy the pictures and other materials and put them in the packaged folder , otherwise the image material will not be found when running the .exe program.

If the original .py program uses an absolute path to add an image, errors may not be found when running on a local computer. But if you send the packaged file to another machine and run the .exe program, an error will be reported, which probably means "the picture and other materials cannot be found according to the absolute path you wrote", even if you copied the material in the folder, but Absolute paths are also wrong, so it's better to use the previous method.

2) The packaged main file problem

Only one module to be packaged can be specified, which is the startup module

After release, the execution path of the executable file cannot contain Chinese (preferably without spaces)

Do not have the following judgments in the file that starts the execution, otherwise the execution of the executable file will have no effect

  if __name__=='__main__':
Put the exe to run on other machines, pop up "can not find msvcr100.dll", manually copy "msvcr100.dll" to the dist directory

3) Develop good habits

When writing code, you should develop good habits, what function to use to import what function, do not just import the entire library, and finally you will find that your 100KB code is packaged to 500MB, all of which are libraries
 

The second method: set the import location when modifying the spec file, and it will be automatically recognized when packaging

The packaging process cannot find its own self-built modules such as

ModuleNotFoundError: No module named 'core'

You can add the module to datas. Note that the copied name ('core', 'core') is preferably the original name (personal experience).

You can also copy your own modules directly under site-packages, and then package them.

The packaging is successful but cannot be executed correctly, the process cannot be blocked, and the .exe keeps looping .

This is because when calling some modules, it is also a process, and in multi-process, the process in your program will not be blocked, and the process will continue to cycle. Use the following method to solve it. Note that the following code should be placed before the import module at the beginning of the code.

from multiprocessing import freeze_support

freeze_support()

multi-level tree dependencies

In special cases, the dll is packaged into the exe, and there will be such a DLL not found issue

  • It may be because the dll itself also has dependencies, so it is necessary to put the dependent package of the dll in the current development environment into the exe directory of the target environment (or directly package it into the exe) before it can be executed correctly

Otherwise, even if the source code is migrated to another machine, the same error will be reported at runtime, because the dll dependency is not supported in the target machine system

Use the dependencywalker tool to find the dependency package of the Dll:

After downloading, open the depends.exe tool and import the relevant dll to find all dependencies.

insert image description here

For example, the dll I use here has 4 dependencies:

insert image description here
insert image description here

Then   find the dependent library files in turn under C:\Windows\System32

insert image description here

Copy it to the exe execution directory, and it can run normally.
insert image description here

In order to optimize the solution, all dependent dlls can be packaged into exe, which is convenient to run on the target PC.
Please refer to:  Python directly packages DLL files into exe through PyInstaller,

打包后运行闪退


我遇到这个情况,不打包运行的好好的,打包后一运行就闪退,

不要用双击模式运行,可以用cmd或者powershell运行exe,这样闪退后打印出报错信息

报错缺少模块,这种情况很可能是没有在虚拟环境内部进行打包,详见以下文章描述,严格执行打包流程。

https://www.cnblogs.com/new-june/p/11968037.html

Solve the error that dll cannot be found when pyinstaller packaging involves uiautomation screenshots_pyinstaller packaging lacks dll_You Yichen's Blog-CSDN Blog

Body compression

The newly generated exe is really too big, and it is difficult to send a software program of more than 300 M through 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 cumbersome, or the success rate is not high (it depends on the face if the compression fails).

And what I want to share is the simplest method with a high success rate that I have been using - conda to create a virtual environment.
 

Why is the Python package so big
Before compressing and packaging, let me briefly talk about why the Python package is too large?

Python packages exe, which is not only large in size but also extremely slow to run. Most interpreted languages ​​are like this, but Python is especially prominent. To solve large and slow problems, only compiled languages, such as C, C++, and even VB are much better, and the smallest volume is assembly. [1]

In addition, there are Zhihu bosses who said that it is because "Anaconda has a lot of built-in libraries, and many unnecessary modules are packed into it when packaging, and it must be packaged with pure Python."

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

Then the most suitable is - the virtual environment!


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

Remember a few commands first, it's very simple

conda create -n virtual environment name python==3.6 #create virtual environment
 
conda activate virtual environment name #activate virtual environment
 
conda deactivate #exit virtual environment
Run "Anaconda Prompt" from the start menu, and enter the command to create a virtual environment on the interface that appears. Successfully created a virtual environment named aotu based on python version 3.6.

Make Installer installation file

Use Inno SetUp to make exe files into installation packages

Use Inno SetUp to make an installation package - Programmer Sought

Guess you like

Origin blog.csdn.net/qq_42672770/article/details/130085088